Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spaceboundaries2/src/orxonox/worldentities/SpaceBoundaries.cc @ 8703

Last change on this file since 8703 was 8613, checked in by dafrick, 14 years ago

Cleanup.

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