ActionList is now a struct (in Game.zig)

It will be changed in Menu as well
This commit is contained in:
Dusk 2022-10-06 15:15:24 +02:00
parent a3b1e5171e
commit a248749538
1 changed files with 27 additions and 24 deletions

View File

@ -14,14 +14,14 @@ const movement = @import("Game/movement.zig");
const Self = @This(); const Self = @This();
// Codes to access action list // Codes to access action list
pub const ActionCode = enum(usize) { const ActionList = struct {
right = 0, right: Action,
left = 1, left: Action,
down = 2, down: Action,
hard = 3, hard: Action,
swap = 4, swap: Action,
rot_r = 5, rot_r: Action,
rot_l = 6, rot_l: Action,
}; };
grid: Grid, grid: Grid,
@ -36,14 +36,14 @@ held: ?Piece.Type,
piece: Piece, piece: Piece,
shadow: Piece, shadow: Piece,
action_list: [7]Action = .{ action_list: ActionList = .{
Action.init(SDL.Scancode.d, actionRight), // Right .right = Action.init(SDL.Scancode.d, actionRight), // Right
Action.init(SDL.Scancode.a, actionLeft), // Left .left = Action.init(SDL.Scancode.a, actionLeft), // Left
Action.init(SDL.Scancode.s, actionDown), // Down .down = Action.init(SDL.Scancode.s, actionDown), // Down
Action.init(SDL.Scancode.w, actionHard), // Instant Drop .hard = Action.init(SDL.Scancode.w, actionHard), // Instant Drop
Action.init(SDL.Scancode.space, actionSwap), // Swap Piece .swap = Action.init(SDL.Scancode.space, actionSwap), // Swap Piece
Action.init(SDL.Scancode.right, actionRotR), // Rotate .rot_r = Action.init(SDL.Scancode.right, actionRotR), // Rotate
Action.init(SDL.Scancode.left, actionRotL), // Rotate .rot_l = Action.init(SDL.Scancode.left, actionRotL), // Rotate
}, },
timer_down: Timer, timer_down: Timer,
@ -91,7 +91,7 @@ pub fn tick(self: *Self) State {
// Repeat Right // Repeat Right
// DAS // DAS
if (self.action_list[@enumToInt(ActionCode.right)].holding) { if (self.action_list.right.holding) {
if (!self.timer_right_das.started) { if (!self.timer_right_das.started) {
self.timer_right_das.start(); self.timer_right_das.start();
} }
@ -107,14 +107,14 @@ pub fn tick(self: *Self) State {
self.timer_right_arr.start(); self.timer_right_arr.start();
} else if (self.timer_right_arr.finished()) { } else if (self.timer_right_arr.finished()) {
self.timer_right_arr.stop(); self.timer_right_arr.stop();
self.action_list[@enumToInt(ActionCode.right)].holding = false; self.action_list.right.holding = false;
} }
} }
// Repeat Left // Repeat Left
// DAS // DAS
if (self.action_list[@enumToInt(ActionCode.left)].holding) { if (self.action_list.left.holding) {
if (!self.timer_left_das.started) { if (!self.timer_left_das.started) {
self.timer_left_das.start(); self.timer_left_das.start();
} }
@ -130,17 +130,17 @@ pub fn tick(self: *Self) State {
self.timer_left_arr.start(); self.timer_left_arr.start();
} else if (self.timer_left_arr.finished()) { } else if (self.timer_left_arr.finished()) {
self.timer_left_arr.stop(); self.timer_left_arr.stop();
self.action_list[@enumToInt(ActionCode.left)].holding = false; self.action_list.left.holding = false;
} }
} }
// Repeat Down // Repeat Down
if (self.action_list[@enumToInt(ActionCode.down)].holding) { if (self.action_list.down.holding) {
if (!self.timer_down.started) { if (!self.timer_down.started) {
self.timer_down.start(); self.timer_down.start();
} else if (self.timer_down.finished()) { } else if (self.timer_down.finished()) {
self.timer_down.stop(); self.timer_down.stop();
self.action_list[@enumToInt(ActionCode.down)].holding = false; self.action_list.down.holding = false;
} }
} else { } else {
self.timer_down.stop(); self.timer_down.stop();
@ -152,7 +152,7 @@ pub fn tick(self: *Self) State {
if (!self.timer_gravity.started) { if (!self.timer_gravity.started) {
self.timer_gravity.start(); self.timer_gravity.start();
} else if (self.timer_gravity.finished()) { } else if (self.timer_gravity.finished()) {
self.action_list[@enumToInt(ActionCode.down)].activate = true; self.action_list.down.activate = true;
self.timer_gravity.start(); self.timer_gravity.start();
} }
} else { } else {
@ -170,7 +170,10 @@ pub fn tick(self: *Self) State {
// KEY EVENTS // KEY EVENTS
var key_state = SDL.getKeyboardState(); 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) { if (key_state.isPressed(action.*.scancode) and !action.*.holding) {
action.*.holding = true; action.*.holding = true;
action.*.activate = true; action.*.activate = true;