diff --git a/build.zig.zon b/build.zig.zon index 5ed5f4b1..cfd83b9a 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "jok", - .version = "0.24.1", + .version = "0.24.2", .paths = .{ "README.md", "build.zig", diff --git a/src/j2d/AnimationSystem.zig b/src/j2d/AnimationSystem.zig index ed15aeca..b74d8dfb 100644 --- a/src/j2d/AnimationSystem.zig +++ b/src/j2d/AnimationSystem.zig @@ -37,12 +37,10 @@ pub fn add( self: *Self, name: []const u8, frames: []const Frame, - durations: []const f32, loop: bool, ) !void { assert(name.len > 0); assert(frames.len > 0); - assert(frames.len == durations.len); if (self.animations.get(name) != null) { return error.NameUsed; } @@ -55,12 +53,10 @@ pub fn add( .play_index = 0, .passed_time = 0, .is_over = false, + .is_stopped = false, }; errdefer self.allocator.free(anim.frames); - for (anim.frames, 0..) |*f, i| { - f.data = frames[i]; - f.duration = durations[i]; - } + @memcpy(anim.frames, frames); try self.animations.put(dname, anim); } @@ -87,6 +83,7 @@ pub fn addSimple( .play_index = 0, .passed_time = 0, .is_over = false, + .is_stopped = false, }; errdefer self.allocator.free(anim.frames); const duration = 1.0 / fps; @@ -145,7 +142,7 @@ pub fn isOver(self: Self, name: []const u8) !bool { return error.NameNotExist; } -/// Reset animation's status +/// Reset animation's status (clear over/stop state) pub fn reset(self: *Self, name: []const u8) !void { if (self.animations.getPtr(name)) |anim| { return anim.reset(); @@ -153,6 +150,22 @@ pub fn reset(self: *Self, name: []const u8) !void { return error.NameNotExist; } +/// Set animation's stop state +pub fn setStop(self: *Self, name: []const u8, b: bool) !void { + if (self.animations.getPtr(name)) |anim| { + anim.is_stopped = b; + } + return error.NameNotExist; +} + +/// Set animation's loop state +pub fn setLoop(self: *Self, name: []const u8, b: bool) !void { + if (self.animations.getPtr(name)) |anim| { + anim.loop = b; + } + return error.NameNotExist; +} + pub const Frame = struct { pub const Data = union(enum) { sp: Sprite, @@ -170,19 +183,21 @@ pub const Animation = struct { play_index: u32, passed_time: f32, is_over: bool, + is_stopped: bool, pub fn reset(anim: *Animation) void { anim.play_index = 0; anim.passed_time = 0; anim.is_over = false; + anim.is_stopped = false; } pub fn getCurrentFrame(self: Animation) Frame.Data { return self.frames[self.play_index].data; } - pub fn update(anim: *Animation, delta_tick: f32) void { - if (anim.is_over) return; + fn update(anim: *Animation, delta_tick: f32) void { + if (anim.is_over or anim.is_stopped) return; anim.passed_time += delta_tick; while (anim.passed_time >= anim.frames[anim.play_index].duration) { anim.passed_time -= anim.frames[anim.play_index].duration;