Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8469 was 8461, checked in by smerkli, 14 years ago

a little less verbosity, a little more action please :D

File size: 9.7 KB
Line 
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
31#include "worldentities/MobileEntity.h"
32#include "worldentities/ControllableEntity.h"
33#include "core/ObjectListIterator.h"
34#include "core/XMLPort.h"
35#include "worldentities/pawns/Pawn.h"
36#include "infos/PlayerInfo.h"
37#include "interfaces/RadarViewable.h"
38#include "graphics/Billboard.h"
39
40namespace orxonox
41{
42    CreateFactory(SpaceBoundaries);
43
44    SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator)
45    {
46        /* Standardwerte, die zum Tragen kommen,
47         * falls im XML-File keine Werte spezifiziert wurden. */
48        this->setMaxDistance(3000);
49        this->setWarnDistance(2000);
50        this->setShowDistance(2500);
51        this->setHealthDecrease(1);
52        this->setReaction(0);
53       
54        RegisterObject(SpaceBoundaries);
55       
56        // Show Boundaries on the radar.
57        this->centerRadar_ = new RadarViewable(this, this);
58        this->centerRadar_->setRadarObjectShape(RadarViewable::Dot);
59        this->centerRadar_->setRadarVisibility(false);
60    }
61    SpaceBoundaries::~SpaceBoundaries()
62    {
63        delete this->centerRadar_;
64       
65        this->pawnsIn_.clear();
66       
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();
75    }
76   
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   
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);
104            this->setBillboardOptions( tmp );
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   
134    void SpaceBoundaries::setMaxDistance(float r)
135    {
136        this->maxDistance_ = r;
137    }
138    float SpaceBoundaries::getMaxDistance()
139    {
140        return this->maxDistance_;
141    }
142   
143    void SpaceBoundaries::setWarnDistance(float r)
144    {
145        this->warnDistance_ = r;
146    }
147    float SpaceBoundaries::getWarnDistance()
148    {
149        return this->warnDistance_;
150    }
151   
152    void SpaceBoundaries::setShowDistance(float r)
153    {
154        this->showDistance_ = r;
155    }
156    float SpaceBoundaries::getShowDistance()
157    {
158        return this->showDistance_;
159    }
160   
161    void SpaceBoundaries::setHealthDecrease(float amount)
162    {
163        this->healthDecrease_ = amount/1000;
164    }
165    float SpaceBoundaries::getHealthDecrease()
166    {
167        return this->healthDecrease_;
168    }
169   
170    void SpaceBoundaries::setReaction(int mode)
171    {
172        this->reaction_ = mode;
173    }
174    int SpaceBoundaries::getReaction()
175    {
176        return this->reaction_;
177    }
178
179    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
180    {
181        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
182
183        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
184        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
185        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
186        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
187    }
188   
189    void SpaceBoundaries::tick(float dt)
190    {
191        this->checkWhoIsIn();
192        this->removeAllBillboards();
193        //COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl;
194       
195        float distance;
196        bool humanItem;
197        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
198        {
199            Pawn* currentPawn = current->get();
200            if( currentPawn && currentPawn->getNode() ) 
201            {
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!
206                {
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                    }
213                }
214                if( (this->maxDistance_ - distance) < this->showDistance_ )
215                {
216                    this->displayBoundaries(currentPawn); // Zeige Grenze an!
217                }
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                }
231            }
232        }
233    }
234   
235    float SpaceBoundaries::computeDistance(WorldEntity *item)
236    {
237        if(item != NULL)
238        {
239            Vector3 itemPosition = item->getPosition();
240            return (itemPosition.distance(this->getPosition()));
241        } else {
242            return -1;
243        }
244    }
245   
246    void SpaceBoundaries::displayWarning(const std::string warnText)
247    {   
248       
249    }
250   
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       
259        this->positionBillboard(boundaryPosition);
260    }
261   
262    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
263    {
264        Vector3 normal = item->getPosition() - this->getPosition();
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_ )
272        {
273            float dampingFactor = 0.5;
274            velocity = velocity.reflect(normal);
275            Vector3 acceleration = item->getAcceleration();
276            acceleration = acceleration.reflect(normal);
277           
278            item->lookAt( velocity + this->getPosition() );
279           
280            item->setAcceleration(acceleration * dampingFactor);
281            item->setVelocity(velocity * dampingFactor);
282        }
283    }
284   
285    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
286    {
287        if(item != NULL)
288        {
289            if(item->getPlayer())
290            {
291                return item->getPlayer()->isHumanPlayer();
292            }
293        }
294        return false;
295    }
296   
297}
Note: See TracBrowser for help on using the repository browser.