Changeset 8537 for code/branches/tetris/src/modules/tetris/Tetris.cc
- Timestamp:
- May 23, 2011, 3:06:08 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.