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