[Orxonox-commit 3863] r8537 - in code/branches/tetris: data/levels src/modules/tetris

catherine at orxonox.net catherine at orxonox.net
Mon May 23 15:06:09 CEST 2011


Author: catherine
Date: 2011-05-23 15:06:08 +0200 (Mon, 23 May 2011)
New Revision: 8537

Modified:
   code/branches/tetris/data/levels/tetris.oxw
   code/branches/tetris/src/modules/tetris/Tetris.cc
   code/branches/tetris/src/modules/tetris/Tetris.h
   code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc
   code/branches/tetris/src/modules/tetris/TetrisStone.cc
   code/branches/tetris/src/modules/tetris/TetrisStone.h
Log:

Almost working.


Modified: code/branches/tetris/data/levels/tetris.oxw
===================================================================
--- code/branches/tetris/data/levels/tetris.oxw	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/data/levels/tetris.oxw	2011-05-23 13:06:08 UTC (rev 8537)
@@ -49,7 +49,7 @@
       <SpawnPoint position="<?lua print(math.random() * 1000 - 500) ?>,<?lua print(math.random() * 1000 - 500) ?>,<?lua print(math.random() * 1000 - 500) ?>" lookat="0,0,0" />
     <?lua end ?>
 
-    <TetrisCenterpoint name=tetriscenter width=11 height=15 stoneSize=10 stoneTemplate=tetrisstone stoneSpeed=5 position="-55,-75,0">
+    <TetrisCenterpoint name=tetriscenter width=11 height=15 stoneSize=10 stoneTemplate=tetrisstone stoneSpeed=10 position="-55,-75,0">
         <attached>
             <Model position="55,-1,0" mesh="cube.mesh" scale3D="57,1,11" />
             <Model position="-1,76,0" mesh="cube.mesh" scale3D="1,76,1" />

Modified: code/branches/tetris/src/modules/tetris/Tetris.cc
===================================================================
--- code/branches/tetris/src/modules/tetris/Tetris.cc	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/src/modules/tetris/Tetris.cc	2011-05-23 13:06:08 UTC (rev 8537)
@@ -81,39 +81,39 @@
     {
         
     }
-    
+
     void Tetris::tick(float dt)
     {
         SUPER(Tetris, tick, dt);
+
+        if(this->activeStone_ != NULL && !this->isValidMove(this->activeStone_, this->activeStone_->getPosition()))
+        {
+            this->activeStone_->setVelocity(Vector3::ZERO);
+            //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
+            this->createStone();
+            this->startStone();
+        }
+    }
+
+    bool Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
+    {
+        assert(stone);
         
-        TetrisStone* stone = this->activeStone_;
-        if(stone != NULL)
+        if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
+            return false;
+        else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
+            return false;
+        
+        if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
         {
-            // Get the current position of the active stone
-            Vector3 position = stone->getPosition();
-            
-            if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
-                position.x = this->center_->getStoneSize()/2.0;
-            else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
-                position.x = (this->center_->getWidth()-0.5)*this->center_->getStoneSize();
+            stone->setVelocity(Vector3::ZERO);
+            //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true;
+            this->createStone();
+            this->startStone();
+            return false;
+        }
 
-            if(!this->correctStonePos(stone)) //!< If the stone touches another stone
-            {
-                stone->setVelocity(Vector3::ZERO);
-                this->createStone();
-                this->startStone();
-            }
-
-            if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
-            {
-                position.y = this->center_->getStoneSize()/2.0;
-                stone->setVelocity(Vector3::ZERO);
-                this->createStone();
-                this->startStone();
-            }
-            
-            stone->setPosition(position);
-        }
+        return this->correctStonePos(stone, position);
     }
 
     /**
@@ -134,7 +134,7 @@
             return;
         }
 
-        // Start the timer. After it has expired the ball is started.
+        // Start the timer. After it has expired the stone is started.
         this->starttimer_.startTimer();
 
         // Set variable to temporarily force the player to spawn.
@@ -226,6 +226,7 @@
         float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0)*this->center_->getStoneSize();
         float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize();
         stone->setPosition(xPos, yPos, 0.0f);
+        stone->setGame(this);
     }
 
     /**
@@ -234,24 +235,31 @@
     @return
         Returns whether the supplied stone is in the correct position.
     */
-    bool Tetris::correctStonePos(TetrisStone* stone)
+    bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position)
     {
+        assert(stone);
+
         for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
         {
-            TetrisStone* currentStone = it->_Ptr(); //!< Gives access to the current stone in the list
-            Vector3 currentStonePosition = it->_Ptr()->getPosition(); //!< Saves the position of the currentStone
-            Vector3 stonePosition = stone->getPosition(); //!< Saves the position of the supplied stone
-            
-            // @TODO:   Use the TetrisStone member functions to check both stones for an overlap.
-            //          Also make sure to correct the stone position accordingly.
-            //
-            // This case applies if the stones overlap completely
-            //if((stonePosition.x == currentStonePosition.x) && (stonePosition.y == currentStonePosition.y))
-            // This case applies if the stones overlap partially vertically
-            //if(stonePosition.y - stone->getHeight()/2 < currentStonePosition.y + currentStone->getHeight()/2)
+            if(stone == *it)
+                continue;
 
+            Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
             
+            if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
+            {
+                stone->setVelocity(Vector3::ZERO);
+                this->createStone();
+                this->startStone();
+                return false;
+            }// This case applies if the stones overlap completely
+            if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
+            {
+                return false;
+            }// This case applies if the stones overlap partially vertically
         }
+
+        return true;
     }
 
     /**
@@ -265,4 +273,21 @@
         return this->player_;
     }
 
+    /**
+    @brief Set the TetrisCenterpoint (the playing field).
+    @param center A pointer to the TetrisCenterpoint to be set.
+    */
+    void Tetris::setCenterpoint(TetrisCenterpoint* center)
+    {
+        this->center_ = center;
+
+        /*this->grid_.resize(this->center_->getWidth());
+        for(std::vector< std::vector<bool> >::iterator it = this->grid_.begin(); it != this->grid_.end(); it++)
+        {
+            (*it).resize(this->center_->getHeight());
+            for(std::vector<bool>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
+                (*it).insert(it2, false);
+        }*/
+    }
+
 }

