summaryrefslogtreecommitdiff
path: root/src/Animation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Animation.zig')
-rw-r--r--src/Animation.zig41
1 files changed, 41 insertions, 0 deletions
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)),
+ };
+}