summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-21 21:05:10 -0300
committerGabriel Schneider <[email protected]>2026-10-22 10:03:15 -0300
commit15325f929c0c25e97ee98aaa613808661a864e1d (patch)
tree0d760a7effb750f300ae8dcaa15ac956a9b2db89 /src/gui
parent23518c9d7c061f8a06f6632da8b64b37801b514f (diff)
downloadpardes-15325f929c0c25e97ee98aaa613808661a864e1d.tar.gz
pardes-15325f929c0c25e97ee98aaa613808661a864e1d.zip
Pet: wander between random resting spots; the frog hops
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/pet.zig86
1 files changed, 65 insertions, 21 deletions
diff --git a/src/gui/pet.zig b/src/gui/pet.zig
index 2b031f0e..a249d70f 100644
--- a/src/gui/pet.zig
+++ b/src/gui/pet.zig
@@ -8,6 +8,8 @@ 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.
@@ -24,9 +26,11 @@ 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,
pub fn sample(s: *State, kind: Kind, now_ms: u64, area: ?Lane) ?Pose {
const region = area orelse {
@@ -39,9 +43,10 @@ pub const State = struct {
}
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 +54,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 +91,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 +125,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 (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)));
+ if (kind == .frog and ((y == 6 and x == 11) or (y == 7 and x == 10))) return 1;
}
- 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,
@@ -150,6 +179,21 @@ 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).?;
+ 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).?;
for ([_]Kind{ .cat, .frog }) |kind| {