summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-05-21 22:24:01 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-05-21 22:59:59 +0200
commitd324ad963c24edf6d19dffc305a38e0b3607b5c1 (patch)
tree10d487be5d723882a12876ea34bf2dcc2233a4d1
parent27a55a2e841e9c248d6b055780a519a7b5ee4263 (diff)
Add piece rotation
-rw-r--r--src/command.cpp4
-rw-r--r--src/command.h4
-rw-r--r--src/game.cpp15
-rw-r--r--src/game.h3
-rw-r--r--src/keyboard.cpp4
-rw-r--r--src/keyboard.h1
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