From d67b06d006925389aa196a3ef3f9e87b425f3566 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 5 Mar 2022 12:14:43 -0800 Subject: [PATCH] Add block rotation --- src/Main.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index f16448d..095a532 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #define WINDOW_WIDTH 400 #define WINDOW_HEIGHT 400 @@ -107,16 +108,27 @@ class Block { } int rotated_x = x; int rotated_y = y; + int center_x = type->grid[0].size() / 2; + int center_y = type->grid.size() / 2; + int offset_x = x - center_x; + int offset_y = y - center_y; switch (rotation_state) { case 0: + rotated_x = x; + rotated_y = y; break; case 1: - rotated_x *= -1; + rotated_x = center_x + offset_y; + rotated_y = center_y - offset_x; + break; case 2: - rotated_x *= -1; - rotated_y *= -1; + rotated_x = center_x - offset_x; + rotated_y = center_y - offset_y; + break; case 3: - rotated_y *= -1; + rotated_x = center_x - offset_y; + rotated_y = center_y + offset_x; + break; default: rotation_state %= 4; }