From d324ad963c24edf6d19dffc305a38e0b3607b5c1 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 21 May 2022 22:24:01 +0200 Subject: Add piece rotation --- src/command.cpp | 4 ++++ src/command.h | 4 ++++ src/game.cpp | 15 ++++++++++++++- src/game.h | 3 +-- src/keyboard.cpp | 4 ++++ src/keyboard.h | 1 + 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index efe2987..24b50cd 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -40,3 +40,7 @@ KeyboardRightPressed::~KeyboardRightPressed(){} void KeyboardRightPressed::execute( GameState &game ){ game.pressRight(); } +KeyboardSpacePressed::~KeyboardSpacePressed(){} +void KeyboardSpacePressed::execute( GameState &game ){ + game.pressRotate(); +} diff --git a/src/command.h b/src/command.h index c22e6e5..425480c 100644 --- a/src/command.h +++ b/src/command.h @@ -45,4 +45,8 @@ class KeyboardRightPressed: public Command { void execute( GameState &state ) override; }; +class KeyboardSpacePressed: public Command { + ~KeyboardSpacePressed(); + void execute( GameState &state ) override; +}; #endif diff --git a/src/game.cpp b/src/game.cpp index 47526c8..d897aa4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -104,7 +104,6 @@ bool GameState::cellIsEmpty(Point* p){ return( p->x < GRID_WIDTH && p->x >= 0 && grid[p->y][p->x] == 0 ); } -void GameState::pressRotate(){} void GameState::pressDown(){ yspeed_ = 4; } @@ -139,3 +138,17 @@ void GameState::pressRight(){ } piece_ = tmp; } + +void GameState::pressRotate(){ + Piece tmp = piece_; + tmp.rotate(); + Point* p; + // Check if there's space to move to + tmp.initIterator(); + while ( p = tmp.nextAbsBlockPos() ){ + if(!cellIsEmpty(p)){ + return; + } + } + piece_ = tmp; +} diff --git a/src/game.h b/src/game.h index 0e6a553..87e3f93 100644 --- a/src/game.h +++ b/src/game.h @@ -53,9 +53,8 @@ class GameState{ void pressDown(); void releaseDown(); void pressLeft(); - void releaseLeft(); void pressRight(); - void releaseRight(); + void pressSpace(); }; #endif diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 8f5d81c..8c94c68 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -7,6 +7,7 @@ Keyboard::Keyboard(){ releasedDown = new KeyboardDownReleased; pressedLeft = new KeyboardLeftPressed; pressedRight = new KeyboardRightPressed; + pressedSpace = new KeyboardSpacePressed; } Keyboard::~Keyboard(){ delete nop; @@ -14,6 +15,7 @@ Keyboard::~Keyboard(){ delete releasedDown; delete pressedLeft; delete pressedRight; + delete pressedSpace; } Command* Keyboard::handleEvent( SDL_Event e ){ @@ -35,6 +37,8 @@ Command* Keyboard::pressed( SDL_Keycode k, Uint16 mod ){ return pressedLeft; case SDLK_RIGHT: return pressedRight; + case SDLK_SPACE: + return pressedSpace; default: return nop; } diff --git a/src/keyboard.h b/src/keyboard.h index 6853ffc..53a1843 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -20,6 +20,7 @@ class Keyboard{ Command* releasedDown; Command* pressedLeft; Command* pressedRight; + Command* pressedSpace; }; #endif -- cgit v1.2.3