Implement gravity

This commit is contained in:
Dusk 2022-07-21 14:02:22 +02:00
parent 27270e1832
commit 65c1f5eb34
2 changed files with 20 additions and 0 deletions

View File

@ -51,6 +51,7 @@ timer_left_arr: Timer,
timer_left_das: Timer,
timer_right_arr: Timer,
timer_right_das: Timer,
timer_gravity: Timer,
pub fn init(renderer: *SDL.SDL_Renderer) Self {
var ret = Self{
@ -67,6 +68,7 @@ pub fn init(renderer: *SDL.SDL_Renderer) Self {
.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
@ -140,6 +142,19 @@ pub fn tick(self: *Self) State {
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[@enumToInt(ActionCode.down)].activate = true;
self.timer_gravity.start();
}
} else {
self.timer_gravity.stop();
}
// DROP
if (self.piece.dropped) {

View File

@ -22,6 +22,11 @@ pub fn start(self: *Self) void {
self.initial = SDL.SDL_GetTicks64();
}
pub fn unstop (self: *Self) void {
self.started = true;
}
pub fn finished(self: *Self) bool {
if (self.started) {
self.progress = SDL.SDL_GetTicks64() - self.initial;