Make Action struct agnostic

This commit is contained in:
Dusk 2022-07-20 19:31:18 +02:00
parent cb171b7cf8
commit d8e86e24d4
2 changed files with 41 additions and 28 deletions

17
src/Action.zig Normal file
View File

@ -0,0 +1,17 @@
const Game = @import("Game.zig");
const Self = @This();
activate: bool = false,
holding: bool = false,
code: u8, // SDL Keycode
callback: *const anyopaque,
pub fn init(code: u8, callback: *const anyopaque) Self {
return Self{
.code = code,
.callback = callback,
};
}
pub fn call(self: Self, state: anytype) void {
@call(.{}, @ptrCast(fn (@TypeOf(state)) void, self.callback), .{state});
}

View File

@ -6,26 +6,22 @@ const Piece = @import("Piece.zig");
const Bag = @import("Bag.zig"); const Bag = @import("Bag.zig");
const Timer = @import("Timer.zig"); const Timer = @import("Timer.zig");
const State = @import("flow.zig").State; const State = @import("flow.zig").State;
const Action = @import("Action.zig");
const renderer = @import("renderer.zig"); const renderer = @import("renderer.zig");
const movement = @import("movement.zig"); const movement = @import("movement.zig");
const Self = @This(); const Self = @This();
const Action = struct { // Codes to access action list
activate: bool = false, pub const ActionCode = enum(usize) {
holding: bool = false, right = 0,
code: u8, left = 1,
callback: fn (*Self) void, down = 2,
pub const Offset = enum(usize) { hard = 3,
right = 0, swap = 4,
left = 1, rot_r = 5,
down = 2, rot_l = 6,
hard = 3,
swap = 4,
rot_r = 5,
rot_l = 6,
};
}; };
grid: Grid, grid: Grid,
@ -44,13 +40,13 @@ shadow: Piece,
renderer: *SDL.SDL_Renderer, renderer: *SDL.SDL_Renderer,
action_list: [7]Action = .{ action_list: [7]Action = .{
.{ .code = SDL.SDL_SCANCODE_D, .callback = actionRight }, // Right Action.init(SDL.SDL_SCANCODE_D, actionRight), // Right
.{ .code = SDL.SDL_SCANCODE_A, .callback = actionLeft }, // Left Action.init(SDL.SDL_SCANCODE_A, actionLeft), // Left
.{ .code = SDL.SDL_SCANCODE_S, .callback = actionDown }, // Down Action.init(SDL.SDL_SCANCODE_S, actionDown), // Down
.{ .code = SDL.SDL_SCANCODE_W, .callback = actionHard }, // Instant Drop Action.init(SDL.SDL_SCANCODE_W, actionHard), // Instant Drop
.{ .code = SDL.SDL_SCANCODE_SPACE, .callback = actionSwap }, // Swap Piece Action.init(SDL.SDL_SCANCODE_SPACE, actionSwap), // Swap Piece
.{ .code = SDL.SDL_SCANCODE_RIGHT, .callback = actionRotR }, // Rotate Action.init(SDL.SDL_SCANCODE_RIGHT, actionRotR), // Rotate
.{ .code = SDL.SDL_SCANCODE_LEFT, .callback = actionRotL }, // Rotate Action.init(SDL.SDL_SCANCODE_LEFT, actionRotL), // Rotate
}, },
timer_down: Timer, timer_down: Timer,
@ -108,7 +104,7 @@ pub fn tick(self: *Self) State {
// Repeat Right // Repeat Right
// DAS // DAS
if (self.action_list[@enumToInt(Action.Offset.right)].holding) { if (self.action_list[@enumToInt(ActionCode.right)].holding) {
if (!self.timer_right_das.started) { if (!self.timer_right_das.started) {
self.timer_right_das.start(); self.timer_right_das.start();
} }
@ -124,14 +120,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(Action.Offset.right)].holding = false; self.action_list[@enumToInt(ActionCode.right)].holding = false;
} }
} }
// Repeat Left // Repeat Left
// DAS // DAS
if (self.action_list[@enumToInt(Action.Offset.left)].holding) { if (self.action_list[@enumToInt(ActionCode.left)].holding) {
if (!self.timer_left_das.started) { if (!self.timer_left_das.started) {
self.timer_left_das.start(); self.timer_left_das.start();
} }
@ -147,17 +143,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(Action.Offset.left)].holding = false; self.action_list[@enumToInt(ActionCode.left)].holding = false;
} }
} }
// Repeat Down // Repeat Down
if (self.action_list[@enumToInt(Action.Offset.down)].holding) { if (self.action_list[@enumToInt(ActionCode.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(Action.Offset.down)].holding = false; self.action_list[@enumToInt(ActionCode.down)].holding = false;
} }
} else { } else {
self.timer_down.stop(); self.timer_down.stop();
@ -186,7 +182,7 @@ pub fn tick(self: *Self) State {
// Action // Action
if (action.*.activate) { if (action.*.activate) {
action.*.activate = false; action.*.activate = false;
@call(.{}, action.*.callback, .{self}); action.*.call(self);
} }
} }