Changeset 8104 for code/branches/tetris/src/modules/pong/Pong.cc
- Timestamp:
- Mar 23, 2011, 7:40:36 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tetris/src/modules/pong/Pong.cc
r7911 r8104 27 27 */ 28 28 29 /** 30 @file Pong.cc 31 @brief Implementation of the Pong class. 32 */ 33 29 34 #include "Pong.h" 30 35 … … 32 37 #include "core/EventIncludes.h" 33 38 #include "core/command/Executor.h" 39 34 40 #include "PongCenterpoint.h" 35 41 #include "PongBall.h" … … 40 46 namespace orxonox 41 47 { 48 // Events to allow to react to scoring of a player, in the level-file. 42 49 CreateEventName(PongCenterpoint, right); 43 50 CreateEventName(PongCenterpoint, left); … … 45 52 CreateUnloadableFactory(Pong); 46 53 54 /** 55 @brief 56 Constructor. Registers and initializes the object. 57 */ 47 58 Pong::Pong(BaseObject* creator) : Deathmatch(creator) 48 59 { … … 56 67 this->setHUDTemplate("PongHUD"); 57 68 69 // Pre-set the timer, but don't start it yet. 58 70 this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this))); 59 71 this->starttimer_.stopTimer(); 60 72 73 // Set the type of Bots for this particular Gametype. 61 74 this->botclass_ = Class(PongBot); 62 75 } 63 76 77 /** 78 @brief 79 Destructor. Cleans up, if initialized. 80 */ 64 81 Pong::~Pong() 65 82 { … … 68 85 } 69 86 87 /** 88 @brief 89 Cleans up the Gametype by destroying the ball and the bats. 90 */ 70 91 void Pong::cleanup() 71 92 { 72 if (this->ball_ )93 if (this->ball_ != NULL) // Destroy the ball, if present. 73 94 { 74 95 this->ball_->destroy(); … … 76 97 } 77 98 99 // Destroy both bats, if present. 78 100 for (size_t i = 0; i < 2; ++i) 79 101 { 80 if (this->bat_[0] )102 if (this->bat_[0] != NULL) 81 103 { 82 104 this->bat_[0]->destroy(); … … 86 108 } 87 109 110 /** 111 @brief 112 Starts the Pong minigame. 113 */ 88 114 void Pong::start() 89 115 { 90 if (this->center_ )91 { 92 if ( !this->ball_)116 if (this->center_ != NULL) // There needs to be a PongCenterpoint, i.e. the area the game takes place. 117 { 118 if (this->ball_ == NULL) // If there is no ball, create a new ball. 93 119 { 94 120 this->ball_ = new PongBall(this->center_); 121 // Apply the template for the ball specified by the centerpoint. 95 122 this->ball_->addTemplate(this->center_->getBalltemplate()); 96 123 } 97 124 125 // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to. 98 126 this->center_->attach(this->ball_); 99 127 this->ball_->setPosition(0, 0, 0); … … 103 131 this->ball_->setBatLength(this->center_->getBatLength()); 104 132 105 if (!this->bat_[0]) 133 // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint. 134 for (size_t i = 0; i < 2; ++i) 106 135 { 107 this->bat_[0] = new PongBat(this->center_); 108 this->bat_[0]->addTemplate(this->center_->getBattemplate()); 136 if (this->bat_[i] == NULL) 137 { 138 this->bat_[i] = new PongBat(this->center_); 139 this->bat_[i]->addTemplate(this->center_->getBattemplate()); 140 } 109 141 } 110 if (!this->bat_[1]) 111 { 112 this->bat_[1] = new PongBat(this->center_); 113 this->bat_[1]->addTemplate(this->center_->getBattemplate()); 114 } 115 142 143 // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to. 116 144 this->center_->attach(this->bat_[0]); 117 145 this->center_->attach(this->bat_[1]); … … 127 155 this->bat_[1]->setLength(this->center_->getBatLength()); 128 156 157 // Set the bats for the ball. 129 158 this->ball_->setBats(this->bat_); 130 159 } 131 else 160 else // If no centerpoint was specified, an error is thrown. 132 161 { 133 162 COUT(1) << "Error: No Centerpoint specified." << std::endl; 134 } 135 163 // TODO: End the game? 164 } 165 166 // Start the timer. After it has expired the ball is started. 136 167 this->starttimer_.startTimer(); 137 168 138 169 // Set variable to temporarily force the player to spawn. 139 170 bool temp = this->bForceSpawn_; 140 171 this->bForceSpawn_ = true; 141 172 173 // Call start for the parent class. 142 174 Deathmatch::start(); 143 175 176 // Reset the variable. 144 177 this->bForceSpawn_ = temp; 145 178 } 146 179 180 /** 181 @brief 182 Ends the Pong minigame. 183 */ 147 184 void Pong::end() 148 185 { 149 186 this->cleanup(); 150 187 188 // Call end for the parent class. 151 189 Deathmatch::end(); 152 190 } 153 191 192 /** 193 @brief 194 Spawns players, and fills the rest up with bots. 195 */ 154 196 void Pong::spawnPlayersIfRequested() 155 197 { … … 164 206 } 165 207 208 /** 209 @brief 210 Spawns the input player. 211 @param player 212 The player tp be spawned. 213 */ 166 214 void Pong::spawnPlayer(PlayerInfo* player) 167 215 { 168 if (!this->bat_[0]->getPlayer()) 216 assert(player); 217 218 // If the first (left) bat has no player. 219 if (this->bat_[0]->getPlayer() == NULL) 169 220 { 170 221 player->startControl(this->bat_[0]); 171 222 this->players_[player].state_ = PlayerState::Alive; 172 223 } 173 else if (!this->bat_[1]->getPlayer()) 224 // If the second (right) bat has no player. 225 else if (this->bat_[1]->getPlayer() == NULL) 174 226 { 175 227 player->startControl(this->bat_[1]); 176 228 this->players_[player].state_ = PlayerState::Alive; 177 229 } 230 // If both bats are taken. 178 231 else 179 232 return; 180 233 181 if (player && player->getController() && player->getController()->isA(Class(PongAI))) 234 // If the player is an AI, it receives a pointer to the ball. 235 if (player->getController() != NULL && player->getController()->isA(Class(PongAI))) 182 236 { 183 237 PongAI* ai = orxonox_cast<PongAI*>(player->getController()); … … 186 240 } 187 241 242 /** 243 @brief 244 Is called when the player scored. 245 */ 188 246 void Pong::playerScored(PlayerInfo* player) 189 247 { 190 248 Deathmatch::playerScored(player); 191 249 192 if (this->center_) 193 { 250 if (this->center_ != NULL) // If there is a centerpoint. 251 { 252 // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks. 194 253 if (player == this->getRightPlayer()) 195 254 this->center_->fireEvent(FireEventName(PongCenterpoint, right)); … … 197 256 this->center_->fireEvent(FireEventName(PongCenterpoint, left)); 198 257 199 if (player) 258 // Also announce, that the player has scored. 259 if (player != NULL) 200 260 this->gtinfo_->sendAnnounceMessage(player->getName() + " scored"); 201 261 } 202 262 203 if (this->ball_) 263 // If there is a ball present, reset its position, velocity and acceleration. 264 if (this->ball_ != NULL) 204 265 { 205 266 this->ball_->setPosition(Vector3::ZERO); … … 209 270 } 210 271 211 if (this->bat_[0] && this->bat_[1]) 272 // If there are bats reset them to the middle position. 273 if (this->bat_[0] != NULL && this->bat_[1] != NULL) 212 274 { 213 275 this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); … … 215 277 } 216 278 279 // Restart the timer to start the ball. 217 280 this->starttimer_.startTimer(); 218 281 } 219 282 283 /** 284 @brief 285 Starts the ball with some default speed. 286 */ 220 287 void Pong::startBall() 221 288 { 222 if (this->ball_ && this->center_)289 if (this->ball_ != NULL && this->center_ != NULL) 223 290 this->ball_->setSpeed(this->center_->getBallSpeed()); 224 291 } 225 292 293 /** 294 @brief 295 Get the left player. 296 @return 297 Returns a pointer to the player playing on the left. If there is no left player, NULL is returned. 298 */ 226 299 PlayerInfo* Pong::getLeftPlayer() const 227 300 { 228 if (this->bat_ && this->bat_[0])301 if (this->bat_ != NULL && this->bat_[0] != NULL) 229 302 return this->bat_[0]->getPlayer(); 230 303 else … … 232 305 } 233 306 307 /** 308 @brief 309 Get the right player. 310 @return 311 Returns a pointer to the player playing on the right. If there is no right player, NULL is returned. 312 */ 234 313 PlayerInfo* Pong::getRightPlayer() const 235 314 { 236 if (this->bat_ && this->bat_[1])315 if (this->bat_ != NULL && this->bat_[1] != NULL) 237 316 return this->bat_[1]->getPlayer(); 238 317 else
Note: See TracChangeset
for help on using the changeset viewer.