From a248749538f95e828916d67bcdf34136ce624bd5 Mon Sep 17 00:00:00 2001 From: strangedusk Date: Thu, 6 Oct 2022 15:15:24 +0200 Subject: [PATCH] ActionList is now a struct (in Game.zig) It will be changed in Menu as well --- src/Game.zig | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/Game.zig b/src/Game.zig index f564257..3b702a3 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -14,14 +14,14 @@ const movement = @import("Game/movement.zig"); const Self = @This(); // Codes to access action list -pub const ActionCode = enum(usize) { - right = 0, - left = 1, - down = 2, - hard = 3, - swap = 4, - rot_r = 5, - rot_l = 6, +const ActionList = struct { + right: Action, + left: Action, + down: Action, + hard: Action, + swap: Action, + rot_r: Action, + rot_l: Action, }; grid: Grid, @@ -36,14 +36,14 @@ held: ?Piece.Type, piece: Piece, shadow: Piece, -action_list: [7]Action = .{ - Action.init(SDL.Scancode.d, actionRight), // Right - Action.init(SDL.Scancode.a, actionLeft), // Left - Action.init(SDL.Scancode.s, actionDown), // Down - Action.init(SDL.Scancode.w, actionHard), // Instant Drop - Action.init(SDL.Scancode.space, actionSwap), // Swap Piece - Action.init(SDL.Scancode.right, actionRotR), // Rotate - Action.init(SDL.Scancode.left, actionRotL), // Rotate +action_list: ActionList = .{ + .right = Action.init(SDL.Scancode.d, actionRight), // Right + .left = Action.init(SDL.Scancode.a, actionLeft), // Left + .down = Action.init(SDL.Scancode.s, actionDown), // Down + .hard = Action.init(SDL.Scancode.w, actionHard), // Instant Drop + .swap = Action.init(SDL.Scancode.space, actionSwap), // Swap Piece + .rot_r = Action.init(SDL.Scancode.right, actionRotR), // Rotate + .rot_l = Action.init(SDL.Scancode.left, actionRotL), // Rotate }, timer_down: Timer, @@ -91,7 +91,7 @@ pub fn tick(self: *Self) State { // Repeat Right // DAS - if (self.action_list[@enumToInt(ActionCode.right)].holding) { + if (self.action_list.right.holding) { if (!self.timer_right_das.started) { self.timer_right_das.start(); } @@ -107,14 +107,14 @@ pub fn tick(self: *Self) State { self.timer_right_arr.start(); } else if (self.timer_right_arr.finished()) { self.timer_right_arr.stop(); - self.action_list[@enumToInt(ActionCode.right)].holding = false; + self.action_list.right.holding = false; } } // Repeat Left // DAS - if (self.action_list[@enumToInt(ActionCode.left)].holding) { + if (self.action_list.left.holding) { if (!self.timer_left_das.started) { self.timer_left_das.start(); } @@ -130,17 +130,17 @@ pub fn tick(self: *Self) State { self.timer_left_arr.start(); } else if (self.timer_left_arr.finished()) { self.timer_left_arr.stop(); - self.action_list[@enumToInt(ActionCode.left)].holding = false; + self.action_list.left.holding = false; } } // Repeat Down - if (self.action_list[@enumToInt(ActionCode.down)].holding) { + if (self.action_list.down.holding) { if (!self.timer_down.started) { self.timer_down.start(); } else if (self.timer_down.finished()) { self.timer_down.stop(); - self.action_list[@enumToInt(ActionCode.down)].holding = false; + self.action_list.down.holding = false; } } else { self.timer_down.stop(); @@ -152,7 +152,7 @@ pub fn tick(self: *Self) State { if (!self.timer_gravity.started) { self.timer_gravity.start(); } else if (self.timer_gravity.finished()) { - self.action_list[@enumToInt(ActionCode.down)].activate = true; + self.action_list.down.activate = true; self.timer_gravity.start(); } } else { @@ -170,7 +170,10 @@ pub fn tick(self: *Self) State { // KEY EVENTS var key_state = SDL.getKeyboardState(); - for (self.action_list) |*action| { + 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;