summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-03-19 19:06:32 -0400
committerAndrew Opalach <andrew@akon.city> 2019-03-19 19:06:32 -0400
commit625b2cd4f1b6d923add27ba749f2faa276eadd1a (patch)
tree327ab886188716554b4f7dd34be2b2dfe876b284 /src
parent7649bf08fe950563a6ac4c7816f3bf187779c38a (diff)
downloadcetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.tar.gz
cetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.tar.bz2
cetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.zip
change movement again
Diffstat (limited to 'src')
-rw-r--r--src/cetris.c76
-rw-r--r--src/cetris.h18
-rw-r--r--src/input.c63
-rw-r--r--src/matrix.c23
-rw-r--r--src/matrix.h4
5 files changed, 74 insertions, 110 deletions
diff --git a/src/cetris.c b/src/cetris.c
index 419fdaf..e6bbc8d 100644
--- a/src/cetris.c
+++ b/src/cetris.c
@@ -42,11 +42,11 @@ void init_game(struct cetris_game* g) {
g->current_index = 0;
- g->held_move = 0;
- g->prev_move = 0;
- g->next_move_tick = 0;
- g->can_rotate = 0;
- g->can_hard_drop = 0;
+ memset(g->held_moves, 0, sizeof(uint8_t) * 7);
+ g->das_repeat = 0;
+ g->prev_das_move = 0;
+ g->das_move_tick = 0;
+ g->down_move_tick = 0;
g->lines = 0;
g->level = 1;
@@ -135,9 +135,7 @@ void update_game_tick(struct cetris_game* g) {
did_move = 1;
}
- if (did_move) {
- wipe_board(g);
- }
+ if (did_move) wipe_board(g);
g->tick++;
}
@@ -233,44 +231,32 @@ void wipe_board(struct cetris_game* g) {
/* MOVEMENT FUNCTIONS */
-void move_down(struct cetris_game* g) {
- if (g->held_move != DOWN) {
- move_current(g, basic_movements[DOWN]);
- }
- g->held_move = DOWN;
-}
-
-void move_right(struct cetris_game* g) {
- if (g->held_move != RIGHT) {
- move_current(g, basic_movements[RIGHT]);
- }
- g->held_move = RIGHT;
-}
-
-void move_left(struct cetris_game* g) {
- if (g->held_move != LEFT) {
- move_current(g, basic_movements[LEFT]);
- }
- g->held_move = LEFT;
-}
-
-void move_hard_drop(struct cetris_game* g) {
- if (g->held_move != HARD_DROP) {
- hard_drop(g);
- }
- g->held_move = HARD_DROP;
-}
-
-void rotate_clockwise(struct cetris_game* g) {
- if (g->held_move != ROTATE_CW) {
- rotate_matrix(g, 1);
+void move_peice(struct cetris_game* g, input_t move) {
+ if (!g->held_moves[move]) {
+ switch (move) {
+ case LEFT:
+ case RIGHT:
+ case DOWN:
+ move_current(g, basic_movements[move]);
+ break;
+ case HARD_DROP:
+ hard_drop(g);
+ break;
+ case ROTATE_CW:
+ rotate_matrix(g, 1);
+ break;
+ case ROTATE_CCW:
+ rotate_matrix(g, 0);
+ break;
+ }
}
- g->held_move = ROTATE_CW;
+ g->held_moves[move] = 1;
}
-void rotate_counterclockwise(struct cetris_game* g) {
- if (g->held_move != ROTATE_CCW) {
- rotate_matrix(g, 0);
- }
- g->held_move = ROTATE_CCW;
+void stop_holding(struct cetris_game* g, input_t move) {
+ g->held_moves[move] = 0;
+ if (move == RIGHT || move == LEFT) {
+ g->das_move_tick = 0;
+ g->das_repeat = 0;
+ } else if (move == DOWN) g->down_move_tick = 0;
}
diff --git a/src/cetris.h b/src/cetris.h
index 2690163..8264842 100644
--- a/src/cetris.h
+++ b/src/cetris.h
@@ -65,11 +65,11 @@ struct cetris_game {
uint8_t current_index;
/* input_manager */
- input_t held_move;
- input_t prev_move;
- int next_move_tick;
- uint8_t can_rotate;
- uint8_t can_hard_drop;
+ uint8_t held_moves[7];
+ int das_repeat;
+ input_t prev_das_move;
+ int das_move_tick;
+ int down_move_tick;
/* internal game tick */
int tick;
@@ -95,9 +95,5 @@ void wipe_board(struct cetris_game* g);
void init_game(struct cetris_game* g);
void update_game_tick(struct cetris_game* g);
-void move_down(struct cetris_game* g);
-void move_left(struct cetris_game* g);
-void move_right(struct cetris_game* g);
-void move_hard_drop(struct cetris_game* g);
-void rotate_clockwise(struct cetris_game* g);
-void rotate_counterclockwise(struct cetris_game* g);
+void move_peice(struct cetris_game* g, input_t move);
+void stop_holding(struct cetris_game* g, input_t move);
diff --git a/src/input.c b/src/input.c
index e3b61b8..9fd9142 100644
--- a/src/input.c
+++ b/src/input.c
@@ -7,44 +7,45 @@
#include "cetris.h"
uint8_t handle_inputs(struct cetris_game* g) {
- uint8_t did_move = 0;
- if (g->held_move && !g->next_move_tick) {
- if (g->held_move == RIGHT || g->held_move == LEFT) {
- if (g->prev_move == g->held_move) {
- g->next_move_tick = g->tick + CETRIS_DAS_PERIOD;
- } else {
- g->next_move_tick = g->tick + CETRIS_DAS_DELAY;
- }
- } else {
- g->next_move_tick = g->tick + CETRIS_DROP_PERIOD;
+ if ((g->held_moves[RIGHT] || g->held_moves[LEFT]) && !g->das_move_tick) {
+ if (g->das_repeat == 0) {
+ g->das_move_tick = g->tick + CETRIS_DAS_DELAY;
+ } else if (g->das_repeat > 0) {
+ g->das_move_tick = g->tick + CETRIS_DAS_PERIOD;
}
}
- if (g->next_move_tick && g->tick >= g->next_move_tick) {
- switch (g->held_move) {
- case DOWN:
- g->score++;
- move_current(g, basic_movements[DOWN]);
- break;
- case LEFT:
- move_current(g, basic_movements[LEFT]);
- break;
- case RIGHT:
- move_current(g, basic_movements[RIGHT]);
- break;
+ if (g->held_moves[DOWN] && !g->down_move_tick) {
+ g->down_move_tick = g->tick + CETRIS_DROP_PERIOD;
+ }
+
+ uint8_t did_move = 0;
+ if (g->das_move_tick && g->tick >= g->das_move_tick) {
+ if (g->held_moves[RIGHT]) {
+ if (g->prev_das_move == RIGHT || g->das_repeat == 0) g->das_repeat++;
+ else g->das_repeat = 0;
+ move_current(g, basic_movements[RIGHT]);
+ g->prev_das_move = RIGHT;
+ }
+
+ if (g->held_moves[LEFT]) {
+ if (g->prev_das_move == LEFT || g->das_repeat == 0) g->das_repeat++;
+ else g->das_repeat = 0;
+ move_current(g, basic_movements[LEFT]);
+ g->prev_das_move = LEFT;
}
+
+ g->das_move_tick = 0;
did_move = 1;
}
- if (did_move) {
- g->next_move_tick = 0;
- g->prev_move = g->held_move;
+ if (g->down_move_tick && g->tick >= g->down_move_tick) {
+ if (g->held_moves[DOWN]) {
+ move_current(g, basic_movements[DOWN]);
+ }
+
+ g->down_move_tick = 0;
+ did_move = 1;
}
return did_move;
}
-
-void clear_held_key(struct cetris_game* g) {
- g->prev_move = 0;
- g->held_move = 0;
- g->next_move_tick = 0;
-}
diff --git a/src/matrix.c b/src/matrix.c
index 1fabdce..cac9c47 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -142,7 +142,8 @@ void hard_drop(struct cetris_game* g) {
void rotate_matrix(struct cetris_game* g, int clockwise) {
if (g->current.t == O) return;
- rstate next; int8_t wall_kick;
+ rstate next;
+ int8_t wall_kick;
switch (g->current.r) {
case INIT:
if (clockwise) {
@@ -197,26 +198,6 @@ void rotate_matrix(struct cetris_game* g, int clockwise) {
}
}
- /*
- uint8_t wall_kick;
- switch (g->current.r) {
- case INIT:
- wall_kick = (next == RRIGHT) ? 0 : 7;
- break;
- case ONCE_RIGHT:
- wall_kick = (next == INIT) ? 1 : 2;
- break;
- case ONCE_LEFT:
- wall_kick = (next == INIT) ? 6 : 5;
- break;
- case TWICE:
- wall_kick = (next == RRIGHT) ? 3 : 4;
- break;
- default: // check for invalid rotations
- assert(0);
- }
- */
-
vec2 kick;
uint8_t set_current = 0;
uint8_t did_kick = 0;
diff --git a/src/matrix.h b/src/matrix.h
index f75e1e0..3ab0252 100644
--- a/src/matrix.h
+++ b/src/matrix.h
@@ -5,8 +5,8 @@
#include "types.h"
#include "cetris.h"
-const piece_matrix default_matrices[7];
-const vec2 basic_movements[4];
+extern const piece_matrix default_matrices[7];
+extern const vec2 basic_movements[4];
void move_current(struct cetris_game* g, vec2 offset);
void overlay_current_matrix(struct cetris_game* g);