use super::CritterState;
use std::f64::consts::{PI, TAU};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CritterPose {
pub dx: f64,
pub dy: f64,
pub rot: f64,
pub sx: f64,
pub sy: f64,
pub morph: f64,
pub yaw: f64,
pub eye_w: f64,
pub eye_h: f64,
pub pill: f64,
pub eye_dx: f64,
pub eye_dy: f64,
pub blink: f64,
}
impl Default for CritterPose {
fn default() -> Self {
Self::REST
}
}
impl CritterPose {
pub const REST: CritterPose = CritterPose {
dx: 0.0,
dy: 0.0,
rot: 0.0,
sx: 1.0,
sy: 1.0,
morph: 0.0,
yaw: 0.0,
eye_w: 1.0,
eye_h: 1.0,
pill: 0.0,
eye_dx: 0.0,
eye_dy: 0.0,
blink: 0.0,
};
pub fn lerp(&self, o: &CritterPose, k: f64) -> CritterPose {
let l = |a: f64, b: f64| a + (b - a) * k;
CritterPose {
dx: l(self.dx, o.dx),
dy: l(self.dy, o.dy),
rot: l(self.rot, o.rot),
sx: l(self.sx, o.sx),
sy: l(self.sy, o.sy),
morph: l(self.morph, o.morph),
yaw: l(self.yaw, o.yaw),
eye_w: l(self.eye_w, o.eye_w),
eye_h: l(self.eye_h, o.eye_h),
pill: l(self.pill, o.pill),
eye_dx: l(self.eye_dx, o.eye_dx),
eye_dy: l(self.eye_dy, o.eye_dy),
blink: l(self.blink, o.blink),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CritterTrick {
TiltLeft,
TiltRight,
LookAround,
TurnLeft,
TurnRight,
Spin,
Somersault,
Hop,
DoubleBlink,
Wobble,
LookAtViewer,
SmallHop,
}
use CritterTrick::*;
pub const WORKING_TRICKS: &[CritterTrick] =
&[TiltLeft, TiltRight, LookAround, TurnLeft, TurnRight, Spin, Somersault, Hop, DoubleBlink, Wobble];
pub const ATTENTION_TRICKS: &[CritterTrick] = &[LookAtViewer, SmallHop, LookAtViewer, DoubleBlink, SmallHop];
pub const TRICK_SLOT: (f64, f64) = (1.6, 2.2);
fn ease(x: f64) -> f64 {
let x = x.clamp(0.0, 1.0);
x * x * (3.0 - 2.0 * x)
}
fn back_out(x: f64) -> f64 {
let x = x.clamp(0.0, 1.0) - 1.0;
let c = 1.2;
1.0 + (c + 1.0) * x * x * x + c * x * x
}
fn hold(p: f64) -> f64 {
if p < 0.3 {
back_out(p / 0.3)
} else if p < 0.7 {
1.0
} else {
1.0 - ease((p - 0.7) / 0.3)
}
}
fn bell(p: f64, a: f64, b: f64) -> f64 {
if p <= a || p >= b {
0.0
} else {
(PI * (p - a) / (b - a)).sin().powi(2)
}
}
fn arc(p: f64, a: f64, b: f64) -> f64 {
if p <= a || p >= b {
0.0
} else {
(PI * (p - a) / (b - a)).sin()
}
}
fn squash(p: f64, a: f64, b: f64, k: f64) -> (f64, f64) {
let s = bell(p, a - 0.18, a) + bell(p, b, b + 0.2) * 1.2;
let st = bell(p, a, a + 0.18) * 0.6; (1.0 + k * s - 0.5 * k * st, 1.0 - k * s + k * st)
}
pub fn trick_pose(trick: CritterTrick, p: f64) -> CritterPose {
let p = p.clamp(0.0, 1.0);
let mut o = CritterPose::REST;
match trick {
TiltLeft | TiltRight => {
let s = if trick == TiltLeft { -1.0 } else { 1.0 };
let h = hold(p);
o.rot = s * 0.32 * h;
o.dx = s * 2.0 * h;
o.eye_dx = s * 2.0 * h;
o.eye_dy = 1.0 * h;
}
LookAround => {
let e = hold(p);
o.eye_dx = 4.0 * (TAU * p).sin() * e;
o.eye_dy = -2.5 * (TAU * p).cos().max(0.0) * e;
o.eye_w = 1.0 + 0.15 * e;
o.eye_h = 1.0 - 0.1 * e;
}
TurnLeft | TurnRight => {
let s = if trick == TurnLeft { -1.0 } else { 1.0 };
let h = hold(p);
o.yaw = s * h;
o.pill = h;
o.eye_dx = s * 6.0 * h;
o.rot = s * 0.08 * h;
}
Spin => {
o.rot = TAU * ease(p);
let m = bell(p, 0.1, 0.9);
o.morph = 0.9 * m;
o.sx = 1.0 + 0.08 * m;
o.sy = 1.0 - 0.06 * m;
o.eye_w = 1.0 - 0.3 * m;
o.eye_h = 1.0 - 0.45 * m;
}
Somersault => {
let (a, b) = (0.2, 0.75);
let q = ((p - a) / (b - a)).clamp(0.0, 1.0);
o.dy = -9.0 * arc(p, a, b);
o.rot = TAU * ease(q);
let m = bell(p, a, b);
o.morph = m;
o.eye_h = 1.0 - 0.5 * m;
let (x, y) = squash(p, a, b, 0.14);
o.sx = x;
o.sy = y;
}
Hop => {
let (a, b) = (0.2, 0.65);
o.dy = -8.0 * arc(p, a, b);
let (x, y) = squash(p, a, b, 0.16);
o.sx = x;
o.sy = y;
o.eye_dy = -1.5 * arc(p, a, b);
o.eye_h = 1.0 + 0.2 * arc(p, a, b);
}
DoubleBlink => {
o.blink = bell(p, 0.25, 0.4) + bell(p, 0.5, 0.65);
o.eye_dy = 0.5 * bell(p, 0.2, 0.7);
}
Wobble => {
let d = 1.0 - ease(p);
o.rot = 0.22 * (3.0 * TAU * p).sin() * d * ease(p * 5.0);
o.sx = 1.0 + 0.05 * (2.0 * TAU * p).sin() * d;
o.sy = 2.0 - o.sx;
}
LookAtViewer => {
let h = hold(p);
o.eye_w = 1.0 + 0.3 * h;
o.eye_h = 1.0 - 0.1 * h;
o.eye_dy = 1.5 * h;
o.sx = 1.0 + 0.04 * h;
o.sy = 1.0 + 0.04 * h;
o.blink = bell(p, 0.55, 0.65);
}
SmallHop => {
let (a, b) = (0.25, 0.6);
o.dy = -4.0 * arc(p, a, b);
let (x, y) = squash(p, a, b, 0.08);
o.sx = x;
o.sy = y;
}
}
o
}
fn mix(seed: u32, slot: i64) -> u32 {
let mut h = seed ^ (slot as u32).wrapping_mul(0x9E37_79B9) ^ ((slot >> 32) as u32);
h ^= h >> 16;
h = h.wrapping_mul(0x7FEB_352D);
h ^= h >> 15;
h = h.wrapping_mul(0x846C_A68B);
h ^ (h >> 16)
}
fn order(n: usize, seed: u32, c: i64) -> [u8; 16] {
let mut o = [0u8; 16];
for (i, v) in o.iter_mut().enumerate() {
*v = i as u8;
}
let mut h = mix(seed, c);
for i in (1..n.min(16)).rev() {
h = mix(h, i as i64);
o.swap(i, h as usize % (i + 1));
}
o
}
pub fn trick_for(set: &[CritterTrick], seed: u32, slot: i64) -> CritterTrick {
let n = set.len().clamp(1, 16) as i64;
let (c, i) = (slot.div_euclid(n), slot.rem_euclid(n) as usize);
let mut cur = order(n as usize, seed, c);
if n > 1 && cur[0] == order(n as usize, seed, c - 1)[n as usize - 1] {
cur.swap(0, 1);
}
set[cur[i] as usize]
}
pub fn critter_pose(state: CritterState, t: f64, seed: u32, reduced: bool) -> CritterPose {
use CritterState::*;
let (set, slot) = match state {
Idle | Off => return CritterPose::REST,
Working => (WORKING_TRICKS, TRICK_SLOT.0),
Thinking => (WORKING_TRICKS, TRICK_SLOT.0 * 1.4),
Attention => (ATTENTION_TRICKS, TRICK_SLOT.1),
};
let off = (seed % 997) as f64 / 997.0 * slot;
if reduced {
let mut o = CritterPose::REST;
o.rot = 0.07 * (TAU * (t + off) / (2.0 * slot)).sin();
return o;
}
let u = (t + off) / slot;
let n = u.floor();
let mut o = trick_pose(trick_for(set, seed, n as i64), u - n);
if state == Thinking {
o.eye_dy -= 1.5;
}
o
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: &CritterPose, b: &CritterPose) -> bool {
let f = |x: f64, y: f64| (x - y).abs() < 1e-6;
f(a.dx, b.dx) && f(a.dy, b.dy) && f(a.rot.rem_euclid(TAU) % (TAU - 1e-9), b.rot.rem_euclid(TAU) % (TAU - 1e-9))
&& f(a.sx, b.sx) && f(a.sy, b.sy) && f(a.morph, b.morph) && f(a.yaw, b.yaw)
&& f(a.eye_w, b.eye_w) && f(a.eye_h, b.eye_h) && f(a.pill, b.pill)
&& f(a.eye_dx, b.eye_dx) && f(a.eye_dy, b.eye_dy) && f(a.blink, b.blink)
}
#[test]
fn every_trick_starts_and_ends_at_rest() {
for &k in WORKING_TRICKS.iter().chain(ATTENTION_TRICKS) {
assert!(close(&trick_pose(k, 0.0), &CritterPose::REST), "{k:?} at 0");
assert!(close(&trick_pose(k, 1.0), &CritterPose::REST), "{k:?} at 1");
}
}
#[test]
fn tricks_never_snap() {
for seed in [1u32, 77, 4242] {
for st in [CritterState::Working, CritterState::Attention] {
let mut prev = critter_pose(st, 0.0, seed, false);
for i in 1..2000 {
let p = critter_pose(st, i as f64 / 240.0, seed, false);
let dr = (p.rot - prev.rot).rem_euclid(TAU);
let d = dr.min(TAU - dr) + (p.dy - prev.dy).abs() + (p.eye_dx - prev.eye_dx).abs()
+ (p.morph - prev.morph).abs() + (p.sx - prev.sx).abs() + (p.blink - prev.blink).abs();
assert!(d < 0.6, "{st:?} seed {seed} jumps at step {i}: {d}");
prev = p;
}
}
}
}
#[test]
fn states_and_sets() {
assert_eq!(critter_pose(CritterState::Idle, 3.3, 9, false), CritterPose::REST);
assert_eq!(critter_pose(CritterState::Off, 3.3, 9, false), CritterPose::REST);
let r = critter_pose(CritterState::Working, 1.0, 9, true);
assert!(r.rot.abs() <= 0.07 && r.morph == 0.0 && r.dy == 0.0);
for s in 0..200 {
assert!(ATTENTION_TRICKS.contains(&trick_for(ATTENTION_TRICKS, 5, s)));
assert_ne!(trick_for(WORKING_TRICKS, 5, s), trick_for(WORKING_TRICKS, 5, s - 1), "slot {s}");
}
let used: Vec<_> = (0..400).map(|s| trick_for(WORKING_TRICKS, 5, s)).collect();
assert!(WORKING_TRICKS.iter().all(|k| used.contains(k)));
let other: Vec<_> = (0..40).map(|s| trick_for(WORKING_TRICKS, 6, s)).collect();
assert_ne!(used[..40], other[..]);
let mid = trick_pose(CritterTrick::Spin, 0.5);
assert!(mid.morph > 0.8 && (mid.rot - PI).abs() < 1e-6);
assert!(trick_pose(CritterTrick::Hop, 0.42).dy < -7.0);
let land = trick_pose(CritterTrick::Hop, 0.75);
assert!(land.sx > 1.05 && land.sy < 0.95);
}
}