From 4f3599fa42bd82e38d74c3b6555c9b0c25bbe5fc Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 22 Sep 2023 20:15:13 +0200 Subject: [PATCH] Reimplement game actions --- src/Game.zig | 49 +++++++++++++++++++++---------------------- src/Game/movement.zig | 2 +- src/input.zig | 10 ++++++++- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/Game.zig b/src/Game.zig index dfacf2f..722700c 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -37,13 +37,13 @@ piece: Piece, shadow: Piece, action_list: ActionList = .{ - .right = Action.init(input.menu_left, actionRight), // Right - .left = Action.init(input.menu_left, actionLeft), // Left - .down = Action.init(input.menu_left, actionDown), // Down - .hard = Action.init(input.menu_left, actionHard), // Instant Drop - .swap = Action.init(input.menu_left, actionSwap), // Swap Piece - .rot_r = Action.init(input.menu_left, actionRotR), // Rotate - .rot_l = Action.init(input.menu_left, actionRotL), // Rotate + .right = Action.init(input.game_right, actionRight), // Right + .left = Action.init(input.game_left, actionLeft), // Left + .down = Action.init(input.game_down, actionDown), // Down + .hard = Action.init(input.game_drop, actionHard), // Instant Drop + .swap = Action.init(input.game_swap_piece, actionSwap), // Swap Piece + .rot_r = Action.init(input.game_rotate_r, actionRotR), // Rotate + .rot_l = Action.init(input.game_rotate_l, actionRotL), // Rotate }, timer_down: Timer, @@ -168,26 +168,25 @@ pub fn tick(self: *Self) State { } // KEY EVENTS - // var key_state = SDL.getKeyboardState(); + const action_fields = std.meta.fields(ActionList); + inline for (action_fields) |field| { + // REVIEW: Is this necessary? + const action = &@field(self.action_list, field.name); - // const action_fields = std.meta.fields(ActionList); - // inline for (action_fields) |field| { - // std.debug.print("\n{any}", .{field.name}); - // const action = &@field(self.action_list, field.name); - // if (key_state.isPressed(action.*.scancode) and !action.*.holding) { - // action.*.holding = true; - // action.*.activate = true; - // } - // if (!key_state.isPressed(action.*.scancode)) { - // action.*.holding = false; - // } + if (action.input.isDown() and !action.*.holding) { + action.*.holding = true; + action.*.activate = true; + } + if (!action.input.isDown()) { + action.*.holding = false; + } - // // Action - // if (action.*.activate) { - // action.*.activate = false; - // action.*.call(self); - // } - // } + // Action + if (action.*.activate) { + action.*.activate = false; + action.*.call(self); + } + } // Update Shadow { diff --git a/src/Game/movement.zig b/src/Game/movement.zig index d9d21f5..5da4715 100644 --- a/src/Game/movement.zig +++ b/src/Game/movement.zig @@ -69,7 +69,7 @@ pub fn drop(grid: Grid, piece: Piece) Grid { for (piece.structure[y], 0..) |_, x| { if (piece.structure[y][x]) { new_grid.cells[@as(usize, @intCast(piece.row + @as(i32, @intCast(y))))][@as(usize, @intCast(piece.col + @as(i32, @intCast(x))))].free = false; - //new_grid.cells[@as(usize, @intCast(piece.row + @as(i32, @intCast(y))))][@as(usize, @intCast(piece.col + @as(i32, @intCast(x))))].color = piece.color; + new_grid.cells[@as(usize, @intCast(piece.row + @as(i32, @intCast(y))))][@as(usize, @intCast(piece.col + @as(i32, @intCast(x))))].color = piece.color; } } } diff --git a/src/input.zig b/src/input.zig index 972a252..ca8a7ec 100644 --- a/src/input.zig +++ b/src/input.zig @@ -24,9 +24,17 @@ fn init(comptime name: []const u8, comptime key_code: []const u8) Input { }; } -pub const menu_left = init("Menu Left", "LEFT"); pub const menu_right = init("Menu Right", "RIGHT"); +pub const menu_left = init("Menu Left", "LEFT"); pub const menu_down = init("Menu Down", "DOWN"); pub const menu_up = init("Menu Up", "UP"); pub const menu_accept = init("Menu Accept", "ENTER"); pub const menu_cancel = init("Menu Cancel", "BACKSPACE"); + +pub const game_right = init("Move Right", "D"); +pub const game_left = init("Move Left", "A"); +pub const game_down = init("Move Down", "S"); +pub const game_drop = init("Drop Piece", "W"); +pub const game_swap_piece = init("Swap Piece", "SPACE"); +pub const game_rotate_r = init("Rotate Piece Clockwise", "RIGHT"); +pub const game_rotate_l = init("Rotate Piece Counterclockwise", "LEFT");