[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); |
---|
| 49 | this->setWarnDistance(2000); |
---|
[8244] | 50 | this->setShowDistance(2500); |
---|
[8201] | 51 | this->setHealthDecrease(1); |
---|
| 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 | |
---|
| 64 | if(this->boundary_ != NULL) |
---|
| 65 | { |
---|
| 66 | delete this->boundary_; |
---|
| 67 | } |
---|
| 68 | |
---|
[8281] | 69 | this->pawnsIn_.clear(); |
---|
| 70 | |
---|
[8301] | 71 | for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++) |
---|
| 72 | { |
---|
| 73 | if( current->billy != NULL) |
---|
| 74 | { |
---|
| 75 | delete current->billy; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | this->billboards_.clear(); |
---|
[8087] | 79 | } |
---|
[8110] | 80 | |
---|
[8281] | 81 | void SpaceBoundaries::checkWhoIsIn() |
---|
| 82 | { |
---|
| 83 | pawnsIn_.clear(); |
---|
| 84 | for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current) |
---|
| 85 | { |
---|
| 86 | Pawn* currentPawn = *current; |
---|
| 87 | float distance = this->computeDistance(currentPawn); |
---|
| 88 | if(distance <= this->maxDistance_) |
---|
| 89 | { |
---|
| 90 | pawnsIn_.push_back(currentPawn); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[8301] | 95 | void SpaceBoundaries::positionBillboard(const Vector3 position) |
---|
| 96 | { |
---|
| 97 | std::vector<billboardAdministration>::iterator current; |
---|
| 98 | for( current = this->billboards_.begin(); current != this->billboards_.end(); current++) |
---|
| 99 | { |
---|
| 100 | if(!current->usedYet) |
---|
| 101 | { |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | if( current == this->billboards_.end() ) |
---|
| 106 | { |
---|
| 107 | Billboard *tmp = new Billboard(this); |
---|
| 108 | setBillboardOptions( tmp ); |
---|
| 109 | tmp->setPosition(position); |
---|
| 110 | billboardAdministration tmp2 = { true, tmp }; |
---|
| 111 | this->billboards_.push_back( tmp2 ); |
---|
| 112 | |
---|
| 113 | } else { |
---|
| 114 | current->billy->setPosition(position); |
---|
| 115 | current->billy->setVisible(true); |
---|
| 116 | current->usedYet = true; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void SpaceBoundaries::setBillboardOptions(Billboard *billy) |
---|
| 121 | { |
---|
| 122 | if(billy != NULL) |
---|
| 123 | { |
---|
| 124 | billy->setMaterial("Shield"); |
---|
| 125 | billy->setVisible(true); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void SpaceBoundaries::removeAllBillboards() |
---|
| 130 | { |
---|
| 131 | for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ ) |
---|
| 132 | { |
---|
| 133 | current->usedYet = false; |
---|
| 134 | current->billy->setVisible(false); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[8110] | 138 | void SpaceBoundaries::setMaxDistance(float r) |
---|
| 139 | { |
---|
[8166] | 140 | this->maxDistance_ = r; |
---|
[8110] | 141 | } |
---|
| 142 | float SpaceBoundaries::getMaxDistance() |
---|
| 143 | { |
---|
[8166] | 144 | return this->maxDistance_; |
---|
[8110] | 145 | } |
---|
| 146 | |
---|
| 147 | void SpaceBoundaries::setWarnDistance(float r) |
---|
| 148 | { |
---|
[8166] | 149 | this->warnDistance_ = r; |
---|
[8110] | 150 | } |
---|
| 151 | float SpaceBoundaries::getWarnDistance() |
---|
| 152 | { |
---|
[8166] | 153 | return this->warnDistance_; |
---|
[8110] | 154 | } |
---|
[8201] | 155 | |
---|
[8244] | 156 | void SpaceBoundaries::setShowDistance(float r) |
---|
| 157 | { |
---|
| 158 | this->showDistance_ = r; |
---|
| 159 | } |
---|
| 160 | float SpaceBoundaries::getShowDistance() |
---|
| 161 | { |
---|
| 162 | return this->showDistance_; |
---|
| 163 | } |
---|
| 164 | |
---|
[8201] | 165 | void SpaceBoundaries::setHealthDecrease(float amount) |
---|
| 166 | { |
---|
| 167 | this->healthDecrease_ = amount/1000; |
---|
| 168 | } |
---|
| 169 | float SpaceBoundaries::getHealthDecrease() |
---|
| 170 | { |
---|
| 171 | return this->healthDecrease_; |
---|
| 172 | } |
---|
[8087] | 173 | |
---|
| 174 | void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 175 | { |
---|
[8110] | 176 | SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); |
---|
[8087] | 177 | |
---|
[8110] | 178 | XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); |
---|
| 179 | XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); |
---|
[8201] | 180 | XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); |
---|
[8087] | 181 | } |
---|
[8110] | 182 | |
---|
| 183 | void SpaceBoundaries::tick(float dt) |
---|
| 184 | { |
---|
[8301] | 185 | this->removeAllBillboards(); |
---|
[8281] | 186 | COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl; |
---|
| 187 | |
---|
[8301] | 188 | float distance; |
---|
| 189 | bool humanItem; |
---|
[8281] | 190 | for( std::list<Pawn*>::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ ) |
---|
[8110] | 191 | { |
---|
[8281] | 192 | Pawn* currentPawn = *current; |
---|
[8301] | 193 | distance = this->computeDistance(currentPawn); |
---|
| 194 | humanItem = this->isHumanPlayer(currentPawn); |
---|
| 195 | COUT(0) << "Distanz:" << distance << std::endl; // message for debugging |
---|
[8281] | 196 | if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an! |
---|
[8110] | 197 | { |
---|
[8301] | 198 | COUT(0) << "You are leaving the area" << std::endl; // message for debugging |
---|
[8281] | 199 | if(humanItem) |
---|
[8164] | 200 | { |
---|
[8281] | 201 | COUT(0) << "humanItem ist true" << std::endl; |
---|
| 202 | this->displayWarning("Attention! You are leaving the area!"); |
---|
| 203 | } else { |
---|
| 204 | |
---|
[8201] | 205 | } |
---|
[8281] | 206 | } |
---|
| 207 | if( (this->maxDistance_ - distance) < this->showDistance_) |
---|
| 208 | { |
---|
| 209 | // Zeige Grenze an! |
---|
| 210 | this->displayBoundaries(currentPawn); |
---|
| 211 | } |
---|
| 212 | if(distance > this->maxDistance_) |
---|
| 213 | { |
---|
| 214 | if(humanItem) |
---|
[8164] | 215 | { |
---|
[8281] | 216 | COUT(0) << "Health should be decreasing!" << std::endl; |
---|
| 217 | this->displayWarning("You are out of the area now!"); |
---|
| 218 | currentPawn->removeHealth( (distance - maxDistance_) * this->healthDecrease_); |
---|
| 219 | } else { |
---|
[8244] | 220 | |
---|
[8164] | 221 | } |
---|
[8281] | 222 | |
---|
| 223 | this->bounceBack(currentPawn); |
---|
[8110] | 224 | } |
---|
| 225 | } |
---|
[8281] | 226 | this->checkWhoIsIn(); |
---|
[8110] | 227 | } |
---|
| 228 | |
---|
| 229 | float SpaceBoundaries::computeDistance(WorldEntity *item) |
---|
| 230 | { |
---|
[8301] | 231 | if(item != NULL) |
---|
| 232 | { |
---|
| 233 | Vector3 itemPosition = item->getPosition(); |
---|
| 234 | return (itemPosition.distance(this->getPosition())); |
---|
| 235 | } else { |
---|
| 236 | return -1; |
---|
| 237 | } |
---|
[8110] | 238 | } |
---|
| 239 | |
---|
[8164] | 240 | void SpaceBoundaries::displayWarning(const std::string warnText) |
---|
[8301] | 241 | { |
---|
[8164] | 242 | |
---|
| 243 | } |
---|
[8110] | 244 | |
---|
[8244] | 245 | void SpaceBoundaries::displayBoundaries(Pawn *item) |
---|
| 246 | { |
---|
| 247 | |
---|
| 248 | Vector3 direction = item->getPosition() - this->getPosition(); |
---|
| 249 | direction.normalise(); |
---|
| 250 | |
---|
| 251 | Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_; |
---|
| 252 | |
---|
[8301] | 253 | this->positionBillboard(boundaryPosition); |
---|
[8244] | 254 | } |
---|
| 255 | |
---|
| 256 | void SpaceBoundaries::bounceBack(Pawn *item) |
---|
| 257 | { |
---|
| 258 | Vector3 normal = item->getPosition() - this->getPosition(); |
---|
[8301] | 259 | if( item->getVelocity().dotProduct(normal) > 0 ) // Greife nur ein, falls sich das Pawn nach Aussen bewegt. |
---|
[8244] | 260 | { |
---|
[8279] | 261 | float dampingFactor = 0.5; |
---|
| 262 | |
---|
[8244] | 263 | normal.normalise(); |
---|
| 264 | Vector3 velocity = item->getVelocity(); |
---|
| 265 | velocity = velocity.reflect(normal); |
---|
| 266 | Vector3 acceleration = item->getAcceleration(); |
---|
| 267 | acceleration = acceleration.reflect(normal); |
---|
[8279] | 268 | |
---|
| 269 | item->lookAt( velocity + this->getPosition() ); |
---|
[8281] | 270 | |
---|
[8279] | 271 | item->setAcceleration(acceleration * dampingFactor); |
---|
| 272 | item->setVelocity(velocity * dampingFactor); |
---|
[8244] | 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
[8164] | 276 | bool SpaceBoundaries::isHumanPlayer(Pawn *item) |
---|
| 277 | { |
---|
| 278 | if(item != NULL) |
---|
| 279 | { |
---|
[8201] | 280 | if(item->getPlayer()) |
---|
| 281 | { |
---|
| 282 | return item->getPlayer()->isHumanPlayer(); |
---|
| 283 | } |
---|
[8164] | 284 | } |
---|
[8201] | 285 | return false; |
---|
[8164] | 286 | } |
---|
[8110] | 287 | |
---|
[8087] | 288 | } |
---|