summary refs log tree commit diff
diff options
context:
space:
mode:
authorturnipgod2025-03-06 12:35:55 -0500
committerturnipgod2025-03-06 12:35:55 -0500
commitc4b1af3f97d0d7fe64a7369dd72891ee3b4fd422 (patch)
treec98d3dd1cf09ffb1ce2554231873a36424b54499
parent359c47fa7573ba214f3b2e00a19fe2fd4c90e0d2 (diff)
added delayed auto shift (DAS) HEAD main
-rw-r--r--src/main.c83
1 files changed, 39 insertions, 44 deletions
diff --git a/src/main.c b/src/main.c
index 2fee6bd..255d841 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,6 +14,7 @@
 #define FALL_TICK 0.3
 #define SOFT_DROP_MULT 4.0
 #define LOCK_TICK 0.5
+#define DAS 0.3
 
 enum InputResult {
     NONE = 0,
@@ -49,6 +50,7 @@ struct {
     int held;
     double phase_time;
     double fall_tick;
+    double das_timer;
 } game_state;
 
 void draw_block(int x, int y, int width, int height, struct Color color)
@@ -354,58 +356,51 @@ void hold_piece(void)
 }
 
 int handle_input(void) {
-    static int moved_l = 0;
-    static int moved_r = 0;
-    static int moved_cw = 0;
-    static int moved_ccw = 0;
-    static int hard_dropped = 0;
-
-    if (IsKeyDown(KEY_A)) {
-        if (!moved_l) {
-            moved_l = move_piece(-1);
-            return MOVED;
-        }
-    } else {
-        moved_l = 0;
+    enum InputResult action = NONE;
+
+    if (IsKeyPressed(KEY_A)) {
+        move_piece(-1);
+        game_state.das_timer = 0.0;
+        action = MOVED;
     }
 
-    if (IsKeyDown(KEY_D)) {
-        if (!moved_r) {
-            moved_r = move_piece(+1);
-            return MOVED;
-        }
-    } else {
-        moved_r = 0;
+    if (IsKeyPressed(KEY_D)) {
+        move_piece(+1);
+        game_state.das_timer = 0.0;
+        action = MOVED;
     }
 
-    if (IsKeyDown(KEY_L)) {
-        if (!moved_cw) {
-            moved_cw = rotate_piece(+1);
-            return MOVED;
-        }
+    if (IsKeyDown(KEY_A) || IsKeyDown(KEY_D)) {
+        game_state.das_timer += GetFrameTime();
     } else {
-        moved_cw = 0;
+        game_state.das_timer = 0.0;
     }
 
-    if (IsKeyDown(KEY_J)) {
-        if (!moved_ccw) {
-            moved_ccw = rotate_piece(-1);
-            return MOVED;
-        }
-    } else {
-        moved_ccw = 0;
+    if (IsKeyDown(KEY_A) && game_state.das_timer > DAS) {
+        move_piece(-1);
+        action = MOVED;
     }
 
-    if (IsKeyDown(KEY_W)) {
-        if (!hard_dropped) {
-            hard_dropped = 1;
-            while (!is_overlap(0, -1)) {
-                game_state.current_piece.pos_y--;
-            }
-            return HARD_DROPPED;
+    if (IsKeyDown(KEY_D) && game_state.das_timer > DAS) {
+        move_piece(+1);
+        action = MOVED;
+    }
+
+    if (IsKeyPressed(KEY_L)) {
+        rotate_piece(+1);
+        action = MOVED;
+    }
+
+    if (IsKeyPressed(KEY_J)) {
+        rotate_piece(-1);
+        action = MOVED;
+    }
+
+    if (IsKeyPressed(KEY_W)) {
+        while (!is_overlap(0, -1)) {
+            game_state.current_piece.pos_y--;
         }
-    } else {
-        hard_dropped = 0;
+        action = HARD_DROPPED;
     }
 
     if (IsKeyDown(KEY_S)) {
@@ -414,11 +409,11 @@ int handle_input(void) {
         game_state.fall_tick = FALL_TICK;
     }
 
-    if (IsKeyDown(KEY_SPACE)) {
+    if (IsKeyPressed(KEY_SPACE)) {
         hold_piece();
     }
 
-    return NONE;
+    return action;
 }
 
 void lock_phase(void) {