Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/SpaceBoundaries.cc @ 8552

Last change on this file since 8552 was 8470, checked in by rgrieder, 13 years ago

A classic fix ;)
Orxonox crashed on startup.

File size: 9.8 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"
[8087]39
40namespace 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);
[8404]52        this->setReaction(0);
[8201]53       
[8087]54        RegisterObject(SpaceBoundaries);
[8201]55       
[8110]56        // Show Boundaries on the radar.
[8201]57        this->centerRadar_ = new RadarViewable(this, this);
58        this->centerRadar_->setRadarObjectShape(RadarViewable::Dot);
59        this->centerRadar_->setRadarVisibility(false);
[8087]60    }
61    SpaceBoundaries::~SpaceBoundaries()
62    {
[8470]63        if (this->isInitialized())
64        {
65            delete this->centerRadar_;
[8244]66       
[8470]67            this->pawnsIn_.clear();
[8281]68       
[8470]69            for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++)
[8301]70            {
[8470]71                if( current->billy != NULL)
72                {
73                    delete current->billy;
74                }
[8301]75            }
[8470]76            this->billboards_.clear();
[8301]77        }
[8087]78    }
[8110]79   
[8281]80    void SpaceBoundaries::checkWhoIsIn()
81    {
82        pawnsIn_.clear();
83        for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current)
84        {
85            Pawn* currentPawn = *current;
86            float distance = this->computeDistance(currentPawn);
87            if(distance <= this->maxDistance_)
88            {
89                pawnsIn_.push_back(currentPawn);
90            }
91        }
92    }
93   
[8301]94    void SpaceBoundaries::positionBillboard(const Vector3 position)
95    {
96        std::vector<billboardAdministration>::iterator current;
97        for( current = this->billboards_.begin(); current != this->billboards_.end(); current++)
98        {
99            if(!current->usedYet)
100            {
101                break;
102            }
103        }
104        if( current == this->billboards_.end() )
105        {
106            Billboard *tmp = new Billboard(this);
[8404]107            this->setBillboardOptions( tmp );
[8301]108            tmp->setPosition(position);
109            billboardAdministration tmp2 = { true, tmp };
110            this->billboards_.push_back( tmp2 );
111           
112        } else {
113            current->billy->setPosition(position);
114            current->billy->setVisible(true);
115            current->usedYet = true;
116        }
117    }
118   
119    void SpaceBoundaries::setBillboardOptions(Billboard *billy)
120    {
121        if(billy != NULL)
122        {
123            billy->setMaterial("Shield");
124            billy->setVisible(true);
125        }
126    }
127   
128    void SpaceBoundaries::removeAllBillboards()
129    {
130        for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ )
131        {
132            current->usedYet = false;
133            current->billy->setVisible(false);
134        }
135    }
136   
[8110]137    void SpaceBoundaries::setMaxDistance(float r)
138    {
[8166]139        this->maxDistance_ = r;
[8110]140    }
141    float SpaceBoundaries::getMaxDistance()
142    {
[8166]143        return this->maxDistance_;
[8110]144    }
145   
146    void SpaceBoundaries::setWarnDistance(float r)
147    {
[8166]148        this->warnDistance_ = r;
[8110]149    }
150    float SpaceBoundaries::getWarnDistance()
151    {
[8166]152        return this->warnDistance_;
[8110]153    }
[8201]154   
[8244]155    void SpaceBoundaries::setShowDistance(float r)
156    {
157        this->showDistance_ = r;
158    }
159    float SpaceBoundaries::getShowDistance()
160    {
161        return this->showDistance_;
162    }
163   
[8201]164    void SpaceBoundaries::setHealthDecrease(float amount)
165    {
166        this->healthDecrease_ = amount/1000;
167    }
168    float SpaceBoundaries::getHealthDecrease()
169    {
170        return this->healthDecrease_;
171    }
[8404]172   
173    void SpaceBoundaries::setReaction(int mode)
174    {
175        this->reaction_ = mode;
176    }
177    int SpaceBoundaries::getReaction()
178    {
179        return this->reaction_;
180    }
[8087]181
182    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
183    {
[8110]184        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
[8087]185
[8110]186        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
187        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
[8201]188        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
[8404]189        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
[8087]190    }
[8110]191   
192    void SpaceBoundaries::tick(float dt)
193    {
[8404]194        this->checkWhoIsIn();
[8301]195        this->removeAllBillboards();
[8461]196        //COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl;
[8281]197       
[8301]198        float distance;
199        bool humanItem;
[8404]200        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
[8110]201        {
[8404]202            Pawn* currentPawn = current->get();
203            if( currentPawn && currentPawn->getNode() ) 
[8110]204            {
[8404]205                distance = this->computeDistance(currentPawn);
206                humanItem = this->isHumanPlayer(currentPawn);
[8461]207                //COUT(0) << "Distanz:" << distance << std::endl; // message for debugging
[8404]208                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an!
[8164]209                {
[8461]210                    //COUT(0) << "You are near by the boundaries!" << std::endl; // message for debugging
[8404]211                    if(humanItem)
212                    {
[8461]213                        //COUT(0) << "humanItem ist true" << std::endl;
[8404]214                        this->displayWarning("Attention! You are near by the boundaries!");
215                    }
[8201]216                }
[8404]217                if( (this->maxDistance_ - distance) < this->showDistance_ )
[8164]218                {
[8404]219                    this->displayBoundaries(currentPawn); // Zeige Grenze an!
[8164]220                }
[8404]221                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
222                {
223                    if( humanItem )
224                    {
[8461]225                        //COUT(0) << "Health should be decreasing!" << std::endl;
[8404]226                        this->displayWarning("You are out of the area now!");
227                    }
228                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
229                }
230                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.
231                {
232                    this->conditionalBounceBack(currentPawn, distance, dt);
233                }
[8110]234            }
235        }
236    }
237   
238    float SpaceBoundaries::computeDistance(WorldEntity *item)
239    {
[8301]240        if(item != NULL)
241        {
242            Vector3 itemPosition = item->getPosition();
243            return (itemPosition.distance(this->getPosition()));
244        } else {
245            return -1;
246        }
[8110]247    }
248   
[8164]249    void SpaceBoundaries::displayWarning(const std::string warnText)
[8301]250    {   
[8164]251       
252    }
[8110]253   
[8244]254    void SpaceBoundaries::displayBoundaries(Pawn *item)
255    {
256       
257        Vector3 direction = item->getPosition() - this->getPosition();
258        direction.normalise();
259       
260        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
261       
[8301]262        this->positionBillboard(boundaryPosition);
[8244]263    }
264   
[8404]265    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
[8244]266    {
267        Vector3 normal = item->getPosition() - this->getPosition();
[8404]268        normal.normalise();
269        Vector3 velocity = item->getVelocity();
270        float normalSpeed = item->getVelocity().dotProduct(normal);
271       
272        /* Checke, ob das Pawn innerhalb des nächsten Ticks, das erlaubte Gebiet verlassen würde.
273           Falls ja: Spicke es zurück. */
274        if( currentDistance + normalSpeed * dt > this->maxDistance_ )
[8244]275        {
[8279]276            float dampingFactor = 0.5;
[8244]277            velocity = velocity.reflect(normal);
278            Vector3 acceleration = item->getAcceleration();
279            acceleration = acceleration.reflect(normal);
[8279]280           
281            item->lookAt( velocity + this->getPosition() );
[8281]282           
[8279]283            item->setAcceleration(acceleration * dampingFactor);
284            item->setVelocity(velocity * dampingFactor);
[8244]285        }
286    }
287   
[8164]288    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
289    {
290        if(item != NULL)
291        {
[8201]292            if(item->getPlayer())
293            {
294                return item->getPlayer()->isHumanPlayer();
295            }
[8164]296        }
[8201]297        return false;
[8164]298    }
[8110]299   
[8087]300}
Note: See TracBrowser for help on using the repository browser.