Changeset 8537
- Timestamp:
- May 23, 2011, 3:06:08 PM (14 years ago)
- Location:
- code/branches/tetris
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tetris/data/levels/tetris.oxw
r8246 r8537 50 50 <?lua end ?> 51 51 52 <TetrisCenterpoint name=tetriscenter width=11 height=15 stoneSize=10 stoneTemplate=tetrisstone stoneSpeed= 5position="-55,-75,0">52 <TetrisCenterpoint name=tetriscenter width=11 height=15 stoneSize=10 stoneTemplate=tetrisstone stoneSpeed=10 position="-55,-75,0"> 53 53 <attached> 54 54 <Model position="55,-1,0" mesh="cube.mesh" scale3D="57,1,11" /> -
code/branches/tetris/src/modules/tetris/Tetris.cc
r8488 r8537 82 82 83 83 } 84 84 85 85 void Tetris::tick(float dt) 86 86 { 87 87 SUPER(Tetris, tick, dt); 88 89 TetrisStone* stone = this->activeStone_;90 if(stone != NULL)91 {92 // Get the current position of the active stone93 Vector3 position = stone->getPosition();94 95 if(position.x < this->center_->getStoneSize()/2.0) //!< If the stone touches the left edge of the level96 position.x = this->center_->getStoneSize()/2.0;97 else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level 98 position.x = (this->center_->getWidth()-0.5)*this->center_->getStoneSize();99 100 if(!this->correctStonePos(stone)) //!< If the stone touches another stone101 {102 stone->setVelocity(Vector3::ZERO);103 this->createStone();104 this->startStone();105 }106 107 108 109 position.y = this->center_->getStoneSize()/2.0;110 stone->setVelocity(Vector3::ZERO);111 112 113 }114 115 stone->setPosition(position); 116 }88 89 if(this->activeStone_ != NULL && !this->isValidMove(this->activeStone_, this->activeStone_->getPosition())) 90 { 91 this->activeStone_->setVelocity(Vector3::ZERO); 92 //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true; 93 this->createStone(); 94 this->startStone(); 95 } 96 } 97 98 bool Tetris::isValidMove(TetrisStone* stone, const Vector3& position) 99 { 100 assert(stone); 101 102 if(position.x < this->center_->getStoneSize()/2.0) //!< If the stone touches the left edge of the level 103 return false; 104 else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level 105 return false; 106 107 if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level 108 { 109 stone->setVelocity(Vector3::ZERO); 110 //this->grid_[(int)(position.x/this->center_->getStoneSize())][(int)(position.y/this->center_->getStoneSize())] = true; 111 this->createStone(); 112 this->startStone(); 113 return false; 114 } 115 116 return this->correctStonePos(stone, position); 117 117 } 118 118 … … 135 135 } 136 136 137 // Start the timer. After it has expired the ballis started.137 // Start the timer. After it has expired the stone is started. 138 138 this->starttimer_.startTimer(); 139 139 … … 227 227 float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize(); 228 228 stone->setPosition(xPos, yPos, 0.0f); 229 stone->setGame(this); 229 230 } 230 231 … … 235 236 Returns whether the supplied stone is in the correct position. 236 237 */ 237 bool Tetris::correctStonePos(TetrisStone* stone) 238 { 238 bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position) 239 { 240 assert(stone); 241 239 242 for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 240 243 { 241 TetrisStone* currentStone = it->_Ptr(); //!< Gives access to the current stone in the list 242 Vector3 currentStonePosition = it->_Ptr()->getPosition(); //!< Saves the position of the currentStone 243 Vector3 stonePosition = stone->getPosition(); //!< Saves the position of the supplied stone 244 if(stone == *it) 245 continue; 246 247 Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone 244 248 245 // @TODO: Use the TetrisStone member functions to check both stones for an overlap. 246 // Also make sure to correct the stone position accordingly. 247 // 248 // This case applies if the stones overlap completely 249 //if((stonePosition.x == currentStonePosition.x) && (stonePosition.y == currentStonePosition.y)) 250 // This case applies if the stones overlap partially vertically 251 //if(stonePosition.y - stone->getHeight()/2 < currentStonePosition.y + currentStone->getHeight()/2) 252 253 254 } 249 if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y)) 250 { 251 stone->setVelocity(Vector3::ZERO); 252 this->createStone(); 253 this->startStone(); 254 return false; 255 }// This case applies if the stones overlap completely 256 if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize())) 257 { 258 return false; 259 }// This case applies if the stones overlap partially vertically 260 } 261 262 return true; 255 263 } 256 264 … … 266 274 } 267 275 276 /** 277 @brief Set the TetrisCenterpoint (the playing field). 278 @param center A pointer to the TetrisCenterpoint to be set. 279 */ 280 void Tetris::setCenterpoint(TetrisCenterpoint* center) 281 { 282 this->center_ = center; 283 284 /*this->grid_.resize(this->center_->getWidth()); 285 for(std::vector< std::vector<bool> >::iterator it = this->grid_.begin(); it != this->grid_.end(); it++) 286 { 287 (*it).resize(this->center_->getHeight()); 288 for(std::vector<bool>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++) 289 (*it).insert(it2, false); 290 }*/ 291 } 292 268 293 } -
code/branches/tetris/src/modules/tetris/Tetris.h
r8488 r8537 56 56 public: 57 57 Tetris(BaseObject* creator); //!< Constructor. Registers and initializes the object. 58 virtual ~Tetris(); //!< Destructor. Cleans up, if initialized. 59 60 virtual void tick(float dt); 58 virtual ~Tetris(); //!< Destructor. Cleans up, if initialized. 61 59 62 60 virtual void start(void); //!< Starts the Tetris minigame. 63 61 virtual void end(void); ///!< Ends the Tetris minigame. 64 62 63 virtual void tick(float dt); 64 65 65 virtual void spawnPlayer(PlayerInfo* player); //!< Spawns the input player. 66 66 67 /** 68 @brief Set the TetrisCenterpoint (the playing field). 69 @param center A pointer to the TetrisCenterpoint to be set. 70 */ 71 void setCenterpoint(TetrisCenterpoint* center) 72 { this->center_ = center; } 67 void setCenterpoint(TetrisCenterpoint* center); 73 68 74 69 PlayerInfo* getPlayer(void) const; //!< Get the player. 70 71 bool isValidMove(TetrisStone* stone, const Vector3& position); 75 72 76 73 protected: 77 74 virtual void spawnPlayersIfRequested(); //!< Spawns player. 78 75 76 private: 79 77 void startStone(void); //!< Starts with the first stone. 80 78 void createStone(void); 81 79 void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats. 82 bool correctStonePos(TetrisStone* stone ); //!< Check whether the supplied stone is in an allowed position80 bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position 83 81 84 82 PlayerInfo* player_; … … 86 84 WeakPtr<TetrisCenterpoint> center_; //!< The playing field. 87 85 std::vector<TetrisStone*> stones_; //!< A list of all stones in play. 86 std::vector< std::vector<bool> > grid_; 88 87 TetrisStone* activeStone_; 89 88 -
code/branches/tetris/src/modules/tetris/TetrisCenterpoint.cc
r8249 r8537 68 68 SUPER(TetrisCenterpoint, XMLPort, xmlelement, mode); 69 69 70 XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode); 71 XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode); 72 XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode); 70 XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode); // die Breite 71 XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode); // die Grösse 72 XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode); 73 73 XMLPortParam(TetrisCenterpoint, "stoneTemplate", setStoneTemplate, getStoneTemplate, xmlelement, mode); 74 74 XMLPortParam(TetrisCenterpoint, "stoneSpeed", setStoneSpeed, getStoneSpeed, xmlelement, mode); -
code/branches/tetris/src/modules/tetris/TetrisStone.cc
r8249 r8537 37 37 #include "core/XMLPort.h" 38 38 39 #include "Tetris.h" 40 39 41 namespace orxonox 40 42 { … … 52 54 this->delay_ = false; 53 55 this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisStone::enableMovement, this))); 56 this->previousPosition_ = Vector3::ZERO; 57 } 58 59 void TetrisStone::tick(float dt) 60 { 61 SUPER(TetrisStone, tick, dt); 54 62 } 55 63 … … 76 84 { 77 85 const Vector3& position = this->getPosition(); 78 this->setPosition(position.x+value.x/abs(value.x)*this->size_, position.y, position.z); 86 Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z); 87 if(!this->tetris_->isValidMove(this, newPos)) 88 return; 89 90 //this->previousPosition_ = position; 91 this->setPosition(newPos); 79 92 this->delay_ = true; 80 93 this->delayTimer_.startTimer(); -
code/branches/tetris/src/modules/tetris/TetrisStone.h
r8249 r8537 57 57 virtual ~TetrisStone() {} 58 58 59 virtual void tick(float dt); 60 59 61 virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down. 60 62 virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down. … … 75 77 { return this->size_; } 76 78 79 const Vector3& getPreviousPosition() const 80 { return this->previousPosition_; } 81 82 void setGame(Tetris* tetris) 83 { assert(tetris); tetris_ = tetris; } 84 77 85 private: 78 86 void enableMovement(void) … … 82 90 bool delay_; 83 91 Timer delayTimer_; 92 93 Vector3 previousPosition_; 94 Tetris* tetris_; 84 95 }; 85 96 }
Note: See TracChangeset
for help on using the changeset viewer.