From 67322d79c305409b187b6ea4a971ff4c4538c40f Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 13 Mar 2022 12:44:13 -0700 Subject: [PATCH] Add wall kicks --- src/Main.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Main.cpp b/src/Main.cpp index 8654fdb..773ad4c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -283,12 +283,26 @@ int main() if (rotate) { block.rotation_state++; // Check to see if new rotation state is overlapping any tiles + int offset_required = 0; for (auto tile : block.get_tiles()) { - if (tile.x <= 0 || tile.x >= GRID_WIDTH || grid[tile.y][tile.x]) { + if (grid[tile.y][tile.x]) { + // Can't wall kick off of blocks block.rotation_state--; break; } + if (tile.x <= 0) { + int potential_offset = -tile.x; + if (potential_offset > abs(offset_required)) { + offset_required = potential_offset; + } + } else if (tile.x >= GRID_WIDTH) { + int potential_offset = GRID_WIDTH - tile.x - 1; + if (-potential_offset > abs(offset_required)) { + offset_required = potential_offset; + } + } } + block.position.x += offset_required; rotate = false; rotate_sound.play(); }