From 0085d899650006679365a1d68dcb43650c297486 Mon Sep 17 00:00:00 2001 From: Alec Goncharow Date: Sun, 11 Feb 2024 23:00:27 -0500 Subject: some cheeky animation action --- src/Animation.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Animation.zig (limited to 'src/Animation.zig') diff --git a/src/Animation.zig b/src/Animation.zig new file mode 100644 index 0000000..5201193 --- /dev/null +++ b/src/Animation.zig @@ -0,0 +1,41 @@ +const root = @import("root.zig"); +const rl = @cImport(@cInclude("raylib.h")); +const rlm = @cImport(@cInclude("raymath.h")); + +const Animation = @This(); + +world_from: rl.Vector2, +world_to: rl.Vector2, +duration: u32, // TODO frame independent durations +tick: u32, +active: bool, + +pub fn init() Animation { + return Animation{ + .world_from = .{ .x = 0.0, .y = 0.0 }, + .world_to = .{ .x = 0.0, .y = 0.0 }, + .duration = 60, // TODO speed based duration + .tick = 0, + .active = false, + }; +} + +pub fn next(self: *Animation) rl.Vector2 { + if (self.tick >= self.duration) { + self.active = false; + return self.world_to; + } + + self.tick += 1; + return self.lerp(); +} + +inline fn lerp(self: *Animation) rl.Vector2 { + const t: f32 = @as(f32, @floatFromInt(self.tick)) / @as(f32, @floatFromInt(self.duration)); + const v1 = self.world_from; + const v2 = self.world_to; + return .{ + .x = v1.x + (t * (v2.x - v1.x)), + .y = v1.y + (t * (v2.y - v1.y)), + }; +} -- cgit v1.2.3-70-g09d2