summaryrefslogtreecommitdiff
path: root/src/Animation.zig
blob: 93255ec7d1212edb0b37e736c0350d858b8c71a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const root = @import("root.zig");
const rl = @cImport(@cInclude("raylib.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)),
    };
}