[8087] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Maurus Kaufmann |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "SpaceBoundaries.h" |
---|
| 30 | |
---|
[8110] | 31 | #include "worldentities/MobileEntity.h" |
---|
[8164] | 32 | #include "worldentities/ControllableEntity.h" |
---|
[8110] | 33 | #include "core/ObjectListIterator.h" |
---|
[8201] | 34 | #include "core/XMLPort.h" |
---|
| 35 | #include "worldentities/pawns/Pawn.h" |
---|
| 36 | #include "infos/PlayerInfo.h" |
---|
| 37 | #include "interfaces/RadarViewable.h" |
---|
[8244] | 38 | #include "graphics/Billboard.h" |
---|
[8087] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateFactory(SpaceBoundaries); |
---|
| 43 | |
---|
| 44 | SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator) |
---|
| 45 | { |
---|
[8201] | 46 | /* Standardwerte, die zum Tragen kommen, |
---|
| 47 | * falls im XML-File keine Werte spezifiziert wurden. */ |
---|
| 48 | this->setMaxDistance(3000); |
---|
[8468] | 49 | this->setWarnDistance(this->getMaxDistance()); |
---|
| 50 | this->setShowDistance(this->getMaxDistance()); |
---|
[8404] | 51 | this->setReaction(0); |
---|
[8201] | 52 | |
---|
[8087] | 53 | RegisterObject(SpaceBoundaries); |
---|
[8201] | 54 | |
---|
[8110] | 55 | // Show Boundaries on the radar. |
---|
[8201] | 56 | this->centerRadar_ = new RadarViewable(this, this); |
---|
| 57 | this->centerRadar_->setRadarObjectShape(RadarViewable::Dot); |
---|
| 58 | this->centerRadar_->setRadarVisibility(false); |
---|
[8087] | 59 | } |
---|
| 60 | SpaceBoundaries::~SpaceBoundaries() |
---|
| 61 | { |
---|
[8201] | 62 | delete this->centerRadar_; |
---|
[8244] | 63 | |
---|
[8281] | 64 | this->pawnsIn_.clear(); |
---|
| 65 | |
---|
[8301] | 66 | for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++) |
---|
| 67 | { |
---|
| 68 | if( current->billy != NULL) |
---|
| 69 | { |
---|
| 70 | delete current->billy; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | this->billboards_.clear(); |
---|
[8087] | 74 | } |
---|
[8110] | 75 | |
---|
[8281] | 76 | void SpaceBoundaries::checkWhoIsIn() |
---|
| 77 | { |
---|
| 78 | pawnsIn_.clear(); |
---|
| 79 | for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current) |
---|
| 80 | { |
---|
| 81 | Pawn* currentPawn = *current; |
---|
[8468] | 82 | if( this->reaction_ == 0 ) |
---|
[8281] | 83 | { |
---|
[8468] | 84 | float distance = this->computeDistance(currentPawn); |
---|
| 85 | if(distance <= this->maxDistance_) |
---|
| 86 | { |
---|
| 87 | pawnsIn_.push_back(currentPawn); |
---|
| 88 | } |
---|
[8513] | 89 | } else if (this->reaction_ == 2) { |
---|
| 90 | float distance = this->computeDistance(currentPawn); |
---|
| 91 | if(distance >= this->maxDistance_) |
---|
| 92 | { |
---|
| 93 | pawnsIn_.push_back(currentPawn); |
---|
| 94 | } |
---|
[8468] | 95 | } else { |
---|
[8281] | 96 | pawnsIn_.push_back(currentPawn); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
[8301] | 101 | void SpaceBoundaries::positionBillboard(const Vector3 position) |
---|
| 102 | { |
---|
| 103 | std::vector<billboardAdministration>::iterator current; |
---|
| 104 | for( current = this->billboards_.begin(); current != this->billboards_.end(); current++) |
---|
| 105 | { |
---|
| 106 | if(!current->usedYet) |
---|
| 107 | { |
---|
| 108 | break; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | if( current == this->billboards_.end() ) |
---|
| 112 | { |
---|
| 113 | Billboard *tmp = new Billboard(this); |
---|
[8404] | 114 | this->setBillboardOptions( tmp ); |
---|
[8301] | 115 | tmp->setPosition(position); |
---|
| 116 | billboardAdministration tmp2 = { true, tmp }; |
---|
| 117 | this->billboards_.push_back( tmp2 ); |
---|
| 118 | |
---|
| 119 | } else { |
---|
| 120 | current->billy->setPosition(position); |
---|
| 121 | current->billy->setVisible(true); |
---|
| 122 | current->usedYet = true; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void SpaceBoundaries::setBillboardOptions(Billboard *billy) |
---|
| 127 | { |
---|
| 128 | if(billy != NULL) |
---|
| 129 | { |
---|
[8513] | 130 | billy->setMaterial("Grid"); |
---|
[8301] | 131 | billy->setVisible(true); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void SpaceBoundaries::removeAllBillboards() |
---|
| 136 | { |
---|
| 137 | for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ ) |
---|
| 138 | { |
---|
| 139 | current->usedYet = false; |
---|
| 140 | current->billy->setVisible(false); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
[8110] | 144 | void SpaceBoundaries::setMaxDistance(float r) |
---|
| 145 | { |
---|
[8166] | 146 | this->maxDistance_ = r; |
---|
[8110] | 147 | } |
---|
| 148 | float SpaceBoundaries::getMaxDistance() |
---|
| 149 | { |
---|
[8166] | 150 | return this->maxDistance_; |
---|
[8110] | 151 | } |
---|
| 152 | |
---|
| 153 | void SpaceBoundaries::setWarnDistance(float r) |
---|
| 154 | { |
---|
[8166] | 155 | this->warnDistance_ = r; |
---|
[8110] | 156 | } |
---|
| 157 | float SpaceBoundaries::getWarnDistance() |
---|
| 158 | { |
---|
[8166] | 159 | return this->warnDistance_; |
---|
[8110] | 160 | } |
---|
[8201] | 161 | |
---|
[8244] | 162 | void SpaceBoundaries::setShowDistance(float r) |
---|
| 163 | { |
---|
| 164 | this->showDistance_ = r; |
---|
| 165 | } |
---|
| 166 | float SpaceBoundaries::getShowDistance() |
---|
| 167 | { |
---|
| 168 | return this->showDistance_; |
---|
| 169 | } |
---|
| 170 | |
---|
[8201] | 171 | void SpaceBoundaries::setHealthDecrease(float amount) |
---|
| 172 | { |
---|
| 173 | this->healthDecrease_ = amount/1000; |
---|
| 174 | } |
---|
| 175 | float SpaceBoundaries::getHealthDecrease() |
---|
| 176 | { |
---|
| 177 | return this->healthDecrease_; |
---|
| 178 | } |
---|
[8404] | 179 | |
---|
| 180 | void SpaceBoundaries::setReaction(int mode) |
---|
| 181 | { |
---|
| 182 | this->reaction_ = mode; |
---|
| 183 | } |
---|
| 184 | int SpaceBoundaries::getReaction() |
---|
| 185 | { |
---|
| 186 | return this->reaction_; |
---|
| 187 | } |
---|
[8087] | 188 | |
---|
| 189 | void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 190 | { |
---|
[8110] | 191 | SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); |
---|
[8087] | 192 | |
---|
[8110] | 193 | XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); |
---|
| 194 | XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); |
---|
[8468] | 195 | XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode); |
---|
[8201] | 196 | XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); |
---|
[8404] | 197 | XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode); |
---|
[8087] | 198 | } |
---|
[8110] | 199 | |
---|
| 200 | void SpaceBoundaries::tick(float dt) |
---|
| 201 | { |
---|
[8404] | 202 | this->checkWhoIsIn(); |
---|
[8301] | 203 | this->removeAllBillboards(); |
---|
[8468] | 204 | COUT(4) << "Groesse der Pawn-Liste 'SpaceBoundaries::pawnsIn_': " << (int) pawnsIn_.size() << std::endl; |
---|
[8281] | 205 | |
---|
[8301] | 206 | float distance; |
---|
| 207 | bool humanItem; |
---|
[8404] | 208 | for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ ) |
---|
[8110] | 209 | { |
---|
[8404] | 210 | Pawn* currentPawn = current->get(); |
---|
| 211 | if( currentPawn && currentPawn->getNode() ) |
---|
[8110] | 212 | { |
---|
[8404] | 213 | distance = this->computeDistance(currentPawn); |
---|
| 214 | humanItem = this->isHumanPlayer(currentPawn); |
---|
[8468] | 215 | COUT(5) << "Distanz:" << distance << std::endl; // message for debugging |
---|
[8404] | 216 | if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an! |
---|
[8164] | 217 | { |
---|
[8404] | 218 | if(humanItem) |
---|
| 219 | { |
---|
[8468] | 220 | COUT(5) << "humanItem ist true" << std::endl; |
---|
| 221 | this->displayWarning("Attention! You are close to the boundary!"); |
---|
[8404] | 222 | } |
---|
[8201] | 223 | } |
---|
[8513] | 224 | if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ ) |
---|
[8164] | 225 | { |
---|
[8404] | 226 | this->displayBoundaries(currentPawn); // Zeige Grenze an! |
---|
[8164] | 227 | } |
---|
[8404] | 228 | if(distance > this->maxDistance_ && (this->reaction_ == 1) ) |
---|
| 229 | { |
---|
| 230 | if( humanItem ) |
---|
| 231 | { |
---|
[8468] | 232 | COUT(5) << "Health should be decreasing!" << std::endl; |
---|
[8404] | 233 | this->displayWarning("You are out of the area now!"); |
---|
| 234 | } |
---|
| 235 | currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_); |
---|
| 236 | } |
---|
| 237 | if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Annahme: Ein Pawn kann von einem Tick bis zum nächsten nicht mehr als 100 Distanzeinheiten zurücklegen. |
---|
| 238 | { |
---|
| 239 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
| 240 | } |
---|
[8513] | 241 | if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) ) |
---|
| 242 | { |
---|
| 243 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
| 244 | } |
---|
[8110] | 245 | } |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | float SpaceBoundaries::computeDistance(WorldEntity *item) |
---|
| 250 | { |
---|
[8301] | 251 | if(item != NULL) |
---|
| 252 | { |
---|
| 253 | Vector3 itemPosition = item->getPosition(); |
---|
| 254 | return (itemPosition.distance(this->getPosition())); |
---|
| 255 | } else { |
---|
| 256 | return -1; |
---|
| 257 | } |
---|
[8110] | 258 | } |
---|
| 259 | |
---|
[8164] | 260 | void SpaceBoundaries::displayWarning(const std::string warnText) |
---|
[8301] | 261 | { |
---|
[8164] | 262 | |
---|
| 263 | } |
---|
[8110] | 264 | |
---|
[8244] | 265 | void SpaceBoundaries::displayBoundaries(Pawn *item) |
---|
| 266 | { |
---|
| 267 | |
---|
| 268 | Vector3 direction = item->getPosition() - this->getPosition(); |
---|
| 269 | direction.normalise(); |
---|
| 270 | |
---|
| 271 | Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_; |
---|
| 272 | |
---|
[8301] | 273 | this->positionBillboard(boundaryPosition); |
---|
[8244] | 274 | } |
---|
| 275 | |
---|
[8404] | 276 | void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt) |
---|
[8244] | 277 | { |
---|
| 278 | Vector3 normal = item->getPosition() - this->getPosition(); |
---|
[8404] | 279 | normal.normalise(); |
---|
| 280 | Vector3 velocity = item->getVelocity(); |
---|
| 281 | float normalSpeed = item->getVelocity().dotProduct(normal); |
---|
| 282 | |
---|
| 283 | /* Checke, ob das Pawn innerhalb des nächsten Ticks, das erlaubte Gebiet verlassen würde. |
---|
| 284 | Falls ja: Spicke es zurück. */ |
---|
[8513] | 285 | if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure" |
---|
[8244] | 286 | { |
---|
[8513] | 287 | bounceBack(item, &normal, &velocity); |
---|
| 288 | } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure" |
---|
| 289 | { |
---|
| 290 | normal = normal * (-1); |
---|
| 291 | bounceBack(item, &normal, &velocity); |
---|
[8244] | 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
[8513] | 295 | void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity) |
---|
| 296 | { |
---|
| 297 | float dampingFactor = 0.5; |
---|
| 298 | *velocity = velocity->reflect(*normal); |
---|
| 299 | Vector3 acceleration = item->getAcceleration(); |
---|
| 300 | acceleration = acceleration.reflect(*normal); |
---|
| 301 | |
---|
| 302 | item->lookAt( *velocity + this->getPosition() ); |
---|
| 303 | |
---|
| 304 | item->setAcceleration(acceleration * dampingFactor); |
---|
| 305 | item->setVelocity(*velocity * dampingFactor); |
---|
| 306 | |
---|
| 307 | item->setPosition( item->getPosition() - *normal * 10 ); // Setze das SpaceShip noch etwas von der Grenze weg. |
---|
| 308 | } |
---|
| 309 | |
---|
[8164] | 310 | bool SpaceBoundaries::isHumanPlayer(Pawn *item) |
---|
| 311 | { |
---|
| 312 | if(item != NULL) |
---|
| 313 | { |
---|
[8201] | 314 | if(item->getPlayer()) |
---|
| 315 | { |
---|
| 316 | return item->getPlayer()->isHumanPlayer(); |
---|
| 317 | } |
---|
[8164] | 318 | } |
---|
[8201] | 319 | return false; |
---|
[8164] | 320 | } |
---|
[8110] | 321 | |
---|
[8087] | 322 | } |
---|