diff options
Diffstat (limited to 'src/gui/pet.zig')
| -rw-r--r-- | src/gui/pet.zig | 113 |
1 files changed, 79 insertions, 34 deletions
diff --git a/src/gui/pet.zig b/src/gui/pet.zig index 2b031f0e..7a13ca91 100644 --- a/src/gui/pet.zig +++ b/src/gui/pet.zig @@ -8,11 +8,13 @@ pub const height = 9; pub const max_vertices = width * height * 6; pub const Lane = struct { left: f32, right: f32, top: f32, scale: f32 }; pub const Pose = struct { x: f32, y: f32, scale: f32, left: bool, walking: bool, frame: u8 }; +/// The frog's hop: sits two ticks, takes off, peaks for two, comes down, lands. +const hop_ticks = 8; /// Only the trailing blank part of the global tag is eligible. The caller /// supplies its last occupied cell, including any wide-grapheme continuation. -pub fn lane(occupied: usize, cell_w: f32, screen_w: f32, top: f32, band_h: f32, editing: bool) ?Lane { - if (editing or cell_w <= 0 or band_h < height + 2) return null; +pub fn lane(occupied: usize, cell_w: f32, screen_w: f32, top: f32, band_h: f32) ?Lane { + if (cell_w <= 0 or band_h < height + 2) return null; const scale = @min(3, @floor((band_h - 2) / height)); const left = @as(f32, @floatFromInt(occupied + 2)) * cell_w; const right = screen_w - 2 * cell_w - width * scale; @@ -24,24 +26,26 @@ pub const State = struct { kind: Kind = .off, tick: ?u64 = null, x: f32 = 0, + target: f32 = 0, left: bool = false, idle: u8 = 12, frame: u8 = 0, + rng: std.Random.DefaultPrng = undefined, + /// No lane (the tag's text has filled it) hides the pet but keeps + /// where it was and what it was doing. pub fn sample(s: *State, kind: Kind, now_ms: u64, area: ?Lane) ?Pose { - const region = area orelse { - s.* = .{}; - return null; - }; + const region = area orelse return null; if (kind == .off) { s.* = .{}; return null; } const tick = now_ms / 100; if (s.kind != kind or s.tick == null) { - s.* = .{ .kind = kind, .tick = tick, .x = region.left }; + s.* = .{ .kind = kind, .tick = tick, .x = region.left, .target = region.left, .rng = .init(now_ms) }; } s.x = std.math.clamp(s.x, region.left, region.right); + s.target = std.math.clamp(s.target, region.left, region.right); // Coming back from suspension never launches the pet across the tag. const steps = @min(tick -| s.tick.?, 2); s.tick = tick; @@ -49,15 +53,25 @@ pub const State = struct { s.frame +%= 1; if (s.idle > 0) { s.idle -= 1; + if (s.idle == 0) { + // Wander to a random spot of the lane; one too close + // would be a shuffle in place, so take the far end. + s.target = region.left + s.rng.random().float(f32) * (region.right - region.left); + if (@abs(s.target - s.x) < 8 * region.scale) + s.target = if (s.x - region.left > region.right - s.x) region.left else region.right; + s.left = s.target < s.x; + s.frame = 255; // the walk starts at frame 0 + } continue; } - const speed: f32 = if (kind == .cat) 1.8 else 1.3; - s.x += (if (s.left) -speed else speed) * region.scale; - if (s.x >= region.right or s.x <= region.left) { - s.x = std.math.clamp(s.x, region.left, region.right); - s.left = !s.left; - s.idle = if (kind == .cat) 18 else 24; + // The cat walks steadily; the frog travels only while airborne. + const phase = s.frame % hop_ticks; + if (kind == .cat or (phase >= 2 and phase < 6)) { + const step = (if (kind == .cat) @as(f32, 1.8) else 2.6) * region.scale; + s.x = if (s.left) @max(s.target, s.x - step) else @min(s.target, s.x + step); } + if (s.x == s.target and (kind == .cat or phase == hop_ticks - 1)) + s.idle = s.rng.random().intRangeAtMost(u8, 20, 60); } return .{ .x = @floor(s.x), .y = region.top, .scale = region.scale, .left = s.left, .walking = s.idle == 0, .frame = s.frame }; } @@ -76,14 +90,25 @@ const cat = [_][]const u8{ }; const frog = [_][]const u8{ " ", - " ## ## ", - " #o####o# ", - " ######## ", - " ###### ", - " ######## ", - " ########## ", - " ## ## ", - "### ###", + " ", + " ", + " ## ", + " #o## ", + " #########", + " ######### ", + " ##### ## ", + "###### ### ", +}; +const frog_leap = [_][]const u8{ + " ## ", + " #o## ", + " #######", + " ######## ", + " ####### ", + " ### ## ", + "## # ", + "# ", + " ", }; /// 0 transparent, 1 theme ink, 2 eye cutout. Walk legs and idle blinks are @@ -99,13 +124,16 @@ pub fn pixel(kind: Kind, pose: Pose, x_in: usize, y: usize) u2 { if (!pose.walking and breathing) { if (kind == .cat and y == 3 and x <= 1) return @intFromBool(x == 0); - if (kind == .frog and y == 4 and (x == 2 or x == 9)) return 1; + if (kind == .frog and ((y == 6 and x == 11) or (y == 7 and x == 10))) return 1; } - if (pose.walking and alternate and y >= 7) { - if (kind == .cat) return @intFromBool((y == 7 and (x == 3 or x == 7)) or (y == 8 and (x == 2 or x == 8))); - return @intFromBool((y == 7 and (x == 2 or x == 9)) or (y == 8 and (x <= 2 or x >= 9))); - } - const ch = (if (kind == .cat) cat else frog)[y][x]; + if (kind == .cat and pose.walking and alternate and y >= 7) + return @intFromBool((y == 7 and (x == 3 or x == 7)) or (y == 8 and (x == 2 or x == 8))); + // A hopping frog leaps two rows low at take-off and one on the way down. + const phase = pose.frame % hop_ticks; + const leaping = kind == .frog and pose.walking and phase >= 2 and phase < 6; + const sink: usize = if (!leaping) 0 else if (phase == 2) 2 else if (phase == 5) 1 else 0; + if (y < sink) return 0; + const ch = (if (kind == .cat) cat else if (leaping) frog_leap else frog)[y - sink][x]; return switch (ch) { '#' => 1, 'o' => if (!pose.walking and pose.frame % 30 >= 27) 1 else 2, @@ -113,11 +141,10 @@ pub fn pixel(kind: Kind, pose: Pose, x_in: usize, y: usize) u2 { }; } -test "pet stays in trailing workspace blanks and disappears during editing" { - try std.testing.expect(lane(20, 8, 200, 0, 16, false) == null); - try std.testing.expect(lane(3, 8, 200, 0, 16, true) == null); - try std.testing.expect(lane(3, 8, 200, 0, 10, false) == null); - const region = lane(3, 8, 200, 2, 20, false).?; +test "pet stays in trailing workspace blanks, and a lost lane hides it without a reset" { + try std.testing.expect(lane(20, 8, 200, 0, 16) == null); + try std.testing.expect(lane(3, 8, 200, 0, 10) == null); + const region = lane(3, 8, 200, 2, 20).?; try std.testing.expect(region.left >= 5 * 8); var state: State = .{}; var moved = false; @@ -130,12 +157,15 @@ test "pet stays in trailing workspace blanks and disappears during editing" { reversed = reversed or pose.left; } try std.testing.expect(moved and reversed); + const kept = state; try std.testing.expect(state.sample(.cat, 300000, null) == null); + try std.testing.expectEqual(kept.x, state.x); + try std.testing.expectEqual(kept.tick, state.tick); try std.testing.expect(state.sample(.off, 300100, region) == null); } test "pet clock is fixed rate and sprites bounded in both directions" { - const region = lane(1, 8, 400, 0, 16, false).?; + const region = lane(1, 8, 400, 0, 16).?; var state: State = .{}; const start = state.sample(.frog, 1000, region).?; try std.testing.expectEqualDeep(start, state.sample(.frog, 1099, region).?); @@ -150,8 +180,23 @@ test "pet clock is fixed rate and sprites bounded in both directions" { }; } +test "pet rests at random spots and the frog travels only in the air" { + const region = lane(1, 8, 800, 0, 16).?; + var state: State = .{}; + var last = state.sample(.frog, 0, region).?; + var rested_midway = false; + for (1..3000) |i| { + const pose = state.sample(.frog, i * 100, region).?; + const phase = pose.frame % hop_ticks; + if (pose.x != last.x) try std.testing.expect(pose.walking and phase >= 2 and phase < 6); + rested_midway = rested_midway or (last.walking and !pose.walking and pose.x > region.left and pose.x < @floor(region.right)); + last = pose; + } + try std.testing.expect(rested_midway); +} + test "pet initial idle visibly animates without walking or leaving its tile" { - const region = lane(1, 8, 400, 0, 16, false).?; + const region = lane(1, 8, 400, 0, 16).?; for ([_]Kind{ .cat, .frog }) |kind| { var state: State = .{}; const initial = state.sample(kind, 0, region).?; |