Modified: code/branches/tetris/src/modules/tetris/Tetris.h
===================================================================
--- code/branches/tetris/src/modules/tetris/Tetris.h	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/src/modules/tetris/Tetris.h	2011-05-23 13:06:08 UTC (rev 8537)
@@ -55,36 +55,35 @@
     {
         public:
             Tetris(BaseObject* creator); //!< Constructor. Registers and initializes the object.
-            virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.
-            
-            virtual void tick(float dt);
+            virtual ~Tetris(); //!< Destructor. Cleans up, if initialized.           
 
             virtual void start(void); //!< Starts the Tetris minigame.
             virtual void end(void); ///!< Ends the Tetris minigame.
 
+            virtual void tick(float dt);
+
             virtual void spawnPlayer(PlayerInfo* player); //!< Spawns the input player.
 
-            /**
-            @brief Set the TetrisCenterpoint (the playing field).
-            @param center A pointer to the TetrisCenterpoint to be set.
-            */
-            void setCenterpoint(TetrisCenterpoint* center)
-                { this->center_ = center; }
+            void setCenterpoint(TetrisCenterpoint* center);
 
             PlayerInfo* getPlayer(void) const; //!< Get the player.
 
+            bool isValidMove(TetrisStone* stone, const Vector3& position);
+
         protected:
             virtual void spawnPlayersIfRequested(); //!< Spawns player.
 
+        private:
             void startStone(void); //!< Starts with the first stone.
             void createStone(void);
             void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
-            bool correctStonePos(TetrisStone* stone); //!< Check whether the supplied stone is in an allowed position
+            bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position
             
             PlayerInfo* player_;
 
             WeakPtr<TetrisCenterpoint> center_; //!< The playing field.
             std::vector<TetrisStone*> stones_; //!< A list of all stones in play.
+            std::vector< std::vector<bool> > grid_;
             TetrisStone* activeStone_;
             
             Timer starttimer_; //!< A timer to delay the start of the game.

Modified: code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc
===================================================================
--- code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc	2011-05-23 13:06:08 UTC (rev 8537)
@@ -67,9 +67,9 @@
     {
         SUPER(TetrisCenterpoint, XMLPort, xmlelement, mode);
 
-        XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode);
-        XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode);
-        XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode);
+        XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode); // die Breite
+        XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode); // die Grösse
+        XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode); 
         XMLPortParam(TetrisCenterpoint, "stoneTemplate", setStoneTemplate, getStoneTemplate, xmlelement, mode);
         XMLPortParam(TetrisCenterpoint, "stoneSpeed", setStoneSpeed, getStoneSpeed, xmlelement, mode);
     }

Modified: code/branches/tetris/src/modules/tetris/TetrisStone.cc
===================================================================
--- code/branches/tetris/src/modules/tetris/TetrisStone.cc	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/src/modules/tetris/TetrisStone.cc	2011-05-23 13:06:08 UTC (rev 8537)
@@ -36,6 +36,8 @@
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
 
+#include "Tetris.h"
+
 namespace orxonox
 {
     CreateFactory(TetrisStone);
@@ -51,8 +53,14 @@
         this->size_ = 10.0f;
         this->delay_ = false;
         this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisStone::enableMovement, this)));
+        this->previousPosition_ = Vector3::ZERO;
     }
 
+    void TetrisStone::tick(float dt)
+    {
+        SUPER(TetrisStone, tick, dt);
+    }
+
     /**
     @brief
         Overloaded the function to rotate the stone.
@@ -75,7 +83,12 @@
         if(!this->delay_)
         {
             const Vector3& position = this->getPosition();
-            this->setPosition(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
+            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
+            if(!this->tetris_->isValidMove(this, newPos))
+                return;
+
+            //this->previousPosition_ = position;
+            this->setPosition(newPos);
             this->delay_ = true;
             this->delayTimer_.startTimer();
         }

Modified: code/branches/tetris/src/modules/tetris/TetrisStone.h
===================================================================
--- code/branches/tetris/src/modules/tetris/TetrisStone.h	2011-05-23 12:52:25 UTC (rev 8536)
+++ code/branches/tetris/src/modules/tetris/TetrisStone.h	2011-05-23 13:06:08 UTC (rev 8537)
@@ -56,6 +56,8 @@
             TetrisStone(BaseObject* creator); //!< Constructor. Registers and initializes the object.
             virtual ~TetrisStone() {}
 
+            virtual void tick(float dt);
+
             virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
             virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down.
 
@@ -74,6 +76,12 @@
             float getSize(void) const
                 { return this->size_; }
 
+            const Vector3& getPreviousPosition() const
+                { return this->previousPosition_; }
+
+            void setGame(Tetris* tetris)
+                { assert(tetris); tetris_ = tetris; }
+
         private:
             void enableMovement(void)
                 { this->delay_ = false; }
@@ -81,6 +89,9 @@
             float size_; //!< The dimensions a stone has in the game world.
             bool delay_;
             Timer delayTimer_;
+
+            Vector3 previousPosition_;
+            Tetris* tetris_;
     };
 }
 




More information about the Orxonox-commit mailing list