Changeset 9326
- Timestamp:
- Jul 22, 2012, 5:06:56 PM (12 years ago)
- Location:
- code/branches/presentation2012merge/src/modules/tetris
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2012merge/src/modules/tetris/Tetris.cc
r9323 r9326 95 95 void Tetris::cleanup() 96 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 }//*/ 101 97 if (this->isInitialized()) 98 { 99 if (this->activeBrick_) 100 { 101 this->activeBrick_->destroy(); 102 this->activeBrick_ = NULL; 103 } 104 if (this->futureBrick_) 105 { 106 this->futureBrick_->destroy(); 107 this->futureBrick_ = NULL; 108 } 109 110 for (std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 111 (*it)->destroy(); 112 this->stones_.clear(); 113 } 102 114 } 103 115 … … 111 123 if(!this->isValidBrickPosition(this->activeBrick_, this->activeBrick_->getPosition())) 112 124 { 125 for (unsigned int i = 0; i < this->activeBrick_->getNumberOfStones(); i++) 126 this->stones_.push_back(this->activeBrick_->getStone(i)); 113 127 this->activeBrick_->setVelocity(Vector3::ZERO); 114 128 this->activeBrick_->releaseStones(this->center_); 115 //delete this->activeBrick_; //releasing the memory116 129 this->findFullRows(); 117 130 if(this->endGameCriteria_ < 0.1f) //end game if two bricks are created within a 0.1s interval. 131 { 118 132 this->end(); 119 this->createBrick(); 133 return; 134 } 120 135 this->startBrick(); 121 136 this->endGameCriteria_ = 0.0f; … … 133 148 return false; 134 149 135 for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 136 { 137 if(stone == *it) 138 continue; 139 150 for(std::list<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 151 { 140 152 const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone 141 153 … … 189 201 190 202 // we use a reverse iterator because we have to check for collisions with the topmost stones first 191 for(std::vector<TetrisStone*>::const_reverse_iterator it = this->stones_.rbegin(); it != this->stones_.rend(); ++it) 192 { 193 if(this->activeBrick_->contains(*it))//skip the moving brick' stones 194 continue; 203 for(std::list<TetrisStone*>::const_reverse_iterator it = this->stones_.rbegin(); it != this->stones_.rend(); ++it) 204 { 195 205 //Vector3 currentStonePosition = rotateVector((*it)->getPosition(), this->activeBrick_->getRotationCount()); 196 206 const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone … … 264 274 if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place. 265 275 { 266 // Create the futureBrick_267 this->createBrick();268 276 // Create the first brick. 269 277 this->createBrick(); … … 350 358 cameraIndex = this->activeBrick_->getCurrentCameraIndex(); 351 359 this->player_->stopControl(); 360 // destroy old active brick 361 this->activeBrick_->destroy(); 352 362 } 353 363 354 364 // Make the last brick to be created the active brick. 355 this->activeBrick_ = this->bricks_.back(); 356 365 this->activeBrick_ = this->futureBrick_; 366 this->futureBrick_ = NULL; 367 368 // set its position 357 369 this->player_->startControl(this->activeBrick_); 370 float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); 371 float yPos = (this->center_->getHeight()-0.5f)*this->center_->getStoneSize(); 372 this->activeBrick_->setPosition(xPos, yPos, 0.0f); 358 373 this->activeBrick_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f); 359 374 this->activeBrick_->setCameraPosition(cameraIndex); 375 376 // create a new future brick 377 this->createBrick(); 360 378 } 361 379 362 380 void Tetris::createBrick(void) //TODO: random rotation offset between 0 and 3 (times 90°) 363 381 { 364 // Use the futureBrick_ as new currentBrick by setting its position and storing it in this->bricks365 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 382 // create new futureBrick_ 377 383 this->futureBrick_ = new TetrisBrick(this->center_); … … 426 432 { 427 433 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()); 434 for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ) 435 { 436 std::list<TetrisStone*>::iterator it_temp = it++; 437 correctPosition = static_cast<unsigned int>(((*it_temp)->getPosition().y - 5)/this->center_->getStoneSize()); 431 438 if(correctPosition == row) 432 439 { … … 447 454 void Tetris::clearRow(unsigned int row) 448 455 {// clear the full row 449 for(std:: vector<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)456 for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ) 450 457 { 451 458 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 459 { 460 (*it)->destroy(); 461 this->stones_.erase(it++); 462 } 463 else 464 ++it; 454 465 } 455 466 // 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>(((*it 2)->getPosition().y - 5)/this->center_->getStoneSize()) > row)459 (*it 2)->setPosition((*it2)->getPosition()-Vector3(0,10,0));467 for(std::list<TetrisStone*>::iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 468 { 469 if(static_cast<unsigned int>(((*it)->getPosition().y - 5)/this->center_->getStoneSize()) > row) 470 (*it)->setPosition((*it)->getPosition()-Vector3(0,10,0)); 460 471 } 461 472 -
code/branches/presentation2012merge/src/modules/tetris/Tetris.h
r9286 r9326 56 56 public: 57 57 Tetris(BaseObject* creator); //!< Constructor. Registers and initializes the object. 58 virtual ~Tetris(); //!< Destructor. Cleans up, if initialized. 58 virtual ~Tetris(); //!< Destructor. Cleans up, if initialized. 59 59 60 60 virtual void start(void); //!< Starts the Tetris minigame. … … 88 88 void clearRow(unsigned int row); 89 89 90 90 91 91 PlayerInfo* player_; 92 92 93 93 WeakPtr<TetrisCenterpoint> center_; //!< The playing field. 94 std::vector<TetrisBrick*> bricks_; //!< A list of all bricks in play. 95 std::vector<TetrisStone*> stones_; //!< A list of all stones in play. 96 std::vector< std::vector<bool> > grid_; 94 std::list<TetrisStone*> stones_; //!< A list of all stones in play. 97 95 TetrisBrick* activeBrick_; 98 96 TetrisBrick* futureBrick_; 99 97 100 98 Timer starttimer_; //!< A timer to delay the start of the game. 101 99 float endGameCriteria_; //<! Works as a timer which is resetted, whenever a brick is created. -
code/branches/presentation2012merge/src/modules/tetris/TetrisBrick.cc
r9286 r9326 175 175 return NULL; 176 176 } 177 //TODO: refactor this function; is not needed if brickstones are added to Tetris::stones_ after collision.178 bool TetrisBrick::contains(TetrisStone* stone)179 {180 for(unsigned int i = 0; i < brickStones_.size(); i++)181 {182 if(stone == brickStones_[i])183 return true;184 }185 return false;186 }187 177 188 178 /** -
code/branches/presentation2012merge/src/modules/tetris/TetrisBrick.h
r9286 r9326 65 65 { return this->brickStones_.size(); } 66 66 TetrisStone* getStone(unsigned int i); 67 bool contains(TetrisStone* stone);68 67 69 68 void setGame(Tetris* tetris)
Note: See TracChangeset
for help on using the changeset viewer.