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); }