From 41d2578d3401e38fa8600a533ad5998dc39e7be5 Mon Sep 17 00:00:00 2001
From: Ekaitz Zarraga <ekaitz@elenq.tech>
Date: Sat, 21 May 2022 21:36:37 +0200
Subject: add move sideways capability

---
 src/command.cpp  |  8 ++++++++
 src/command.h    |  8 ++++++++
 src/game.cpp     | 48 ++++++++++++++++++++++++++++++++++--------------
 src/game.h       |  2 ++
 src/keyboard.cpp | 13 +++++++++++--
 src/keyboard.h   |  2 ++
 src/piece.cpp    |  6 ++++++
 src/piece.h      |  2 ++
 8 files changed, 73 insertions(+), 16 deletions(-)

diff --git a/src/command.cpp b/src/command.cpp
index d0ee45b..efe2987 100644
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -32,3 +32,11 @@ KeyboardDownPressed::~KeyboardDownPressed(){}
 void KeyboardDownPressed::execute( GameState &game ){
     game.pressDown();
 }
+KeyboardLeftPressed::~KeyboardLeftPressed(){}
+void KeyboardLeftPressed::execute( GameState &game ){
+    game.pressLeft();
+}
+KeyboardRightPressed::~KeyboardRightPressed(){}
+void KeyboardRightPressed::execute( GameState &game ){
+    game.pressRight();
+}
diff --git a/src/command.h b/src/command.h
index a40e786..c22e6e5 100644
--- a/src/command.h
+++ b/src/command.h
@@ -36,5 +36,13 @@ class KeyboardDownPressed: public Command {
     ~KeyboardDownPressed();
     void execute( GameState &state ) override;
 };
+class KeyboardLeftPressed: public Command {
+    ~KeyboardLeftPressed();
+    void execute( GameState &state ) override;
+};
+class KeyboardRightPressed: public Command {
+    ~KeyboardRightPressed();
+    void execute( GameState &state ) override;
+};
 
 #endif
diff --git a/src/game.cpp b/src/game.cpp
index 1ed8cc0..17000a0 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -51,20 +51,14 @@ void GameState::update( unsigned int dt ){
         Piece tmp = piece_;
         tmp.advance();
         Point* p;
-        tmp.initIterator();
-
         // Check if there's space to move forward
-        bool canadvance;
+        tmp.initIterator();
         while ( p = tmp.nextAbsBlockPos() ){
-            if ( grid[p->y][p->x] == 0 ){
-                canadvance = true;
-            } else {
-                canadvance = false;
+            if(!cellIsEmpty(p)){
+                return;
             }
         }
-        if ( canadvance ){
-            piece_ = tmp;
-        }
+        piece_ = tmp;
     }
 
     // If timer finished, level up
@@ -106,6 +100,9 @@ void GameState::render(){
     renderer_->update();
 }
 
+bool GameState::cellIsEmpty(Point* p){
+    return( grid[p->y][p->x] == 0 );
+}
 
 void GameState::pressRotate(){}
 void GameState::pressDown(){
@@ -115,7 +112,30 @@ void GameState::releaseDown(){
     yspeed_ = 1;
 }
 
-void GameState::pressLeft(){}
-void GameState::releaseLeft(){}
-void GameState::pressRight(){}
-void GameState::releaseRight(){}
+void GameState::pressLeft(){
+    Piece tmp = piece_;
+    tmp.move_left();
+    Point* p;
+
+    // Check if there's space to move to
+    tmp.initIterator();
+    while ( p = tmp.nextAbsBlockPos() ){
+        if(!cellIsEmpty(p)){
+            return;
+        }
+    }
+    piece_ = tmp;
+}
+void GameState::pressRight(){
+    Piece tmp = piece_;
+    tmp.move_right();
+    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 8965010..0e6a553 100644
--- a/src/game.h
+++ b/src/game.h
@@ -47,6 +47,8 @@ class GameState{
         void update(unsigned int dt);
         void render();
 
+        bool cellIsEmpty(Point* p);
+
         void pressRotate();
         void pressDown();
         void releaseDown();
diff --git a/src/keyboard.cpp b/src/keyboard.cpp
index 1eff1d1..8f5d81c 100644
--- a/src/keyboard.cpp
+++ b/src/keyboard.cpp
@@ -2,13 +2,18 @@
 
 Keyboard::Keyboard(){
     nop = new Nop;
-    pressedDown = new KeyboardDownPressed;
-    releasedDown = new KeyboardDownReleased;
+
+    pressedDown   = new KeyboardDownPressed;
+    releasedDown  = new KeyboardDownReleased;
+    pressedLeft   = new KeyboardLeftPressed;
+    pressedRight  = new KeyboardRightPressed;
 }
 Keyboard::~Keyboard(){
     delete nop;
     delete pressedDown;
     delete releasedDown;
+    delete pressedLeft;
+    delete pressedRight;
 }
 
 Command* Keyboard::handleEvent( SDL_Event e ){
@@ -26,6 +31,10 @@ Command* Keyboard::pressed( SDL_Keycode k, Uint16 mod ){
     switch(k){
         case SDLK_DOWN:
             return pressedDown;
+        case SDLK_LEFT:
+            return pressedLeft;
+        case SDLK_RIGHT:
+            return pressedRight;
         default:
             return nop;
     }
diff --git a/src/keyboard.h b/src/keyboard.h
index 561fda2..6853ffc 100644
--- a/src/keyboard.h
+++ b/src/keyboard.h
@@ -18,6 +18,8 @@ class Keyboard{
         Command* nop;
         Command* pressedDown;
         Command* releasedDown;
+        Command* pressedLeft;
+        Command* pressedRight;
 };
 
 #endif
diff --git a/src/piece.cpp b/src/piece.cpp
index 51e8dc7..67755f0 100644
--- a/src/piece.cpp
+++ b/src/piece.cpp
@@ -69,3 +69,9 @@ void Piece::rotate(){
 void Piece::advance(){
     position_.y++;
 }
+void Piece::move_left(){
+    position_.x--;
+}
+void Piece::move_right(){
+    position_.x++;
+}
diff --git a/src/piece.h b/src/piece.h
index 53bd112..ee95539 100644
--- a/src/piece.h
+++ b/src/piece.h
@@ -24,6 +24,8 @@ class Piece {
         Point getPosition();
         void rotate();
         void advance();
+        void move_left();
+        void move_right();
 
         void initIterator();
         Point* nextAbsBlockPos();
-- 
cgit v1.2.3