Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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