From eb305add947f00c28564a45eed2d5a815c931183 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 12 Feb 2024 20:06:58 +0200 Subject: [PATCH] make controls work with keyboard and gamepads --- build.zig | 4 +- src/main-scene.zig | 41 ++------------------ src/player-input.zig | 90 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 40 deletions(-) create mode 100644 src/player-input.zig diff --git a/build.zig b/build.zig index ee555da..2ff3ef0 100644 --- a/build.zig +++ b/build.zig @@ -12,11 +12,11 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - raylib.addTo(b, exe, target, optimize, .{}); - const git_submodule_run = b.addSystemCommand(&.{"git", "submodule", "update", "--init", "--recursive"}); exe.step.dependOn(&git_submodule_run.step); + raylib.addTo(b, exe, target, optimize, .{}); + b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/src/main-scene.zig b/src/main-scene.zig index fecbc88..f5a371e 100644 --- a/src/main-scene.zig +++ b/src/main-scene.zig @@ -2,6 +2,7 @@ const rl = @import("raylib"); const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; +const PlayerInput = @import("player-input.zig"); const friction = 0.99; const walkForce = 4000; @@ -151,7 +152,6 @@ pub fn tick(self: *Self) !void { const screenWidth: f32 = @floatFromInt(rl.GetScreenWidth()); const screenHeight: f32 = @floatFromInt(rl.GetScreenHeight()); - const mouseDelta = rl.GetMouseDelta(); self.camera.offset.x = screenWidth/2; self.camera.offset.y = screenHeight/2; @@ -173,44 +173,9 @@ pub fn tick(self: *Self) !void { }); } - var moveDx: f32 = 0; - var moveDy: f32 = 0; - if (rl.IsKeyDown(rl.KeyboardKey.KEY_W)) { - moveDy -= 1; - } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_S)) { - moveDy += 1; - } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) { - moveDx -= 1; - } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_D)) { - moveDx += 1; - } - - var handDx: f32 = mouseDelta.x; - var handDy: f32 = mouseDelta.y; - - var gamepad: i32 = 0; - if (rl.IsGamepadAvailable(gamepad)) { - moveDx += rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_LEFT_X); - moveDy += rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_LEFT_Y); - - handDx += rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_RIGHT_X); - handDy += rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_RIGHT_Y); - } - - var moveDir = rl.Vector2{ .x = moveDx, .y = moveDy }; - moveDir = rl.Vector2Normalize(moveDir); - if (moveDir.x != 0 or moveDir.y != 0) { - player.handDirection = moveDir; - } - if (handDx != 0 or handDy != 0) { - player.handDirection.x = handDx; - player.handDirection.y = handDy; - player.handDirection = player.handDirection.normalize(); - } + player.handDirection = PlayerInput.getHandPosition(); + const moveDir = PlayerInput.getWalkDirection(); player.acceleration = moveDir; player.acceleration = rl.Vector2Scale(player.acceleration, walkForce); diff --git a/src/player-input.zig b/src/player-input.zig new file mode 100644 index 0000000..ec848bd --- /dev/null +++ b/src/player-input.zig @@ -0,0 +1,90 @@ +const rl = @import("raylib"); +const std = @import("std"); + +const gamepad: i32 = 0; + +var mouseHandPosition: rl.Vector2 = .{ .x = 0, .y = 0 }; +const mouseHandRadius = 100.0; + +fn clampVector(vec: rl.Vector2) rl.Vector2 { + if (vec.length2() > 1) { + return vec.normalize(); + } + + return vec; +} + +fn getKeyboardWalkDirection() rl.Vector2 { + var dx: f32 = 0; + var dy: f32 = 0; + if (rl.IsKeyDown(rl.KeyboardKey.KEY_W)) { + dy -= 1; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_S)) { + dy += 1; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) { + dx -= 1; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_D)) { + dx += 1; + } + + return rl.Vector2{ .x = dx, .y = dy }; +} + +fn getGamepadWalkDirection() ?rl.Vector2 { + if (!rl.IsGamepadAvailable(gamepad)) { + return null; + } + + const x = rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_LEFT_X); + const y = rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_LEFT_Y); + + if (@fabs(x) < 0.001 and @fabs(y) < 0.001) { + return null; + } + + return clampVector(.{ .x = x, .y = y }); +} + +pub fn getWalkDirection() rl.Vector2 { + var walkDirection = getKeyboardWalkDirection().normalize(); + if (getGamepadWalkDirection()) |dir| { + walkDirection = dir; + } + + return walkDirection; +} + +fn getGamepadHandPosition() ?rl.Vector2 { + if (!rl.IsGamepadAvailable(gamepad)) { + return null; + } + + const x = rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_RIGHT_X); + const y = rl.GetGamepadAxisMovement(gamepad, .GAMEPAD_AXIS_RIGHT_Y); + + if (@fabs(x) < 0.001 and @fabs(y) < 0.001) { + return null; + } + + return clampVector(.{ .x = x, .y = y }); +} + +fn getMouseHandPosition() rl.Vector2 { + const mouseDelta = rl.GetMouseDelta(); + + mouseHandPosition = clampVector(mouseHandPosition.add(mouseDelta.scale(1.0/mouseHandRadius))); + + return mouseHandPosition; +} + +pub fn getHandPosition() rl.Vector2 { + var handPosition = getMouseHandPosition(); + if (getGamepadHandPosition()) |pos| { + handPosition = pos; + } + + return clampVector(handPosition); +}