251 lines
6.3 KiB
Zig
251 lines
6.3 KiB
Zig
const std = @import("std");
|
|
const SDL = @import("sdl2");
|
|
|
|
const Grid = @import("Game/Grid.zig");
|
|
const Piece = @import("Game/Piece.zig");
|
|
const Bag = @import("Game/Bag.zig");
|
|
const Timer = @import("Timer.zig");
|
|
const State = @import("flow.zig").State;
|
|
const Action = @import("Action.zig");
|
|
|
|
const Renderer = @import("Game/Renderer.zig");
|
|
const movement = @import("Game/movement.zig");
|
|
|
|
const Self = @This();
|
|
|
|
// Codes to access action list
|
|
const ActionList = struct {
|
|
right: Action,
|
|
left: Action,
|
|
down: Action,
|
|
hard: Action,
|
|
swap: Action,
|
|
rot_r: Action,
|
|
rot_l: Action,
|
|
};
|
|
|
|
grid: Grid,
|
|
|
|
renderer: Renderer,
|
|
|
|
state: State = State.game,
|
|
|
|
bag: Bag,
|
|
|
|
held: ?Piece.Type,
|
|
piece: Piece,
|
|
shadow: Piece,
|
|
|
|
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,
|
|
timer_left_arr: Timer,
|
|
timer_left_das: Timer,
|
|
timer_right_arr: Timer,
|
|
timer_right_das: Timer,
|
|
timer_gravity: Timer,
|
|
|
|
needs_reinit: bool = false,
|
|
|
|
pub fn init(renderer: *Renderer.Renderer) Self {
|
|
var ret = Self{
|
|
.grid = Grid.init(),
|
|
|
|
.bag = Bag.init(),
|
|
.piece = undefined,
|
|
.shadow = undefined,
|
|
.held = null,
|
|
.renderer = Renderer.init(renderer),
|
|
|
|
.timer_down = Timer.init(50),
|
|
.timer_left_arr = Timer.init(50),
|
|
.timer_left_das = Timer.init(200),
|
|
.timer_right_arr = Timer.init(50),
|
|
.timer_right_das = Timer.init(200),
|
|
.timer_gravity = Timer.init(200),
|
|
};
|
|
|
|
// Get a piece from the bag
|
|
ret.piece = ret.bag.pop();
|
|
|
|
return ret;
|
|
}
|
|
|
|
// Main Game loop
|
|
pub fn tick(self: *Self) State {
|
|
|
|
// TIMERS
|
|
// Dropping a piece
|
|
if (self.piece.timer_dropped.finished()) {
|
|
self.piece.timer_dropped.stop();
|
|
self.piece.dropped = true;
|
|
}
|
|
// Repeat Right
|
|
|
|
// DAS
|
|
if (self.action_list.right.holding) {
|
|
if (!self.timer_right_das.started) {
|
|
self.timer_right_das.start();
|
|
}
|
|
} else {
|
|
// Stop both
|
|
self.timer_right_das.stop();
|
|
self.timer_right_arr.stop();
|
|
}
|
|
|
|
// ARR
|
|
if (self.timer_right_das.finished()) {
|
|
if (!self.timer_right_arr.started) {
|
|
self.timer_right_arr.start();
|
|
} else if (self.timer_right_arr.finished()) {
|
|
self.timer_right_arr.stop();
|
|
self.action_list.right.holding = false;
|
|
}
|
|
}
|
|
|
|
// Repeat Left
|
|
|
|
// DAS
|
|
if (self.action_list.left.holding) {
|
|
if (!self.timer_left_das.started) {
|
|
self.timer_left_das.start();
|
|
}
|
|
} else {
|
|
// Stop both
|
|
self.timer_left_das.stop();
|
|
self.timer_left_arr.stop();
|
|
}
|
|
|
|
// ARR
|
|
if (self.timer_left_das.finished()) {
|
|
if (!self.timer_left_arr.started) {
|
|
self.timer_left_arr.start();
|
|
} else if (self.timer_left_arr.finished()) {
|
|
self.timer_left_arr.stop();
|
|
self.action_list.left.holding = false;
|
|
}
|
|
}
|
|
|
|
// Repeat Down
|
|
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.down.holding = false;
|
|
}
|
|
} else {
|
|
self.timer_down.stop();
|
|
}
|
|
|
|
// Gravity
|
|
|
|
if (!self.piece.timer_dropped.started) {
|
|
if (!self.timer_gravity.started) {
|
|
self.timer_gravity.start();
|
|
} else if (self.timer_gravity.finished()) {
|
|
self.action_list.down.activate = true;
|
|
self.timer_gravity.start();
|
|
}
|
|
} else {
|
|
self.timer_gravity.stop();
|
|
}
|
|
|
|
// DROP
|
|
|
|
if (self.piece.dropped) {
|
|
self.grid = movement.drop(self.grid, self.piece);
|
|
// New Piece
|
|
self.piece = self.bag.pop();
|
|
}
|
|
|
|
// KEY EVENTS
|
|
var key_state = SDL.getKeyboardState();
|
|
|
|
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;
|
|
}
|
|
if (!key_state.isPressed(action.*.scancode)) {
|
|
action.*.holding = false;
|
|
}
|
|
|
|
// Action
|
|
if (action.*.activate) {
|
|
action.*.activate = false;
|
|
action.*.call(self);
|
|
}
|
|
}
|
|
|
|
// Update Shadow
|
|
{
|
|
self.shadow = movement.shadow(self.grid, self.piece);
|
|
self.shadow.color.a /= 4;
|
|
}
|
|
|
|
// Check for a top out
|
|
{
|
|
if (movement.isToppedOut(self.grid)) {
|
|
std.debug.print("Topped Out!\n", .{});
|
|
// Return to main menu
|
|
self.state = State.main_menu;
|
|
// Flag that I need a reset
|
|
self.needs_reinit = true;
|
|
}
|
|
}
|
|
|
|
self.grid.clearLines();
|
|
|
|
self.renderer.render(self.*);
|
|
|
|
return self.state;
|
|
}
|
|
|
|
// 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;
|
|
self.piece = Piece.init(held_piece);
|
|
self.piece.swapped = true;
|
|
}
|
|
} else {
|
|
self.held = self.piece.piece_type;
|
|
self.piece = self.bag.pop();
|
|
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);
|
|
}
|