Changeset 9286 for code/branches/presentation2012merge/src/modules/tetris
- Timestamp:
- Jun 10, 2012, 5:13:00 PM (12 years ago)
- Location:
- code/branches/presentation2012merge
- Files:
-
- 9 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2012merge
- Property svn:mergeinfo changed
/code/branches/pCuts (added) merged: 9023-9024,9027,9081-9092
- Property svn:mergeinfo changed
-
code/branches/presentation2012merge/src/modules/tetris/CMakeLists.txt
r8706 r9286 3 3 TetrisCenterpoint.cc 4 4 TetrisStone.cc 5 TetrisBrick.cc 6 TetrisScore.cc 5 7 ) 6 8 … … 10 12 LINK_LIBRARIES 11 13 orxonox 14 overlays 12 15 SOURCE_FILES ${TETRIS_SRC_FILES} 13 16 ) -
code/branches/presentation2012merge/src/modules/tetris/Tetris.cc
r8858 r9286 23 23 * ... 24 24 * Co-authors: 25 * ... 26 * 25 * Johannes Ritz 26 * 27 * 28 *BUG c) destroying the old stones causes segfault -> WeakPointer as solution ? 29 *BUG d) wrong collision detection: sometimes stones "bounce off" 30 * 31 * 32 *TASK c) end the game in a nicer way 33 *TASK d) save the highscore 34 *TASK e) eye candy 27 35 */ 28 36 … … 42 50 #include "TetrisCenterpoint.h" 43 51 #include "TetrisStone.h" 52 #include "TetrisBrick.h" 44 53 #include "infos/PlayerInfo.h" 45 54 … … 52 61 @brief 53 62 Constructor. Registers and initializes the object. 63 @ingroup Tetris 54 64 */ 55 65 Tetris::Tetris(BaseObject* creator) : Deathmatch(creator) … … 57 67 RegisterObject(Tetris); 58 68 59 this->active Stone_ = NULL;69 this->activeBrick_ = NULL; 60 70 61 71 // Pre-set the timer, but don't start it yet. 62 this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Tetris::start Stone, this)));72 this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Tetris::startBrick, this))); 63 73 this->starttimer_.stopTimer(); 64 74 65 75 this->player_ = NULL; 76 this->endGameCriteria_ = 0.0f; 77 this->setHUDTemplate("TetrisHUD"); 78 this->futureBrick_ = NULL; 66 79 } 67 80 … … 82 95 void Tetris::cleanup() 83 96 { 97 /*for(int i = 0;i < this->stones_.size(); i++) //TODO: Why isn't there any code like this 98 { // compensating the 'new' statement? 99 delete this->stones_[i]; 100 }//*/ 84 101 85 102 } … … 89 106 SUPER(Tetris, tick, dt); 90 107 91 if(this->activeStone_ != NULL) 92 { 93 if(!this->isValidStonePosition(this->activeStone_, this->activeStone_->getPosition())) 94 { 95 this->activeStone_->setVelocity(Vector3::ZERO); 96 this->createStone(); 97 this->startStone(); 108 if((this->activeBrick_ != NULL)&&(!this->hasEnded())) 109 { 110 this->endGameCriteria_ += dt; 111 if(!this->isValidBrickPosition(this->activeBrick_, this->activeBrick_->getPosition())) 112 { 113 this->activeBrick_->setVelocity(Vector3::ZERO); 114 this->activeBrick_->releaseStones(this->center_); 115 //delete this->activeBrick_; //releasing the memory 116 this->findFullRows(); 117 if(this->endGameCriteria_ < 0.1f) //end game if two bricks are created within a 0.1s interval. 118 this->end(); 119 this->createBrick(); 120 this->startBrick(); 121 this->endGameCriteria_ = 0.0f; 98 122 } 99 123 } … … 123 147 } 124 148 149 /** 150 @brief 151 Check for each stone in a brick if it is moved the right way. 152 */ 153 bool Tetris::isValidMove(TetrisBrick* brick, const Vector3& position, bool isRotation = false) 154 { 155 assert(brick); 156 157 for (unsigned int i = 0; i < brick->getNumberOfStones(); i++ ) 158 { 159 TetrisStone* stone = brick->getStone(i); 160 Vector3 stonePosition; //the current stone's offset to position 161 if(isRotation) 162 stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount()+1); 163 else 164 stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount()); 165 166 if(! this->isValidMove(stone, position + stonePosition )) 167 { 168 return false; 169 } 170 171 //catch illegal rotation (such that collisions with ground are not permitted) 172 if(isRotation) 173 { 174 if((position + stonePosition).y < this->center_->getStoneSize()/2.0f) //!< If the stone has reached the bottom of the level 175 { 176 return false; 177 } 178 } 179 } 180 return true; 181 182 } 183 184 185 125 186 bool Tetris::isValidStonePosition(TetrisStone* stone, const Vector3& position) 126 187 { … … 130 191 for(std::vector<TetrisStone*>::const_reverse_iterator it = this->stones_.rbegin(); it != this->stones_.rend(); ++it) 131 192 { 132 if( stone == *it)193 if(this->activeBrick_->contains(*it))//skip the moving brick' stones 133 194 continue; 134 195 //Vector3 currentStonePosition = rotateVector((*it)->getPosition(), this->activeBrick_->getRotationCount()); 135 196 const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone 136 197 //!< Saves the position of the currentStone 198 199 //filter out cases where the falling stone is already below a steady stone 200 if(position.y < currentStonePosition.y - this->center_->getStoneSize()/2.0f) 201 continue; 137 202 if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize())) 138 203 { 139 this->activeStone_->setPosition(Vector3(this->activeStone_->getPosition().x, currentStonePosition.y+this->center_->getStoneSize(), this->activeStone_->getPosition().z)); 204 int y_offset = static_cast<int>((this->activeBrick_->getPosition().y-currentStonePosition.y+10)/10)*10 + currentStonePosition.y; 205 if(y_offset < 0) //filter out extreme cases (very rare bug) 206 y_offset = 0; 207 this->activeBrick_->setPosition(Vector3(this->activeBrick_->getPosition().x, y_offset, this->activeBrick_->getPosition().z)); 140 208 return false; 141 209 }// This case applies if the stones overlap partially vertically … … 145 213 if(position.y < this->center_->getStoneSize()/2.0f) //!< If the stone has reached the bottom of the level 146 214 { 147 stone->setPosition(Vector3(stone->getPosition().x, this->center_->getStoneSize()/2.0f, stone->getPosition().z)); 215 int yOffset = stone->getPosition().y + this->center_->getStoneSize()/2.0f;//calculate offset 216 if(yOffset < 0) //catch brake-throughs 217 yOffset = 0; 218 this->activeBrick_->setPosition(Vector3(this->activeBrick_->getPosition().x, yOffset, this->activeBrick_->getPosition().z)); 148 219 return false; 149 220 } … … 151 222 return true; 152 223 } 224 /** 225 * @brief This function determines wether a brick touches another brick or the ground. 226 * 227 */ 228 bool Tetris::isValidBrickPosition(TetrisBrick* brick, const Vector3& position) 229 { 230 assert(brick); 231 232 for (unsigned int i = 0; i < brick->getNumberOfStones(); i++ ) 233 { 234 TetrisStone* stone = brick->getStone(i); 235 Vector3 stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount()); 236 if(! this->isValidStonePosition(stone, position + stonePosition) ) 237 return false; 238 } 239 return true; 240 } 241 242 /** 243 @brief 244 A Vector3 is rolled 90 * degrees * amount (anticlockwise rotation) 245 */ 246 Vector3 Tetris::rotateVector(Vector3 position, unsigned int amount) 247 { 248 int temp = 0; 249 for(unsigned int i = 0; i < amount; i++) 250 { 251 temp = position.x; 252 position.x = -position.y; 253 position.y = temp; 254 } 255 return position; 256 } 153 257 154 258 /** … … 160 264 if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place. 161 265 { 162 // Create the first stone. 163 this->createStone(); 266 // Create the futureBrick_ 267 this->createBrick(); 268 // Create the first brick. 269 this->createBrick(); 164 270 } 165 271 else // If no centerpoint was specified, an error is thrown and the level is exited. … … 190 296 void Tetris::end() 191 297 { 298 this->activeBrick_->setVelocity(Vector3::ZERO); 299 if(this->activeBrick_ != NULL) 300 { 301 this->player_->stopControl(); 302 } 303 192 304 this->cleanup(); 193 305 … … 225 337 } 226 338 227 /** 228 @brief 229 Starts the first stone. 230 */ 231 void Tetris::startStone(void) 339 340 341 void Tetris::startBrick(void) 232 342 { 233 343 if(this->player_ == NULL) … … 235 345 236 346 unsigned int cameraIndex = 0; 237 if(this->active Stone_ != NULL)347 if(this->activeBrick_ != NULL) 238 348 { 239 349 // Get camera settings 240 cameraIndex = this->active Stone_->getCurrentCameraIndex();350 cameraIndex = this->activeBrick_->getCurrentCameraIndex(); 241 351 this->player_->stopControl(); 242 352 } 243 244 // Make the last stone to be created the active stone. 245 this->activeStone_ = this->stones_.back(); 246 247 this->player_->startControl(this->activeStone_); 248 this->activeStone_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f); 249 this->activeStone_->setCameraPosition(cameraIndex); 250 } 251 252 /** 253 @brief 254 Creates a new stone. 255 */ 256 void Tetris::createStone(void) 257 { 258 // Create a new stone and add it to the list of stones. 259 TetrisStone* stone = new TetrisStone(this->center_); 260 this->stones_.push_back(stone); 261 353 354 // Make the last brick to be created the active brick. 355 this->activeBrick_ = this->bricks_.back(); 356 357 this->player_->startControl(this->activeBrick_); 358 this->activeBrick_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f); 359 this->activeBrick_->setCameraPosition(cameraIndex); 360 } 361 362 void Tetris::createBrick(void) //TODO: random rotation offset between 0 and 3 (times 90°) 363 { 364 // Use the futureBrick_ as new currentBrick by setting its position and storing it in this->bricks 365 if(this->futureBrick_ != NULL) 366 { 367 for (unsigned int i = 0; i < this->futureBrick_->getNumberOfStones(); i++) 368 { 369 this->stones_.push_back(this->futureBrick_->getStone(i)); 370 } 371 float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); 372 float yPos = (this->center_->getHeight()-0.5f)*this->center_->getStoneSize(); 373 this->futureBrick_->setPosition(xPos, yPos, 0.0f); 374 this->bricks_.push_back(this->futureBrick_); 375 } 376 // create new futureBrick_ 377 this->futureBrick_ = new TetrisBrick(this->center_); 378 379 262 380 // Apply the stone template to the stone. 263 stone->addTemplate(this->center_->getStoneTemplate()); 264 265 // Attach the stone to the Centerpoint and set the position of the stone to be at the top middle. 266 this->center_->attach(stone); 267 float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); 268 float yPos = (this->center_->getHeight()-0.5f)*this->center_->getStoneSize(); 269 stone->setPosition(xPos, yPos, 0.0f); 270 stone->setGame(this); 271 } 381 this->futureBrick_->addTemplate(this->center_->getBrickTemplate()); 382 383 // Attach the brick to the Centerpoint and set the position of the brick to be at the left side. 384 this->center_->attach(this->futureBrick_); 385 float xPos = (this->center_->getWidth()*1.6 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); 386 float yPos = (this->center_->getHeight()-5.1f)*this->center_->getStoneSize(); 387 this->futureBrick_->setPosition(xPos, yPos, 0.0f); 388 this->futureBrick_->setGame(this); 389 } 390 272 391 273 392 /** … … 282 401 } 283 402 403 /*TetrisCenterpoint* Tetris::getCenterpoint(void) const 404 { 405 return this->center_; 406 }*/ 407 284 408 /** 285 409 @brief Set the TetrisCenterpoint (the playing field). … … 291 415 } 292 416 417 /** 418 @brief Check each row if it is full. Removes all full rows. Update 419 @brief Manages score. 420 */ 421 void Tetris::findFullRows() 422 { 423 unsigned int correctPosition = 0; 424 unsigned int stonesPerRow = 0; 425 for (unsigned int row = 0; row < this->center_->getHeight(); row++) 426 { 427 stonesPerRow = 0; 428 for(std::vector<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 429 { 430 correctPosition = static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize()); 431 if(correctPosition == row) 432 { 433 stonesPerRow++; 434 if(stonesPerRow == this->center_->getWidth()) 435 { 436 clearRow(row); 437 row--; //the row counter has to be decreased in order to detect multiple rows! 438 this->playerScored(this->player_);// add points 439 //increase the stone's speed 440 this->center_->setStoneSpeed(this->center_->getStoneSpeed()+1.0f); 441 } 442 } 443 } 444 } 445 } 446 447 void Tetris::clearRow(unsigned int row) 448 {// clear the full row 449 for(std::vector<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 450 { 451 if(static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize()) == row) 452 (*it)->setPosition(Vector3(-50,-50,100)); 453 //{(*it)->destroy(); this->stones_.erase(it); orxout()<< "destroy row "<<endl;}//experimental 454 } 455 // adjust height of stones above the deleted row //TODO: check if this could be a source of a bug. 456 for(std::vector<TetrisStone*>::iterator it2 = this->stones_.begin(); it2 != this->stones_.end(); ++it2) 457 { 458 if(static_cast<unsigned int>(((*it2)->getPosition().y - 5)/this->center_->getStoneSize()) > row) 459 (*it2)->setPosition((*it2)->getPosition()-Vector3(0,10,0)); 460 } 461 462 } 463 464 293 465 } -
code/branches/presentation2012merge/src/modules/tetris/Tetris.h
r8706 r9286 68 68 69 69 PlayerInfo* getPlayer(void) const; //!< Get the player. 70 WeakPtr<TetrisCenterpoint> getCenterpoint(void) 71 { return this->center_; } 70 72 71 73 bool isValidMove(TetrisStone* stone, const Vector3& position); 74 bool isValidMove(TetrisBrick* brick, const Vector3& position, bool isRotation); 75 Vector3 rotateVector(Vector3 position, unsigned int amount); 72 76 73 77 protected: 74 78 virtual void spawnPlayersIfRequested(); //!< Spawns player. 75 79 80 76 81 private: 77 void start Stone(void); //!< Starts with the first stone.78 void create Stone(void);82 void startBrick(void); 83 void createBrick(void); 79 84 void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats. 80 85 bool isValidStonePosition(TetrisStone* stone, const Vector3& position); 86 bool isValidBrickPosition(TetrisBrick* brick, const Vector3& position); 87 void findFullRows(void); 88 void clearRow(unsigned int row); 89 81 90 82 91 PlayerInfo* player_; 83 92 84 93 WeakPtr<TetrisCenterpoint> center_; //!< The playing field. 94 std::vector<TetrisBrick*> bricks_; //!< A list of all bricks in play. 85 95 std::vector<TetrisStone*> stones_; //!< A list of all stones in play. 86 96 std::vector< std::vector<bool> > grid_; 87 TetrisStone* activeStone_; 97 TetrisBrick* activeBrick_; 98 TetrisBrick* futureBrick_; 88 99 89 100 Timer starttimer_; //!< A timer to delay the start of the game. 101 float endGameCriteria_; //<! Works as a timer which is resetted, whenever a brick is created. 90 102 }; 91 103 } -
code/branches/presentation2012merge/src/modules/tetris/TetrisCenterpoint.cc
r8706 r9286 30 30 @file TetrisCenterpoint.cc 31 31 @brief Implementation of the TetrisCenterpoint class. 32 @ingroup Tetris 32 33 */ 33 34 … … 55 56 this->stoneSize_ = 10.0f; 56 57 this->stoneTemplate_ = ""; 58 this->brickTemplate_ = ""; 57 59 this->stoneSpeed_ = 20.0f; 58 60 … … 69 71 70 72 XMLPortParam(TetrisCenterpoint, "width", setWidth, getWidth, xmlelement, mode); // die Breite 71 XMLPortParam(TetrisCenterpoint, "height", setHeight, setWidth, xmlelement, mode); // die Grösse73 XMLPortParam(TetrisCenterpoint, "height", setHeight, getHeight, xmlelement, mode); // die Grösse //was sollte das mit setWidth?? 72 74 XMLPortParam(TetrisCenterpoint, "stoneSize", setStoneSize, getStoneSize, xmlelement, mode); 73 75 XMLPortParam(TetrisCenterpoint, "stoneTemplate", setStoneTemplate, getStoneTemplate, xmlelement, mode); 76 XMLPortParam(TetrisCenterpoint, "brickTemplate", setBrickTemplate, getBrickTemplate, xmlelement, mode); 74 77 XMLPortParam(TetrisCenterpoint, "stoneSpeed", setStoneSpeed, getStoneSpeed, xmlelement, mode); 75 78 } -
code/branches/presentation2012merge/src/modules/tetris/TetrisCenterpoint.h
r8706 r9286 45 45 46 46 namespace orxonox 47 { 47 {//idea: add 2 triggers to the centerpoint (one to determine when a box would go above the centerpoint; 48 //the other to find out when the lowest row is filled totally) 48 49 49 50 /** … … 118 119 119 120 /** 121 @brief Set the template for the bricks. 122 @param template The template name to be applied to each brick. 123 */ 124 void setBrickTemplate(const std::string& templateName) 125 { this->brickTemplate_ = templateName; } 126 /** 127 @brief Get the template for the bricks. 128 @return Returns the template name to be applied to each brick. 129 */ 130 const std::string& getBrickTemplate(void) const 131 { return this->brickTemplate_; } 132 133 /** 120 134 @brief Set the speed of the stones. 121 135 @param speed The speed to be set. … … 137 151 float stoneSize_; 138 152 std::string stoneTemplate_; 153 std::string brickTemplate_; 139 154 float stoneSpeed_; 140 155 -
code/branches/presentation2012merge/src/modules/tetris/TetrisPrereqs.h
r8706 r9286 68 68 class TetrisCenterpoint; 69 69 class TetrisStone; 70 class TetrisBrick; 71 class TetrisScore; 70 72 } 71 73 -
code/branches/presentation2012merge/src/modules/tetris/TetrisStone.cc
r8706 r9286 30 30 @file TetrisStone.cc 31 31 @brief Implementation of the TetrisStone class. 32 @ingroup Tetris 32 33 */ 33 34 … … 47 48 Constructor. Registers and initializes the object. 48 49 */ 49 TetrisStone::TetrisStone(BaseObject* creator) : ControllableEntity(creator)50 TetrisStone::TetrisStone(BaseObject* creator) : Pawn(creator) 50 51 { 51 52 RegisterObject(TetrisStone); 52 53 53 54 this->size_ = 10.0f; 54 55 this->delay_ = false; 55 56 this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisStone::enableMovement, this))); 57 this->lockRotation_ = false; 58 56 59 } 57 60 … … 64 67 void TetrisStone::moveFrontBack(const Vector2& value) 65 68 { 66 if(value.x < 0) 69 if(value.x < 0) //speedup on key down 67 70 { 68 71 this->setVelocity(this->getVelocity()*1.1); 72 } 73 else if(!this->lockRotation_) //rotate when key up is pressed 74 { 75 this->lockRotation_ = true; // multiple calls of this function have to be filtered out. 76 this->rotationTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&TetrisStone::unlockRotation, this))); 77 Quaternion q(Degree(90), Vector3::UNIT_Z); 78 this->setOrientation(this->getOrientation()*q); //rotation: roll 90° 79 69 80 } 70 81 } -
code/branches/presentation2012merge/src/modules/tetris/TetrisStone.h
r8706 r9286 38 38 #include "tetris/TetrisPrereqs.h" 39 39 40 #include "worldentities/ ControllableEntity.h"40 #include "worldentities/pawns/Pawn.h" 41 41 #include "tools/Timer.h" 42 42 … … 51 51 @ingroup Tetris 52 52 */ 53 class _TetrisExport TetrisStone : public ControllableEntity53 class _TetrisExport TetrisStone : public Pawn 54 54 { 55 55 public: … … 81 81 void enableMovement(void) 82 82 { this->delay_ = false; } 83 83 void unlockRotation(void) 84 { this->lockRotation_ = false; } 85 84 86 float size_; //!< The dimensions a stone has in the game world. 85 87 bool delay_; 88 bool lockRotation_; 86 89 Timer delayTimer_; 90 Timer rotationTimer_; ///!< This timer is used to filter out multiple rotation inputs. 87 91 88 92 Tetris* tetris_;
Note: See TracChangeset
for help on using the changeset viewer.