diff options
| m--------- | raylib | 0 | ||||
| -rw-r--r-- | src/Animation.zig | 21 | ||||
| -rw-r--r-- | src/root.zig | 23 |
3 files changed, 29 insertions, 15 deletions
diff --git a/raylib b/raylib -Subproject 0656440e38208e4d57201706520eb0571fb13bd +Subproject a8ec8472ff29b228f6c51b4156d140d4783fd33 diff --git a/src/Animation.zig b/src/Animation.zig index 93255ec..fe5a51e 100644 --- a/src/Animation.zig +++ b/src/Animation.zig @@ -5,17 +5,22 @@ const Animation = @This(); world_from: rl.Vector2, world_to: rl.Vector2, -duration: u32, // TODO frame independent durations +//distance: f32, // TODO frame independent durations +//speed: f32, // TODO frame independent durations +duration: u32, tick: u32, active: bool, -pub fn init() Animation { +// 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 = .{ .x = 0.0, .y = 0.0 }, - .world_to = .{ .x = 0.0, .y = 0.0 }, - .duration = 60, // TODO speed based duration + .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 = false, + .active = true, }; } @@ -29,6 +34,10 @@ pub fn next(self: *Animation) rl.Vector2 { 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; diff --git a/src/root.zig b/src/root.zig index 00a56ad..29e5d72 100644 --- a/src/root.zig +++ b/src/root.zig @@ -49,6 +49,10 @@ pub const HexCoord = struct { return cubeRound(rl.Vector3{ .x = frac.x, .y = frac.y, .z = fracS(frac.x, frac.y) }); } + pub inline fn fakeDistance(from: HexCoord, to: HexCoord) u32 { + return @abs(from.q - to.q) + @abs(from.r - to.r); + } + pub inline fn cubeRound(frac: rl.Vector3) HexCoord { var q = @round(frac.x); var r = @round(frac.y); @@ -86,8 +90,9 @@ pub fn setup() !void { .world_coords = .{ .x = 0.0, .y = 0.0 }, .hex_coords = .{ .q = (grid_size / 2) + 1, .r = (grid_size / 2) + 1 }, .target_coords = .{ .q = (grid_size / 2) + 1, .r = (grid_size / 2) + 1 }, - .animation = Animation.init(), + .animation = Animation.init(.{ .x = 0.0, .y = 0.0 }, .{ .x = 0.0, .y = 0.0 }, 1.0), }; + context.main_dude.animation.active = false; context.main_dude.world_coords = HexCoord.qrToWorld(@intCast(context.main_dude.hex_coords.q), @intCast(context.main_dude.hex_coords.r)); // TODO think what it means to populate a hex grid @@ -124,7 +129,7 @@ pub fn update() !void { // TODO FIXME laptop dev doesnt allow middle mouse and mouse move to happen // at same time and im not yak shavin this - if (rl.IsMouseButtonDown(rl.MOUSE_BUTTON_RIGHT)) { + if (rl.IsMouseButtonDown(rl.MOUSE_BUTTON_RIGHT) or rl.IsMouseButtonDown(rl.MOUSE_BUTTON_MIDDLE)) { const delta = rl.GetMouseDelta(); var scale = @log2(@abs(delta.x) + @abs(delta.y)); if (delta.x == 0.0 and delta.y == 0.0) { @@ -137,15 +142,15 @@ pub fn update() !void { if (main_dude.animation.active) { main_dude.world_coords = main_dude.animation.next(); - main_dude.hex_coords = main_dude.target_coords; + main_dude.hex_coords = HexCoord.worldToQr(main_dude.world_coords); } - if (!main_dude.animation.active and rl.IsMouseButtonDown(rl.MOUSE_BUTTON_LEFT)) { - main_dude.animation.world_to = context.hovered_coords.toWorld(); - main_dude.animation.world_from = main_dude.hex_coords.toWorld(); - main_dude.animation.active = true; - main_dude.animation.tick = 0; - main_dude.target_coords = context.hovered_coords; + if (rl.IsMouseButtonDown(rl.MOUSE_BUTTON_LEFT)) { + if (!(main_dude.animation.world_to.x == context.hovered_coords.toWorld().x and main_dude.animation.world_to.y == context.hovered_coords.toWorld().y)) { + main_dude.animation = Animation.init(main_dude.world_coords, context.hovered_coords.toWorld(), 1); + std.debug.print("{}\n", .{main_dude.animation}); + main_dude.target_coords = context.hovered_coords; + } } const wm = rl.GetMouseWheelMove(); |
