summaryrefslogtreecommitdiff
path: root/src/gui/pet.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/pet.zig')
-rw-r--r--src/gui/pet.zig29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/gui/pet.zig b/src/gui/pet.zig
index a249d70f..7a13ca91 100644
--- a/src/gui/pet.zig
+++ b/src/gui/pet.zig
@@ -13,8 +13,8 @@ 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;
@@ -32,11 +32,10 @@ pub const State = struct {
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;
@@ -142,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;
@@ -159,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).?);
@@ -180,7 +181,7 @@ 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, false).?;
+ const region = lane(1, 8, 800, 0, 16).?;
var state: State = .{};
var last = state.sample(.frog, 0, region).?;
var rested_midway = false;
@@ -195,7 +196,7 @@ test "pet rests at random spots and the frog travels only in the air" {
}
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).?;