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