Code "refactoring"

This commit is contained in:
Dusk 2022-07-10 18:25:52 +02:00
parent 46cab832ed
commit 516efd17e2
1 changed files with 100 additions and 87 deletions

View File

@ -4,12 +4,30 @@ const SDL = @import("sdl2");
const Grid = @import("grid.zig");
const Piece = @import("piece.zig");
const Bag = @import("bag.zig");
const Timer = @import("timer.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,
};
};
grid: Grid,
grid_cell_size: i32,
grid_pos_x: i32,
@ -23,16 +41,19 @@ shadow: Piece,
renderer: *SDL.SDL_Renderer,
right_action: bool = false,
left_action: bool = false,
down_action: bool = false,
hdrop_action: bool = false,
swap_action: bool = false,
rotate_right_action: bool = false,
rotate_left_action: bool = false,
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
},
timer_down: Timer,
pub fn init(_renderer: *SDL.SDL_Renderer) Self {
var ret = Self{
.grid = Grid.init(),
.grid_cell_size = undefined,
@ -44,111 +65,97 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
.shadow = undefined,
.held = null,
.renderer = _renderer,
.timer_down = Timer.init(33),
};
var aux_w: i32 = undefined;
var aux_h: i32 = undefined;
_ = SDL.SDL_GetRendererOutputSize(_renderer, &aux_w, &aux_h);
// Calculate positions
{
var aux_w: i32 = undefined;
var aux_h: i32 = undefined;
ret.grid_cell_size = @divFloor(@minimum(aux_w, aux_h), 32);
_ = SDL.SDL_GetRendererOutputSize(_renderer, &aux_w, &aux_h);
ret.grid_pos_x = @divFloor(aux_w, 2) - (ret.grid_cell_size * @divFloor(Grid.ncolumns, 2));
ret.grid_pos_y = @divFloor(aux_h, 2) - (ret.grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2));
ret.grid_cell_size = @divFloor(@minimum(aux_w, aux_h), 32);
ret.grid_pos_x = @divFloor(aux_w, 2) - (ret.grid_cell_size * @divFloor(Grid.ncolumns, 2));
ret.grid_pos_y = @divFloor(aux_h, 2) - (ret.grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2));
ret.piece = ret.bag.pop();
}
ret.piece = ret.bag.pop();
// ret.shadow = movement.shadow(ret.grid, ret.piece);
// ret.shadow.color.a /= 2;
return ret;
}
// fn render(self: Self) void {
// self.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
// self.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
// renderer.renderBag(self.renderer, self.bag, self.grid_cell_size, self.grid_pos_x + self.grid_cell_size * (Grid.ncolumns + 1), self.grid_pos_y);
// if (self.held) |held_piece| {
// renderer.renderHeld(self.renderer, held_piece, self.grid_cell_size, self.grid_pos_x - (self.grid_cell_size * 5), self.grid_pos_y);
// }
// }
pub fn tick(self: *Self) void {
// TIMERS
if (self.action_list[@enumToInt(Action.Offset.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;
}
} else {
self.timer_down.stop();
}
// KEY EVENTS
var key_state = SDL.SDL_GetKeyboardState(null);
// MOVEMENT
for (self.action_list) |*action| {
if (key_state[action.*.code] == 1 and !action.*.holding) {
action.*.holding = true;
action.*.activate = true;
}
if (key_state[action.*.code] == 0) {
action.*.holding = false;
}
// GO RIGHT
if (key_state[SDL.SDL_SCANCODE_D] == 1 and !self.right_action) {
self.right_action = true;
self.piece = movement.moveRight(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_D] == 0) {
self.right_action = false;
}
// GO LEFT
if (key_state[SDL.SDL_SCANCODE_A] == 1 and !self.left_action) {
self.left_action = true;
self.piece = movement.moveLeft(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_A] == 0) {
self.left_action = false;
}
// GO DOWN
if (key_state[SDL.SDL_SCANCODE_S] == 1 and !self.down_action) {
self.down_action = true;
self.piece = movement.moveDown(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_S] == 0) {
self.down_action = false;
// Action
if (action.*.activate) {
action.*.activate = false;
@call(.{}, action.*.callback, .{self});
}
}
// HARD DROP
if (key_state[SDL.SDL_SCANCODE_W] == 1 and !self.hdrop_action) {
self.hdrop_action = true;
self.piece = movement.hardDrop(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_W] == 0) {
self.hdrop_action = false;
}
// SWAP PIECES
if (key_state[SDL.SDL_SCANCODE_SPACE] == 1 and !self.swap_action) {
self.swap_action = true;
self.swapHeld();
}
if (key_state[SDL.SDL_SCANCODE_SPACE] == 0) {
self.swap_action = false;
}
// ROTATE
if (key_state[SDL.SDL_SCANCODE_RIGHT] == 1 and !self.rotate_right_action) {
self.rotate_right_action = true;
self.piece = movement.rotateRight(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_RIGHT] == 0) {
self.rotate_right_action = false;
}
if (key_state[SDL.SDL_SCANCODE_LEFT] == 1 and !self.rotate_left_action) {
self.rotate_left_action = true;
self.piece = movement.rotateLeft(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_LEFT] == 0) {
self.rotate_left_action = false;
}
// DROP
if (self.piece.dropped) {
self.grid = movement.drop(self.grid, self.piece);
// New Piece
self.piece = self.bag.pop();
}
// Update Shadow
self.shadow = movement.shadow(self.grid, self.piece);
self.shadow.color.a /= 4;
{
self.shadow = movement.shadow(self.grid, self.piece);
self.shadow.color.a /= 4;
self.grid.clearLines();
self.grid.clearLines();
}
renderer.render(self.*);
}
fn swapHeld(self: *Self) void {
// ACTION CALLBACKS
fn actionDown(self: *Self) void {
self.piece = movement.moveDown(self.grid, self.piece);
}
fn actionRight(self: *Self) void {
self.piece = movement.moveRight(self.grid, self.piece);
}
fn actionLeft(self: *Self) void {
self.piece = movement.moveLeft(self.grid, self.piece);
}
fn actionHard(self: *Self) void {
self.piece = movement.hardDrop(self.grid, self.piece);
}
fn actionSwap(self: *Self) void {
if (self.held) |held_piece| {
if (!self.piece.swapped) {
self.held = self.piece.piece_type;
@ -161,3 +168,9 @@ fn swapHeld(self: *Self) void {
self.piece.swapped = true;
}
}
fn actionRotR(self: *Self) void {
self.piece = movement.rotateRight(self.grid, self.piece);
}
fn actionRotL(self: *Self) void {
self.piece = movement.rotateLeft(self.grid, self.piece);
}