Changeset 5929 for code/trunk/src/modules/pong/PongBall.cc
- Timestamp:
- Oct 12, 2009, 8:20:07 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core5 (added) merged: 5768-5769,5772,5775-5780,5783-5785,5791-5792,5795-5807,5809-5814,5816-5832,5836-5839,5842-5853,5855-5899,5904-5922,5924-5928
- Property svn:mergeinfo changed
-
code/trunk/src/modules/pong/PongBall.cc
r5781 r5929 33 33 #include "gametypes/Gametype.h" 34 34 #include "PongBat.h" 35 #include "sound/SoundBase.h"36 35 37 36 namespace orxonox … … 41 40 const float PongBall::MAX_REL_Z_VELOCITY = 1.5; 42 41 43 PongBall::PongBall(BaseObject* creator) : MovableEntity(creator) 42 PongBall::PongBall(BaseObject* creator) 43 : MovableEntity(creator) 44 44 { 45 45 RegisterObject(PongBall); 46 46 47 47 this->speed_ = 0; 48 this->accelerationFactor_ = 1.0f; 48 49 this->bat_ = 0; 49 50 this->batID_ = new unsigned int[2]; … … 53 54 54 55 this->registerVariables(); 56 } 55 57 56 this->sidesound_ = new SoundBase(this); 57 this->sidesound_->loadFile("sounds/pong_side.wav"); 58 59 this->batsound_ = new SoundBase(this); 60 this->batsound_->loadFile("sounds/pong_bat.wav"); 61 62 this->scoresound_ = new SoundBase(this); 63 this->scoresound_->loadFile("sounds/pong_score.wav"); 58 PongBall::~PongBall() 59 { 64 60 } 65 61 … … 79 75 SUPER(PongBall, tick, dt); 80 76 81 if (GameMode::isMaster()) 77 Vector3 position = this->getPosition(); 78 Vector3 velocity = this->getVelocity(); 79 Vector3 acceleration = this->getAcceleration(); 80 81 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 82 82 { 83 Vector3 position = this->getPosition(); 84 Vector3 velocity = this->getVelocity(); 83 velocity.z = -velocity.z; 84 if (position.z > this->fieldHeight_ / 2) 85 position.z = this->fieldHeight_ / 2; 86 if (position.z < -this->fieldHeight_ / 2) 87 position.z = -this->fieldHeight_ / 2; 85 88 86 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 89 this->fireEvent(); 90 } 91 92 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 93 { 94 float distance = 0; 95 96 if (this->bat_) 87 97 { 88 velocity.z = -velocity.z; 89 this->sidesound_->play(); 90 91 if (position.z > this->fieldHeight_ / 2) 92 position.z = this->fieldHeight_ / 2; 93 if (position.z < -this->fieldHeight_ / 2) 94 position.z = -this->fieldHeight_ / 2; 95 } 96 97 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 98 { 99 float distance = 0; 100 101 if (this->bat_) 98 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 102 99 { 103 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 100 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 101 if (fabs(distance) <= 1) 104 102 { 105 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 106 if (fabs(distance) <= 1) 103 position.x = this->fieldWidth_ / 2; 104 velocity.x = -velocity.x; 105 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 106 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; 107 108 this->fireEvent(); 109 } 110 else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 111 { 112 if (this->getGametype() && this->bat_[0]) 107 113 { 108 position.x = this->fieldWidth_ / 2; 109 velocity.x = -velocity.x; 110 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 111 this->batsound_->play(); 112 } 113 else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 114 { 115 if (this->getGametype() && this->bat_[0]) 116 { 117 this->getGametype()->playerScored(this->bat_[0]->getPlayer()); 118 this->scoresound_->play(); 119 return; 120 } 114 this->getGametype()->playerScored(this->bat_[0]->getPlayer()); 115 return; 121 116 } 122 117 } 123 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 118 } 119 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 120 { 121 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 122 if (fabs(distance) <= 1) 124 123 { 125 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 126 if (fabs(distance) <= 1) 124 position.x = -this->fieldWidth_ / 2; 125 velocity.x = -velocity.x; 126 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 127 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; 128 129 this->fireEvent(); 130 } 131 else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 132 { 133 if (this->getGametype() && this->bat_[1]) 127 134 { 128 position.x = -this->fieldWidth_ / 2; 129 velocity.x = -velocity.x; 130 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 131 this->batsound_->play(); 132 } 133 else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 134 { 135 if (this->getGametype() && this->bat_[1]) 136 { 137 this->scoresound_->play(); 138 this->getGametype()->playerScored(this->bat_[1]->getPlayer()); 139 return; 140 } 135 this->getGametype()->playerScored(this->bat_[1]->getPlayer()); 136 return; 141 137 } 142 138 } 143 139 } 144 140 } 141 } 145 142 146 if (velocity != this->getVelocity()) 147 this->setVelocity(velocity); 148 if (position != this->getPosition()) 149 this->setPosition(position); 150 } 151 else 152 { 153 Vector3 position = this->getPosition(); 154 Vector3 velocity = this->getVelocity(); 155 156 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 157 { 158 velocity.z = -velocity.z; 159 this->sidesound_->play(); 160 161 if (position.z > this->fieldHeight_ / 2) 162 position.z = this->fieldHeight_ / 2; 163 if (position.z < -this->fieldHeight_ / 2) 164 position.z = -this->fieldHeight_ / 2; 165 } 166 167 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 168 { 169 float distance = 0; 170 171 if (this->bat_) 172 { 173 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 174 { 175 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 176 if (fabs(distance) <= 1) 177 { 178 position.x = this->fieldWidth_ / 2; 179 velocity.x = -velocity.x; 180 this->batsound_->play(); 181 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 182 } 183 } 184 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 185 { 186 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 187 if (fabs(distance) <= 1) 188 { 189 position.x = -this->fieldWidth_ / 2; 190 velocity.x = -velocity.x; 191 this->batsound_->play(); 192 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 193 } 194 } 195 } 196 } 197 198 if (velocity != this->getVelocity()) 143 if (acceleration != this->getAcceleration()) 144 this->setAcceleration(acceleration); 145 if (velocity != this->getVelocity()) 199 146 this->setVelocity(velocity); 200 147 if (position != this->getPosition()) 201 148 this->setPosition(position); 202 }203 149 } 204 150
Note: See TracChangeset
for help on using the changeset viewer.