Reimplement game actions

This commit is contained in:
Dusk 2023-09-22 20:15:13 +02:00
parent a86fda392d
commit 4f3599fa42
3 changed files with 34 additions and 27 deletions

View File

@ -37,13 +37,13 @@ piece: Piece,
shadow: Piece, shadow: Piece,
action_list: ActionList = .{ action_list: ActionList = .{
.right = Action.init(input.menu_left, actionRight), // Right .right = Action.init(input.game_right, actionRight), // Right
.left = Action.init(input.menu_left, actionLeft), // Left .left = Action.init(input.game_left, actionLeft), // Left
.down = Action.init(input.menu_left, actionDown), // Down .down = Action.init(input.game_down, actionDown), // Down
.hard = Action.init(input.menu_left, actionHard), // Instant Drop .hard = Action.init(input.game_drop, actionHard), // Instant Drop
.swap = Action.init(input.menu_left, actionSwap), // Swap Piece .swap = Action.init(input.game_swap_piece, actionSwap), // Swap Piece
.rot_r = Action.init(input.menu_left, actionRotR), // Rotate .rot_r = Action.init(input.game_rotate_r, actionRotR), // Rotate
.rot_l = Action.init(input.menu_left, actionRotL), // Rotate .rot_l = Action.init(input.game_rotate_l, actionRotL), // Rotate
}, },
timer_down: Timer, timer_down: Timer,
@ -168,26 +168,25 @@ pub fn tick(self: *Self) State {
} }
// KEY EVENTS // 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); if (action.input.isDown() and !action.*.holding) {
// inline for (action_fields) |field| { action.*.holding = true;
// std.debug.print("\n{any}", .{field.name}); action.*.activate = true;
// const action = &@field(self.action_list, field.name); }
// if (key_state.isPressed(action.*.scancode) and !action.*.holding) { if (!action.input.isDown()) {
// action.*.holding = true; action.*.holding = false;
// action.*.activate = true; }
// }
// if (!key_state.isPressed(action.*.scancode)) {
// action.*.holding = false;
// }
// // Action // Action
// if (action.*.activate) { if (action.*.activate) {
// action.*.activate = false; action.*.activate = false;
// action.*.call(self); action.*.call(self);
// } }
// } }
// Update Shadow // Update Shadow
{ {

View File

@ -69,7 +69,7 @@ pub fn drop(grid: Grid, piece: Piece) Grid {
for (piece.structure[y], 0..) |_, x| { for (piece.structure[y], 0..) |_, x| {
if (piece.structure[y][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))))].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;
} }
} }
} }

View File

@ -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_right = init("Menu Right", "RIGHT");
pub const menu_left = init("Menu Left", "LEFT");
pub const menu_down = init("Menu Down", "DOWN"); pub const menu_down = init("Menu Down", "DOWN");
pub const menu_up = init("Menu Up", "UP"); pub const menu_up = init("Menu Up", "UP");
pub const menu_accept = init("Menu Accept", "ENTER"); pub const menu_accept = init("Menu Accept", "ENTER");
pub const menu_cancel = init("Menu Cancel", "BACKSPACE"); 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");