Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8460 was 8458, checked in by smerkli, 14 years ago

merged spaceboundaries

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