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
41
42
43
44
45
46
47
48
49
|
const root = @import("root.zig");
const rl = @cImport(@cInclude("raylib.h"));
const Animation = @This();
world_from: rl.Vector2,
world_to: rl.Vector2,
//distance: f32, // TODO frame independent durations
//speed: f32, // TODO frame independent durations
duration: u32,
tick: u32,
active: bool,
// this is wrong, just do per tile/node animations between path finding nodes
pub fn init(world_from: rl.Vector2, world_to: rl.Vector2, speed: u32) Animation {
return Animation{
.world_from = world_from,
.world_to = world_to,
//.distance = calc_distance(world_from, world_to),
//.speed = speed,
.duration = root.HexCoord.fakeDistance(root.HexCoord.worldToQr(world_from), root.HexCoord.worldToQr(world_to)) * 10 / speed, // TODO speed based duration
.tick = 0,
.active = true,
};
}
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 calc_distance(from: rl.Vector2, to: rl.Vector2) f32 {
return @abs(from.x - to.x) + @abs(from.x - to.y);
}
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)),
};
}
|