Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8613 was 8613, checked in by dafrick, 13 years ago

Cleanup.

File size: 11.3 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#include <OgreBillboardSet.h>
40
41
42namespace orxonox
43{
44    CreateFactory(SpaceBoundaries);
45
46    SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator)
47    {
48        /* Standardwerte, die zum Tragen kommen,
49         * falls im XML-File keine Werte spezifiziert wurden. */
50        this->setMaxDistance(3000);
51        this->setWarnDistance(this->getMaxDistance());
52        this->setShowDistance(this->getMaxDistance());
53        this->setReaction(0);
54       
55        RegisterObject(SpaceBoundaries);
56       
57        // Show Boundaries on the radar.
58        this->centerRadar_ = new RadarViewable(this, this);
59        this->centerRadar_->setRadarObjectShape(RadarViewable::Dot);
60        this->centerRadar_->setRadarVisibility(false);
61    }
62    SpaceBoundaries::~SpaceBoundaries()
63    {
64        delete this->centerRadar_;
65       
66        this->pawnsIn_.clear();
67       
68        for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++)
69        {
70            if( current->billy != NULL)
71            {
72                delete current->billy;
73            }
74        }
75        this->billboards_.clear();
76    }
77   
78    void SpaceBoundaries::checkWhoIsIn()
79    {
80        pawnsIn_.clear();
81        for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current)
82        {
83            Pawn* currentPawn = *current;
84            if( this->reaction_ == 0 )
85            {
86                float distance = this->computeDistance(currentPawn);
87                if(distance <= this->maxDistance_)
88                {
89                    pawnsIn_.push_back(currentPawn);
90                }
91            } else if (this->reaction_ == 2) {
92                float distance = this->computeDistance(currentPawn);
93                if(distance >= this->maxDistance_)
94                {
95                    pawnsIn_.push_back(currentPawn);
96                }
97            } else {
98                pawnsIn_.push_back(currentPawn);
99            }
100        }
101    }
102   
103    void SpaceBoundaries::positionBillboard(const Vector3 position)
104    {
105        std::vector<billboardAdministration>::iterator current;
106        for( current = this->billboards_.begin(); current != this->billboards_.end(); current++)
107        {
108            if(!current->usedYet)
109            {
110                break;
111            }
112        }
113        if( current == this->billboards_.end() )
114        {
115            Billboard *tmp = new Billboard(this);
116            tmp->setPosition(position);
117            this->setBillboardOptions( tmp );
118            Vector3 normalisedVec = (position - this->getPosition()).normalisedCopy(); /* Vektor von Kugelmitte nach aussen */
119            tmp->setCommonDirection ( -1.0 * normalisedVec );
120            tmp->setCommonUpVector( Vector3::UNIT_Z );
121            billboardAdministration tmp2 = { true, tmp };
122            this->billboards_.push_back( tmp2 );
123        } else {
124            current->billy->setPosition(position);
125            current->billy->setVisible(true);
126            current->usedYet = true;
127            Vector3 normalisedVec = (position - this->getPosition()).normalisedCopy(); /* Vektor von Kugelmitte nach aussen */
128            current->billy->setCommonDirection ( -1.0 * normalisedVec );
129            current->billy->setCommonUpVector( Vector3::UNIT_Z );
130        }
131    }
132   
133    void SpaceBoundaries::setBillboardOptions(Billboard *billy)
134    {
135        if(billy != NULL)
136        {
137            billy->setMaterial("Grid");
138            billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON);
139            billy->setDefaultDimensions(150, 150);
140            billy->setVisible(true);
141        }
142    }
143   
144    void SpaceBoundaries::removeAllBillboards()
145    {
146        for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ )
147        {
148            current->usedYet = false;
149            current->billy->setVisible(false);
150        }
151    }
152   
153    void SpaceBoundaries::setMaxDistance(float r)
154    {
155        this->maxDistance_ = r;
156    }
157    float SpaceBoundaries::getMaxDistance()
158    {
159        return this->maxDistance_;
160    }
161   
162    void SpaceBoundaries::setWarnDistance(float r)
163    {
164        this->warnDistance_ = r;
165    }
166    float SpaceBoundaries::getWarnDistance()
167    {
168        return this->warnDistance_;
169    }
170   
171    void SpaceBoundaries::setShowDistance(float r)
172    {
173        this->showDistance_ = r;
174    }
175    float SpaceBoundaries::getShowDistance()
176    {
177        return this->showDistance_;
178    }
179   
180    void SpaceBoundaries::setHealthDecrease(float amount)
181    {
182        this->healthDecrease_ = amount/1000;
183    }
184    float SpaceBoundaries::getHealthDecrease()
185    {
186        return this->healthDecrease_;
187    }
188   
189    void SpaceBoundaries::setReaction(int mode)
190    {
191        this->reaction_ = mode;
192    }
193    int SpaceBoundaries::getReaction()
194    {
195        return this->reaction_;
196    }
197
198    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
199    {
200        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
201
202        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
203        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
204        XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode);
205        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
206        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
207    }
208   
209    void SpaceBoundaries::tick(float dt)
210    {
211        this->checkWhoIsIn();
212        this->removeAllBillboards();
213       
214        float distance;
215        bool humanItem;
216        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
217        {
218            Pawn* currentPawn = current->get();
219            if( currentPawn && currentPawn->getNode() ) 
220            {
221                distance = this->computeDistance(currentPawn);
222                humanItem = this->isHumanPlayer(currentPawn);
223                COUT(5) << "Distance:" << distance << std::endl; // message for debugging
224                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning
225                {
226                    if(humanItem)
227                    {
228                        this->displayWarning("Attention! You are close to the boundary!");
229                    }
230                }
231                if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ )
232                {
233                    this->displayBoundaries(currentPawn); // Show the boundary
234                }
235                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
236                {
237                    if( humanItem )
238                    {
239                        COUT(5) << "Health should be decreasing!" << std::endl;
240                        this->displayWarning("You are out of the area now!");
241                    }
242                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
243                }
244                if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick.
245                {
246                    this->conditionalBounceBack(currentPawn, distance, dt);
247                }
248                if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) )
249                {
250                    this->conditionalBounceBack(currentPawn, distance, dt);
251                }
252            }
253        }
254    }
255   
256    float SpaceBoundaries::computeDistance(WorldEntity *item)
257    {
258        if(item != NULL)
259        {
260            Vector3 itemPosition = item->getPosition();
261            return (itemPosition.distance(this->getPosition()));
262        } else {
263            return -1;
264        }
265    }
266   
267    void SpaceBoundaries::displayWarning(const std::string warnText)
268    {   
269        // TODO
270    }
271   
272    void SpaceBoundaries::displayBoundaries(Pawn *item)
273    {
274       
275        Vector3 direction = item->getPosition() - this->getPosition();
276        direction.normalise();
277       
278        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
279       
280        this->positionBillboard(boundaryPosition);
281    }
282   
283    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
284    {
285        Vector3 normal = item->getPosition() - this->getPosition();
286        normal.normalise();
287        Vector3 velocity = item->getVelocity();
288        float normalSpeed = item->getVelocity().dotProduct(normal);
289       
290        /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */
291        if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure"
292        {
293            bounceBack(item, &normal, &velocity);
294        } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure"
295        {
296            normal = normal * (-1);
297            bounceBack(item, &normal, &velocity);
298        }
299    }
300   
301    void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity)
302    {
303        float dampingFactor = 0.5;
304        *velocity = velocity->reflect(*normal);
305        Vector3 acceleration = item->getAcceleration();
306        acceleration = acceleration.reflect(*normal);
307       
308        item->lookAt( *velocity + this->getPosition() );
309       
310        item->setAcceleration(acceleration * dampingFactor);
311        item->setVelocity(*velocity * dampingFactor);
312       
313        item->setPosition( item->getPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary.
314    }
315   
316    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
317    {
318        if(item != NULL)
319        {
320            if(item->getPlayer())
321            {
322                return item->getPlayer()->isHumanPlayer();
323            }
324        }
325        return false;
326    }
327   
328}
Note: See TracBrowser for help on using the repository browser.