Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/SpaceBoundaries.cc @ 11103

Last change on this file since 11103 was 11083, checked in by muemart, 9 years ago

Fix some clang-tidy warnings.
Also, Serialise.h was doing some C-style casts that ended up being const casts. I moved those const casts as close to the source as possible and changed the loadAndIncrease functions to not do that.

  • Property svn:eol-style set to native
File size: 10.6 KB
RevLine 
[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
[8632]31#include <OgreBillboardSet.h>
32
33#include "core/CoreIncludes.h"
[9667]34#include "core/object/ObjectListIterator.h"
[8201]35#include "core/XMLPort.h"
[8632]36
37#include "graphics/Billboard.h"
38#include "infos/PlayerInfo.h"
39#include "worldentities/WorldEntity.h"
[8201]40#include "worldentities/pawns/Pawn.h"
[8087]41
42namespace orxonox
43{
[9667]44    RegisterClass(SpaceBoundaries);
[8087]45
[9667]46    SpaceBoundaries::SpaceBoundaries(Context* context) : StaticEntity(context)
[8087]47    {
[8614]48        RegisterObject(SpaceBoundaries);
49
[8201]50        this->setMaxDistance(3000);
[8614]51        this->setWarnDistance(this->getMaxDistance());
52        this->setShowDistance(this->getMaxDistance());
[8404]53        this->setReaction(0);
[8087]54    }
55    SpaceBoundaries::~SpaceBoundaries()
56    {
[8470]57        if (this->isInitialized())
58        {
59            this->pawnsIn_.clear();
[8767]60
[11071]61            for(BillboardAdministration& billboard : this->billboards_)
[8301]62            {
[11071]63                if( billboard.billy != nullptr)
[8470]64                {
[11071]65                    delete billboard.billy;
[8470]66                }
[8301]67            }
[8470]68            this->billboards_.clear();
[8301]69        }
[8087]70    }
[8767]71
[8281]72    void SpaceBoundaries::checkWhoIsIn()
73    {
74        pawnsIn_.clear();
[11071]75        for(Pawn* currentPawn : ObjectList<Pawn>())
[8281]76        {
[8614]77            if( this->reaction_ == 0 )
[8281]78            {
[8614]79                float distance = this->computeDistance(currentPawn);
80                if(distance <= this->maxDistance_)
81                {
82                    pawnsIn_.push_back(currentPawn);
83                }
84            } else if (this->reaction_ == 2) {
85                float distance = this->computeDistance(currentPawn);
86                if(distance >= this->maxDistance_)
87                {
88                    pawnsIn_.push_back(currentPawn);
89                }
90            } else {
[8281]91                pawnsIn_.push_back(currentPawn);
92            }
93        }
94    }
[8767]95
[8660]96    void SpaceBoundaries::positionBillboard(const Vector3& position, float alpha)
[8301]97    {
[8660]98        size_t current;
99        for (current = 0; current < this->billboards_.size(); ++current)
100            if (!this->billboards_[current].usedYet)
[8301]101                break;
[8660]102
103        if (current == this->billboards_.size())
[8301]104        {
[9667]105            Billboard* billboard = new Billboard(this->getContext());
[8660]106            billboard->setPosition(position);
[8694]107            billboard->setSyncMode(ObjectDirection::None);
[8660]108            this->setBillboardOptions(billboard);
109            BillboardAdministration ba = {true, billboard};
110            this->billboards_.push_back(ba);
[8301]111        }
[8660]112
113        this->billboards_[current].billy->setPosition(position);
114        this->billboards_[current].billy->setVisible(true);
115        this->billboards_[current].billy->setColour(ColourValue(1, 1, 1, alpha));
116        this->billboards_[current].usedYet = true;
117
[9941]118        Vector3 directionVector = (this->getWorldPosition() - position).normalisedCopy(); // vector from the position of the billboard to the center of the sphere
[8660]119        this->billboards_[current].billy->setCommonDirection(directionVector);
120
121        Vector3 upVector = Vector3(directionVector.z, directionVector.z, -(directionVector.x + directionVector.y)); // vector perpendicular to the direction vector
122        upVector.normalise();
123        this->billboards_[current].billy->setCommonUpVector(upVector);
[8301]124    }
[8767]125
[8301]126    void SpaceBoundaries::setBillboardOptions(Billboard *billy)
127    {
[11071]128        if(billy != nullptr)
[8301]129        {
[8614]130            billy->setMaterial("Grid");
131            billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON);
132            billy->setDefaultDimensions(150, 150);
[8301]133            billy->setVisible(true);
134        }
135    }
[8767]136
[8301]137    void SpaceBoundaries::removeAllBillboards()
138    {
[11071]139        for(BillboardAdministration& billboard : this->billboards_)
[8301]140        {
[11071]141            billboard.usedYet = false;
142            billboard.billy->setVisible(false);
[8301]143        }
144    }
[8767]145
[8110]146    void SpaceBoundaries::setMaxDistance(float r)
147    {
[8166]148        this->maxDistance_ = r;
[8110]149    }
150    float SpaceBoundaries::getMaxDistance()
151    {
[8166]152        return this->maxDistance_;
[8110]153    }
[8767]154
[8110]155    void SpaceBoundaries::setWarnDistance(float r)
156    {
[8166]157        this->warnDistance_ = r;
[8110]158    }
159    float SpaceBoundaries::getWarnDistance()
160    {
[8166]161        return this->warnDistance_;
[8110]162    }
[8767]163
[8244]164    void SpaceBoundaries::setShowDistance(float r)
165    {
166        this->showDistance_ = r;
167    }
168    float SpaceBoundaries::getShowDistance()
169    {
170        return this->showDistance_;
171    }
[8767]172
[8201]173    void SpaceBoundaries::setHealthDecrease(float amount)
174    {
175        this->healthDecrease_ = amount/1000;
176    }
177    float SpaceBoundaries::getHealthDecrease()
178    {
179        return this->healthDecrease_;
180    }
[8767]181
[8404]182    void SpaceBoundaries::setReaction(int mode)
183    {
184        this->reaction_ = mode;
185    }
186    int SpaceBoundaries::getReaction()
187    {
188        return this->reaction_;
189    }
[8087]190
191    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
192    {
[8110]193        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
[8087]194
[8110]195        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
196        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
[8614]197        XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode);
[8201]198        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
[8404]199        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
[8087]200    }
[8767]201
[8110]202    void SpaceBoundaries::tick(float dt)
203    {
[8404]204        this->checkWhoIsIn();
[8301]205        this->removeAllBillboards();
[8767]206
[8301]207        float distance;
208        bool humanItem;
[11071]209        for(Pawn* currentPawn : pawnsIn_)
[8110]210        {
[8404]211            if( currentPawn && currentPawn->getNode() ) 
[8110]212            {
[8404]213                distance = this->computeDistance(currentPawn);
214                humanItem = this->isHumanPlayer(currentPawn);
[8858]215//                orxout() << "Distance:" << distance << endl; // message for debugging
[8614]216                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning
[8164]217                {
[8404]218                    if(humanItem)
219                    {
[8614]220                        this->displayWarning("Attention! You are close to the boundary!");
[8404]221                    }
[8201]222                }
[11083]223                if(/* humanItem &&*/ std::abs(this->maxDistance_ - distance) < this->showDistance_ )
[8164]224                {
[8660]225                    this->displayBoundaries(currentPawn, 1.0f - fabs(this->maxDistance_ - distance) / this->showDistance_); // Show the boundary
[8164]226                }
[8404]227                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
228                {
229                    if( humanItem )
230                    {
[8858]231//                        orxout() << "Health should be decreasing!" << endl;
[8404]232                        this->displayWarning("You are out of the area now!");
233                    }
234                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
235                }
[8614]236                if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick.
[8404]237                {
238                    this->conditionalBounceBack(currentPawn, distance, dt);
239                }
[8614]240                if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) )
241                {
242                    this->conditionalBounceBack(currentPawn, distance, dt);
243                }
[8110]244            }
245        }
246    }
[8767]247
[8110]248    float SpaceBoundaries::computeDistance(WorldEntity *item)
249    {
[11071]250        if(item != nullptr)
[8301]251        {
[9941]252            Vector3 itemPosition = item->getWorldPosition();
253            return (itemPosition.distance(this->getWorldPosition()));
[8301]254        } else {
255            return -1;
256        }
[8110]257    }
[8767]258
[8164]259    void SpaceBoundaries::displayWarning(const std::string warnText)
[8767]260    {
[8614]261        // TODO
[8164]262    }
[8767]263
[8660]264    void SpaceBoundaries::displayBoundaries(Pawn *item, float alpha)
[8244]265    {
[8767]266
[9941]267        Vector3 direction = item->getWorldPosition() - this->getWorldPosition();
[8244]268        direction.normalise();
[8767]269
[9941]270        Vector3 boundaryPosition = this->getWorldPosition() + direction * this->maxDistance_;
[8767]271
[8660]272        this->positionBillboard(boundaryPosition, alpha);
[8244]273    }
[8767]274
[8404]275    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
[8244]276    {
[9941]277        Vector3 normal = item->getWorldPosition() - this->getWorldPosition();
[8404]278        normal.normalise();
279        Vector3 velocity = item->getVelocity();
280        float normalSpeed = item->getVelocity().dotProduct(normal);
[8767]281
[8614]282        /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */
283        if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure"
[8244]284        {
[8614]285            bounceBack(item, &normal, &velocity);
286        } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure"
287        {
288            normal = normal * (-1);
289            bounceBack(item, &normal, &velocity);
[8244]290        }
291    }
[8767]292
[8614]293    void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity)
294    {
295        float dampingFactor = 0.5;
296        *velocity = velocity->reflect(*normal);
297        Vector3 acceleration = item->getAcceleration();
298        acceleration = acceleration.reflect(*normal);
[8767]299
[9941]300        item->lookAt( *velocity + this->getWorldPosition() );
[8767]301
[8614]302        item->setAcceleration(acceleration * dampingFactor);
303        item->setVelocity(*velocity * dampingFactor);
[8767]304
[9941]305        item->setPosition( item->getWorldPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary.
[8614]306    }
[8767]307
[8164]308    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
309    {
[11071]310        if(item != nullptr)
[8164]311        {
[8201]312            if(item->getPlayer())
313            {
314                return item->getPlayer()->isHumanPlayer();
315            }
[8164]316        }
[8201]317        return false;
[8164]318    }
[8767]319
[8087]320}
Note: See TracBrowser for help on using the repository browser.