Changeset 8563 for code/branches/tetris/src
- Timestamp:
- May 24, 2011, 9:07:33 PM (14 years ago)
- Location:
- code/branches/tetris/src/modules/tetris
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tetris/src/modules/tetris/Tetris.cc
r8537 r8563 87 87 SUPER(Tetris, tick, dt); 88 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) 89 if(this->activeStone_ != NULL) 90 { 91 std::pair<bool, TetrisStone*> valid = this->isValidMove(this->activeStone_, this->activeStone_->getPosition()); 92 if(!valid.first) 93 { 94 this->activeStone_->setVelocity(Vector3::ZERO); 95 if(valid.second != NULL) 96 { 97 Vector3 position = Vector3(this->activeStone_->getPosition().x, valid.second->getPosition().y+this->center_->getStoneSize(), this->activeStone_->getPosition().z); 98 this->activeStone_->setPosition(position); 99 } 100 this->createStone(); 101 this->startStone(); 102 } 103 } 104 } 105 106 std::pair<bool, TetrisStone*> Tetris::isValidMove(TetrisStone* stone, const Vector3& position) 99 107 { 100 108 assert(stone); 109 110 std::pair<bool, TetrisStone*> valid = std::pair<bool, TetrisStone*>(true, NULL); 101 111 102 112 if(position.x < this->center_->getStoneSize()/2.0) //!< If the stone touches the left edge of the level 103 returnfalse;113 valid.first = false; 104 114 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); 115 valid.first = false; 116 else if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level 117 { 118 valid.first = false; 119 stone->setPosition(Vector3(stone->getPosition().x, this->center_->getStoneSize()/2.0, stone->getPosition().z)); 120 } 121 122 for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) 123 { 124 if(stone == *it) 125 continue; 126 127 const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone 128 129 if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y)) 130 { 131 stone->setVelocity(Vector3::ZERO); 132 this->createStone(); 133 this->startStone(); 134 valid.first = false; 135 return valid; 136 }// This case applies if the stones overlap completely 137 else if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize())) 138 { 139 valid.first = false; 140 valid.second = *it; 141 return valid; 142 }// This case applies if the stones overlap partially vertically 143 } 144 145 return valid; 117 146 } 118 147 … … 232 261 /** 233 262 @brief 234 Validate the stone position.235 @return236 Returns whether the supplied stone is in the correct position.237 */238 bool Tetris::correctStonePos(TetrisStone* stone, const Vector3& position)239 {240 assert(stone);241 242 for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)243 {244 if(stone == *it)245 continue;246 247 Vector3 currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone248 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 completely256 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 vertically260 }261 262 return true;263 }264 265 /**266 @brief267 263 Get the player. 268 264 @return … … 281 277 { 282 278 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 279 } 292 280 -
code/branches/tetris/src/modules/tetris/Tetris.h
r8537 r8563 69 69 PlayerInfo* getPlayer(void) const; //!< Get the player. 70 70 71 boolisValidMove(TetrisStone* stone, const Vector3& position);71 std::pair<bool, TetrisStone*> isValidMove(TetrisStone* stone, const Vector3& position); 72 72 73 73 protected: … … 78 78 void createStone(void); 79 79 void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats. 80 bool correctStonePos(TetrisStone* stone, const Vector3& position); //!< Check whether the supplied stone is in an allowed position81 80 82 81 PlayerInfo* player_; -
code/branches/tetris/src/modules/tetris/TetrisStone.cc
r8537 r8563 54 54 this->delay_ = false; 55 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);62 56 } 63 57 … … 70 64 void TetrisStone::moveFrontBack(const Vector2& value) 71 65 { 72 66 if(value.x < 0) 67 { 68 this->setVelocity(this->getVelocity()*1.1); 69 } 73 70 } 74 71 … … 85 82 const Vector3& position = this->getPosition(); 86 83 Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z); 87 if(!this->tetris_->isValidMove(this, newPos) )84 if(!this->tetris_->isValidMove(this, newPos).first) 88 85 return; 89 86 -
code/branches/tetris/src/modules/tetris/TetrisStone.h
r8537 r8563 57 57 virtual ~TetrisStone() {} 58 58 59 virtual void tick(float dt);60 61 59 virtual void moveFrontBack(const Vector2& value); //!< Overloaded the function to steer the bat up and down. 62 60 virtual void moveRightLeft(const Vector2& value); //!< Overloaded the function to steer the bat up and down. … … 77 75 { return this->size_; } 78 76 79 const Vector3& getPreviousPosition() const80 { return this->previousPosition_; }81 82 77 void setGame(Tetris* tetris) 83 78 { assert(tetris); tetris_ = tetris; } … … 91 86 Timer delayTimer_; 92 87 93 Vector3 previousPosition_;94 88 Tetris* tetris_; 95 89 };
Note: See TracChangeset
for help on using the changeset viewer.