Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8513 was 8513, checked in by kmaurus, 13 years ago

third reactionMode added

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