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/root.zig | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'src/root.zig') diff --git a/src/root.zig b/src/root.zig index 286c0c2..8312f5d 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,8 +1,11 @@ const std = @import("std"); const rl = @cImport(@cInclude("raylib.h")); const context = @import("context.zig"); +const Animation = @import("Animation.zig"); -const grid_size: usize = 21; +const camera_move_speed: f32 = 10.0; + +const grid_size: usize = 45; const grid_central_row = grid_size / 2 + 1; pub const MyRect = struct { @@ -10,6 +13,8 @@ pub const MyRect = struct { color: rl.Color, }; +const dude_radius: f32 = 50.0; +var dude_rotation: f32 = 45.0; const hex_radius: f32 = 100.0; const hex_rotation = 30.0; @@ -24,6 +29,10 @@ pub const HexCoord = struct { return self.r / grid_size + self.q % grid_size; } + pub inline fn toWorld(self: HexCoord) rl.Vector2 { + return qrToWorld(self.q, self.r); + } + pub inline fn qrToWorld(q: i32, r: i32) rl.Vector2 { return .{ .x = hex_radius * (@sqrt(3.0) * @as(f32, @floatFromInt(q)) + @sqrt(3.0) / 2.0 * @as(f32, @floatFromInt(r))), @@ -73,6 +82,13 @@ pub fn setup() !void { const target = HexCoord.qrToWorld(grid_central_row, grid_central_row); context.camera = rl.Camera2D{ .target = target, .offset = rl.Vector2{ .x = 0, .y = 0 }, .rotation = 0, .zoom = 1 }; context.hex_grid = try context.Grid.init(context.gpa, grid_size); + context.main_dude = .{ + .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(), + }; + 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 for (0..grid_size) |_| { @@ -86,33 +102,47 @@ pub fn setup() !void { } pub fn update() !void { - //---------------------------------------------------------------------------------- - const mouse_pos = rl.GetMousePosition(); const mouse_world_pos = rl.GetScreenToWorld2D(mouse_pos, context.camera); + var main_dude = &context.main_dude; context.hovered_coords = HexCoord.worldToQr(mouse_world_pos); const zoom_scale = context.camera.zoom; if (rl.IsKeyDown(rl.KEY_D)) { - context.camera.target.x += 2 / zoom_scale; + context.camera.target.x += camera_move_speed / zoom_scale; } if (rl.IsKeyDown(rl.KEY_A)) { - context.camera.target.x -= 2 / zoom_scale; + context.camera.target.x -= camera_move_speed / zoom_scale; } if (rl.IsKeyDown(rl.KEY_W)) { - context.camera.target.y -= 2 / zoom_scale; + context.camera.target.y -= camera_move_speed / zoom_scale; } if (rl.IsKeyDown(rl.KEY_S)) { - context.camera.target.y += 2 / zoom_scale; + context.camera.target.y += camera_move_speed / zoom_scale; } - if (rl.IsMouseButtonDown(rl.MOUSE_BUTTON_MIDDLE)) { + // 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)) { const delta = rl.GetMouseDelta(); context.camera.target.x -= delta.x / zoom_scale; context.camera.target.y -= delta.y / zoom_scale; } + if (main_dude.animation.active) { + main_dude.world_coords = main_dude.animation.next(); + main_dude.hex_coords = main_dude.target_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; + } + const wm = rl.GetMouseWheelMove(); if (wm != 0.0) { context.camera.zoom += wm * 0.05; @@ -150,9 +180,12 @@ pub fn draw() !void { } } - const center = HexCoord.qrToWorld(@intCast(context.hovered_coords.q), @intCast(context.hovered_coords.r)); + const center = context.hovered_coords.toWorld(); rl.DrawPolyLines(center, 6, hex_radius, hex_rotation, rl.WHITE); + const dude_center = context.main_dude.world_coords; + rl.DrawPoly(dude_center, 4, dude_radius, dude_rotation, rl.RED); + rl.EndMode2D(); rl.EndDrawing(); -- cgit v1.2.3-70-g09d2