Changeset 8104 for code/branches/tetris/src/modules/pong
- Timestamp:
- Mar 23, 2011, 7:40:36 AM (14 years ago)
- Location:
- code/branches/tetris/src/modules/pong
- Files:
-
- 4 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 -
code/branches/tetris/src/modules/pong/Pong.h
r7911 r8104 27 27 */ 28 28 29 /** 30 @file Pong.h 31 @brief Declaration of the Pong class. 32 @ingroup Pong 33 */ 34 29 35 #ifndef _Pong_H__ 30 36 #define _Pong_H__ … … 33 39 34 40 #include "tools/Timer.h" 41 35 42 #include "gametypes/Deathmatch.h" 36 43 37 44 namespace orxonox 38 45 { 46 47 /** 48 @brief 49 Implements a Pong minigame. 50 51 //TODO: Add details to different classes used and how. 52 PongBall, is the ball, PongBats are the things that can be moved by the players (ControllableEntities), PongCenterpoint is the playing field. (x-z area) 53 54 @author 55 Fabian 'x3n' Landau 56 57 @ingroup Pong 58 */ 39 59 class _PongExport Pong : public Deathmatch 40 60 { 41 61 public: 42 Pong(BaseObject* creator); 43 virtual ~Pong(); 62 Pong(BaseObject* creator); //!< Constructor. Registers and initializes the object. 63 virtual ~Pong(); //!< Destructor. Cleans up, if initialized. 44 64 45 virtual void start(); 46 virtual void end(); 65 virtual void start(); //!< Starts the Pong minigame. 66 virtual void end(); ///!< Ends the Pong minigame. 47 67 48 virtual void spawnPlayer(PlayerInfo* player); 68 virtual void spawnPlayer(PlayerInfo* player); //!< Spawns the input player. 49 69 50 virtual void playerScored(PlayerInfo* player); 70 virtual void playerScored(PlayerInfo* player); //!< Is called when the player scored. 51 71 72 /** 73 @brief Set the PongCenterpoint (the playing field). 74 @param center A pointer to the PongCenterpoint to be set. 75 */ 52 76 void setCenterpoint(PongCenterpoint* center) 53 77 { this->center_ = center; } 54 78 55 PlayerInfo* getLeftPlayer() const; 56 PlayerInfo* getRightPlayer() const; 79 PlayerInfo* getLeftPlayer() const; //!< Get the left player. 80 PlayerInfo* getRightPlayer() const; //!< Get the right player. 57 81 58 82 protected: 59 virtual void spawnPlayersIfRequested(); 83 virtual void spawnPlayersIfRequested(); //!< Spawns players, and fills the rest up with bots. 60 84 61 void startBall(); 62 void cleanup(); 85 void startBall(); //!< Starts the ball with some default speed. 86 void cleanup(); //!< Cleans up the Gametype by destroying the ball and the bats. 63 87 64 WeakPtr<PongCenterpoint> center_; 65 WeakPtr<PongBall> ball_; 66 WeakPtr<PongBat> bat_[2]; 67 Timer starttimer_; 88 WeakPtr<PongCenterpoint> center_; //!< The playing field. 89 WeakPtr<PongBall> ball_; //!< The Pong ball. 90 WeakPtr<PongBat> bat_[2]; //!< The two bats. 91 Timer starttimer_; //!< A timer to delay the start of the game. 68 92 }; 69 93 } -
code/branches/tetris/src/modules/pong/PongBall.cc
r7886 r8104 27 27 */ 28 28 29 /** 30 @file PongBall.cc 31 @brief Implementation of the PongBall class. 32 */ 33 29 34 #include "PongBall.h" 30 35 31 36 #include "core/CoreIncludes.h" 32 37 #include "core/GameMode.h" 38 33 39 #include "gametypes/Gametype.h" 40 34 41 #include "PongBat.h" 35 42 … … 40 47 const float PongBall::MAX_REL_Z_VELOCITY = 1.5; 41 48 49 /** 50 @brief 51 Constructor. Registers and initializes the object. 52 */ 42 53 PongBall::PongBall(BaseObject* creator) 43 54 : MovableEntity(creator) … … 57 68 } 58 69 70 /** 71 @brief 72 Destructor. 73 */ 59 74 PongBall::~PongBall() 60 75 { … … 68 83 } 69 84 85 /** 86 @brief 87 Register variables to synchronize over the network. 88 */ 70 89 void PongBall::registerVariables() 71 90 { … … 79 98 } 80 99 100 /** 101 @brief 102 Is called every tick. 103 //TODO: Explain in detail what happens here. 104 @param dt 105 The time since the last tick. 106 */ 81 107 void PongBall::tick(float dt) 82 108 { 83 109 SUPER(PongBall, tick, dt); 84 110 111 // Get the current position, velocity and acceleration of the ball. 85 112 Vector3 position = this->getPosition(); 86 113 Vector3 velocity = this->getVelocity(); 87 114 Vector3 acceleration = this->getAcceleration(); 88 115 116 // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters). 89 117 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 90 118 { 119 // Its velocity in z-direction is inverted (i.e. it bounces off). 91 120 velocity.z = -velocity.z; 121 // And its position is set as to not overstep the boundary it has just crossed. 92 122 if (position.z > this->fieldHeight_ / 2) 93 123 position.z = this->fieldHeight_ / 2; … … 98 128 } 99 129 130 // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry). 100 131 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 101 132 { 102 133 float distance = 0; 103 134 104 if (this->bat_ )135 if (this->bat_ != NULL) // If there are bats. 105 136 { 106 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 137 // If the right boundary has been crossed. 138 if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL) 107 139 { 140 // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) 108 141 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 109 if (fabs(distance) <= 1) 110 { 142 if (fabs(distance) <= 1) // If the bat is there to parry. 143 { 144 // Set the ball to be exactly at the boundary. 111 145 position.x = this->fieldWidth_ / 2; 146 // Invert its veloxity in x-direction (i.e. it bounces off). 112 147 velocity.x = -velocity.x; 148 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 113 149 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 114 150 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; … … 116 152 this->fireEvent(); 117 153 } 154 // If the left player scores. 118 155 else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 119 156 { … … 125 162 } 126 163 } 127 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 164 // If the left boundary has been crossed. 165 else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL) 128 166 { 167 // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) 129 168 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 130 if (fabs(distance) <= 1) 131 { 169 if (fabs(distance) <= 1) // If the bat is there to parry. 170 { 171 // Set the ball to be exactly at the boundary. 132 172 position.x = -this->fieldWidth_ / 2; 173 // Invert its veloxity in x-direction (i.e. it bounces off). 133 174 velocity.x = -velocity.x; 175 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 134 176 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 135 177 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; … … 137 179 this->fireEvent(); 138 180 } 181 // If the right player scores. 139 182 else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 140 183 { … … 149 192 } 150 193 194 // Set the position, velocity and acceleration of the ball, if they have changed. 151 195 if (acceleration != this->getAcceleration()) 152 196 this->setAcceleration(acceleration); … … 157 201 } 158 202 203 /** 204 @brief 205 Set the speed of the ball (in x-direction). 206 @param speed 207 The speed to be set. 208 */ 159 209 void PongBall::setSpeed(float speed) 160 210 { 161 if (speed != this->speed_) 211 if (speed != this->speed_) // If the speed changes 162 212 { 163 213 this->speed_ = speed; 164 214 215 // Set the speed in the direction of the balls current velocity. 165 216 Vector3 velocity = this->getVelocity(); 166 217 if (velocity.x != 0) 167 218 velocity.x = sgn(velocity.x) * this->speed_; 168 else 219 else // If the balls current velocity is zero, the speed is set in a random direction. 169 220 velocity.x = this->speed_ * sgn(rnd(-1,1)); 170 221 … … 173 224 } 174 225 226 /** 227 @brief 228 Set the 229 */ 175 230 void PongBall::setBats(WeakPtr<PongBat>* bats) 176 231 { -
code/branches/tetris/src/modules/pong/PongBall.h
r7885 r8104 27 27 */ 28 28 29 /** 30 @file PongBall.h 31 @brief Declaration of the PongBall class. 32 @ingroup Pong 33 */ 34 29 35 #ifndef _PongBall_H__ 30 36 #define _PongBall_H__ … … 33 39 34 40 #include "util/Math.h" 41 35 42 #include "worldentities/MovableEntity.h" 36 43 37 44 namespace orxonox 38 45 { 46 47 /** 48 @brief 49 This class manages the ball for Pong. 50 51 //TODO: Describe what it's responnsibilities are. 52 Only moves in x-z area. 53 54 @author 55 Fabian 'x3n' Landau 56 57 @ingroup Pong 58 */ 39 59 class _PongExport PongBall : public MovableEntity 40 60 {
Note: See TracChangeset
for help on using the changeset viewer.