Skip to content

Commit

Permalink
enhance 2d animation system: add wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 2, 2024
1 parent 6cc99e2 commit 834db8b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "jok",
.version = "0.24.2",
.version = "0.25.0",
.paths = .{
"README.md",
"build.zig",
Expand Down
25 changes: 13 additions & 12 deletions examples/animation_2d.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn init(ctx: jok.Context) !void {
.{ .sp = player.getSubSprite(3 * 16, 0, 16, 16) },
},
6,
false,
.{},
);
try as.addSimple(
"player_up",
Expand All @@ -53,7 +53,7 @@ pub fn init(ctx: jok.Context) !void {
.{ .sp = player.getSubSprite(6 * 16, 0, 16, 16) },
},
6,
false,
.{},
);
try as.addSimple(
"player_down",
Expand All @@ -63,18 +63,19 @@ pub fn init(ctx: jok.Context) !void {
.{ .sp = player.getSubSprite(0 * 16, 0, 16, 16) },
},
6,
false,
.{},
);
var dcmds: [10]j2d.AnimationSystem.Frame.Data = undefined;
for (0..10) |i| {
var dcmds: [20]j2d.AnimationSystem.Frame.Data = undefined;
for (0..dcmds.len) |i| {
dcmds[i] = .{
.dcmd = .{
.cmd = .{
.circle_fill = .{
.circle = .{
.p = .{ .x = 0, .y = 0 },
.radius = @floatFromInt(i * 10),
.color = jok.Color.rgb(@intCast(i * 48 % 255), @intCast(i * 16 % 255), @intCast(i * 32 % 255)).toInternalColor(),
.num_segments = @intCast(i),
.radius = @floatFromInt(@abs(100 - @as(i32, @intCast(i)) * 10)),
.color = jok.Color.rgb(@intCast(i * 50 % 255), @intCast(i * 50 % 255), 0).toInternalColor(),
.num_segments = 20,
.thickness = 6,
},
},
.depth = 0.5,
Expand All @@ -84,8 +85,8 @@ pub fn init(ctx: jok.Context) !void {
try as.addSimple(
"player_circle_bg",
&dcmds,
8,
true,
10,
.{ .loop = true },
);
}

Expand Down Expand Up @@ -138,7 +139,7 @@ pub fn draw(ctx: jok.Context) !void {
.scale = .{ .x = 4, .y = 4 },
},
);
{
if (!try as.isStopped("player_circle_bg")) {
try b.pushTransform(j2d.AffineTransform.init().translate(pos));
defer b.popTransform();
try b.pushDrawCommand(((try as.getCurrentFrame("player_circle_bg")).dcmd));
Expand Down
39 changes: 33 additions & 6 deletions src/j2d/AnimationSystem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ pub fn destroy(self: *Self) void {
self.allocator.destroy(self);
}

pub const AnimOption = struct {
wait_time: f32 = 0,
loop: bool = false,
};

/// Add animation, each frame has its own duration
pub fn add(
self: *Self,
name: []const u8,
frames: []const Frame,
loop: bool,
opt: AnimOption,
) !void {
assert(name.len > 0);
assert(frames.len > 0);
Expand All @@ -56,7 +61,8 @@ pub fn add(
const anim = Animation{
.name = dname,
.frames = try self.allocator.alloc(Frame, frames.len),
.loop = loop,
.wait_time = opt.wait_time,
.loop = opt.loop,
.play_index = 0,
.passed_time = 0,
.is_over = false,
Expand All @@ -73,7 +79,7 @@ pub fn addSimple(
name: []const u8,
frames: []const Frame.Data,
fps: f32,
loop: bool,
opt: AnimOption,
) !void {
assert(name.len > 0);
assert(frames.len > 0);
Expand All @@ -86,7 +92,8 @@ pub fn addSimple(
const anim = Animation{
.name = dname,
.frames = try self.allocator.alloc(Frame, frames.len),
.loop = loop,
.wait_time = opt.wait_time,
.loop = opt.loop,
.play_index = 0,
.passed_time = 0,
.is_over = false,
Expand Down Expand Up @@ -152,10 +159,23 @@ pub fn isOver(self: Self, name: []const u8) !bool {
return error.NameNotExist;
}

// Get animation's stop state
pub fn isStopped(self: Self, name: []const u8) !bool {
if (self.animations.get(name)) |anim| {
return anim.is_stopped;
}
return error.NameNotExist;
}

/// Reset animation's status (clear over/stop state)
pub fn reset(self: *Self, name: []const u8) !void {
try self.resetWait(name, 0);
}

/// Reset animation's status (clear over/stop state), with wait time
pub fn resetWait(self: *Self, name: []const u8, wait_time: f32) !void {
if (self.animations.getPtr(name)) |anim| {
return anim.reset();
return anim.reset(wait_time);
}
return error.NameNotExist;
}
Expand Down Expand Up @@ -191,13 +211,15 @@ pub const Frame = struct {
pub const Animation = struct {
name: []u8,
frames: []Frame,
wait_time: f32,
loop: bool,
play_index: u32,
passed_time: f32,
is_over: bool,
is_stopped: bool,

pub fn reset(anim: *Animation) void {
pub fn reset(anim: *Animation, wait_time: f32) void {
anim.wait_time = wait_time;
anim.play_index = 0;
anim.passed_time = 0;
anim.is_over = false;
Expand All @@ -211,6 +233,11 @@ pub const Animation = struct {
fn update(anim: *Animation, delta_tick: f32) void {
if (anim.is_over or anim.is_stopped) return;
anim.passed_time += delta_tick;
if (anim.passed_time < anim.wait_time) return;
if (anim.wait_time > 0) {
anim.passed_time -= anim.wait_time;
anim.wait_time = 0; // only take effect once
}
while (anim.passed_time >= anim.frames[anim.play_index].duration) {
anim.passed_time -= anim.frames[anim.play_index].duration;
anim.play_index += 1;
Expand Down

0 comments on commit 834db8b

Please sign in to comment.