- Timestamp:
- Mar 21, 2019, 2:19:16 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/OrxoBlox_FS19/src/modules/OrxoBlox/OrxoBloxBall.cc
r12210 r12212 1 2 //TODO: Sounds (all the sounds are still from the pong module...)3 //TODO: Blocks (the Ball-Block comunication is based on how the blocks are implemented)4 //TODO: The bottom boundary/ the Ball collecter5 //TODO: Ability to shoot the ball (the ball is still constructed like the pong ball)6 7 1 /* 8 2 * ORXONOX - the hottest 3D action shooter ever to exist … … 45 39 #include "gametypes/Gametype.h" 46 40 47 #include "OrxoBloxB locks.h"41 #include "OrxoBloxBat.h" 48 42 49 43 #include "sound/WorldSound.h" … … 67 61 this->speed_ = 0; 68 62 this->accelerationFactor_ = 1.0f; 69 this->b lock_ = nullptr;70 this->bDeleteB lock_ = false;71 this->b lockID_ = new unsigned int[100];72 for (int i = 0; i < 100; i++) { 73 this->blockID_[i] = OBJECTID_UNKNOWN;74 } 63 this->bat_ = nullptr; 64 this->bDeleteBats_ = false; 65 this->batID_ = new unsigned int[2]; 66 this->batID_[0] = OBJECTID_UNKNOWN; 67 this->batID_[1] = OBJECTID_UNKNOWN; 68 this->relMercyOffset_ = 0.05f; 75 69 76 70 this->registerVariables(); … … 102 96 if (this->isInitialized()) 103 97 { 104 if (this->bDeleteB lock_)105 delete[] this->b lock_;106 107 delete[] this->b lockID_;98 if (this->bDeleteBats_) 99 delete[] this->bat_; 100 101 delete[] this->batID_; 108 102 } 109 103 } … … 126 120 registerVariable( this->fieldWidth_ ); 127 121 registerVariable( this->fieldHeight_ ); 128 registerVariable( this->b locklength_ );122 registerVariable( this->batlength_ ); 129 123 registerVariable( this->speed_ ); 130 registerVariable( this->blockID_[0] ); 131 registerVariable( this->blockID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBlock) ); 124 registerVariable( this->relMercyOffset_ ); 125 registerVariable( this->batID_[0] ); 126 registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBats) ); 132 127 } 133 128 … … 135 130 @brief 136 131 Is called every tick. 137 Handles the movement of the ball and its interaction with the boundaries and b locks.132 Handles the movement of the ball and its interaction with the boundaries and bats. 138 133 @param dt 139 134 The time since the last tick. … … 148 143 Vector3 acceleration = this->getAcceleration(); 149 144 150 // If the ball has hit the boundaries on either the right side or the left 151 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 152 { 153 defBoundarySound_->play(); //play boundary sound 154 // Its velocity in x-direction is inverted (i.e. it bounces off). 155 velocity.x = -velocity.x; 156 // And its position is set as to not overstep the boundary it has just crossed. 157 if (position.x > this->fieldWidth_ / 2) 158 position.x = this->fieldWidth_ / 2; 159 if (position.x < -this->fieldWidth_ / 2) 160 position.x = -this->fieldWidth_ / 2; 161 162 this->fireEvent(); 163 } 164 165 // If the ball has hit the boundary on the top 166 if (position.z > this->fieldHeight_ / 2) 145 // 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). 146 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 167 147 { 168 148 defBoundarySound_->play(); //play boundary sound … … 170 150 velocity.z = -velocity.z; 171 151 // And its position is set as to not overstep the boundary it has just crossed. 172 position.z = this->fieldHeight_ / 2; 152 if (position.z > this->fieldHeight_ / 2) 153 position.z = this->fieldHeight_ / 2; 154 if (position.z < -this->fieldHeight_ / 2) 155 position.z = -this->fieldHeight_ / 2; 173 156 174 157 this->fireEvent(); 175 158 } 176 159 177 // If the ball has crossed the bottom boundary 178 if (position.z < -this->fieldHeight_ / 2) 179 { 180 //TODO: Ball Collector 160 // 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). 161 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 162 { 163 float distance = 0; 164 165 if (this->bat_ != nullptr) // If there are bats. 166 { 167 // If the right boundary has been crossed. 168 if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != nullptr) 169 { 170 // 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%) 171 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 172 if (fabs(distance) <= 1) // If the bat is there to parry. 173 { 174 defBatSound_->play(); //play bat sound 175 // Set the ball to be exactly at the boundary. 176 position.x = this->fieldWidth_ / 2; 177 // Invert its velocity in x-direction (i.e. it bounces off). 178 velocity.x = -velocity.x; 179 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 180 velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_; 181 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; 182 183 this->fireEvent(); 184 } 185 // If the left player scores. 186 else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 187 { 188 defScoreSound_->play();//play score sound 189 if (this->getGametype() && this->bat_[0]) 190 { 191 this->getGametype()->playerScored(this->bat_[0]->getPlayer()); 192 return; 193 } 194 } 195 } 196 // If the left boundary has been crossed. 197 else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != nullptr) 198 { 199 // 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%) 200 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 201 if (fabs(distance) <= 1) // If the bat is there to parry. 202 { 203 defBatSound_->play(); //play bat sound 204 // Set the ball to be exactly at the boundary. 205 position.x = -this->fieldWidth_ / 2; 206 // Invert its velocity in x-direction (i.e. it bounces off). 207 velocity.x = -velocity.x; 208 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 209 velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_; 210 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; 211 212 this->fireEvent(); 213 } 214 // If the right player scores. 215 else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 216 { 217 defScoreSound_->play();//play score sound 218 if (this->getGametype() && this->bat_[1]) 219 { 220 this->getGametype()->playerScored(this->bat_[1]->getPlayer()); 221 return; 222 } 223 } 224 } 225 } 181 226 } 182 227 … … 215 260 /** 216 261 @brief 217 Set the b locks for the ball.262 Set the bats for the ball. 218 263 @param bats 219 An array (of size n (n=#Blocks) of weak pointers, to be set as the new blocks.220 */ 221 void OrxoBloxBall::setB lock(WeakPtr<OrxoBloxBlock>* block, int n)222 { 223 if (this->bDeleteB lock_) // If there are already some blocks, delete them.224 { 225 delete[] this->b lock_;226 this->bDeleteB lock_ = false;227 } 228 229 this->b lock_ = block;264 An array (of size 2) of weak pointers, to be set as the new bats. 265 */ 266 void OrxoBloxBall::setBats(WeakPtr<OrxoBloxBat>* bats) 267 { 268 if (this->bDeleteBats_) // If there are already some bats, delete them. 269 { 270 delete[] this->bat_; 271 this->bDeleteBats_ = false; 272 } 273 274 this->bat_ = bats; 230 275 // Also store their object IDs, for synchronization. 231 for (int i = 0; i < n; i++) { 232 this->blockID_[i] = this->block_[i]->getObjectID(); 233 } 234 } 235 236 /** 237 @brief 238 Get the blocks over the network. 239 */ 240 void OrxoBloxBall::applyBlock(int n) 241 { 242 // Make space for the blocks, if they don't exist, yet. 243 if (this->block_ == nullptr) 244 { 245 this->block_ = new WeakPtr<OrxoBloxBlock>[n]; 246 this->bDeleteBlock_ = true; 247 } 248 249 for (int i = 0; i < n; i++) { 250 if (this->blockID_[i] != OBJECTID_UNKNOWN) 251 this->bat_[i] = orxonox_cast<OrxoBloxBlock*>(Synchronisable::getSynchronisable(this->blockID_[i])); 252 } 253 } 254 255 void OrxoBloxBall::setDefScoreSound(const std::string &pongSound) 276 this->batID_[0] = this->bat_[0]->getObjectID(); 277 this->batID_[1] = this->bat_[1]->getObjectID(); 278 } 279 280 /** 281 @brief 282 Get the bats over the network. 283 */ 284 void OrxoBloxBall::applyBats() 285 { 286 // Make space for the bats, if they don't exist, yet. 287 if (this->bat_ == nullptr) 288 { 289 this->bat_ = new WeakPtr<OrxoBloxBat>[2]; 290 this->bDeleteBats_ = true; 291 } 292 293 if (this->batID_[0] != OBJECTID_UNKNOWN) 294 this->bat_[0] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[0])); 295 if (this->batID_[1] != OBJECTID_UNKNOWN) 296 this->bat_[1] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[1])); 297 } 298 299 void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound) 256 300 { 257 301 if( defScoreSound_ ) 258 defScoreSound_->setSource( pongSound);302 defScoreSound_->setSource(OrxoBloxSound); 259 303 else 260 304 assert(0); // This should never happen, because soundpointer is only available on master … … 270 314 } 271 315 272 void OrxoBloxBall::setDefBatSound(const std::string & pongSound)316 void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound) 273 317 { 274 318 if( defBatSound_ ) 275 defBatSound_->setSource( pongSound);319 defBatSound_->setSource(OrxoBloxSound); 276 320 else 277 321 assert(0); // This should never happen, because soundpointer is only available on master … … 287 331 } 288 332 289 void OrxoBloxBall::setDefBoundarySound(const std::string & pongSound)333 void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound) 290 334 { 291 335 if( defBoundarySound_ ) 292 defBoundarySound_->setSource( pongSound);336 defBoundarySound_->setSource(OrxoBloxSound); 293 337 else 294 338 assert(0); // This should never happen, because soundpointer is only available on master
Note: See TracChangeset
for help on using the changeset viewer.