make controls work with keyboard and gamepads

This commit is contained in:
Rokas Puzonas 2024-02-12 20:06:58 +02:00
parent 70256a1547
commit eb305add94
3 changed files with 95 additions and 40 deletions

View File

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

View File

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

90
src/player-input.zig Normal file
View File

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