Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spaceboundaries/src/orxonox/worldentities/SpaceBoundaries.cc @ 8301

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

several billboards that represent the boundary can be displayed now

File size: 8.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(2000);
50        this->setShowDistance(2500);
51        this->setHealthDecrease(1);
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        if(this->boundary_ != NULL)
65        {
66            delete this->boundary_;
67        }
68       
69        this->pawnsIn_.clear();
70       
71        for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++)
72        {
73            if( current->billy != NULL)
74            {
75                delete current->billy;
76            }
77        }
78        this->billboards_.clear();
79    }
80   
81    void SpaceBoundaries::checkWhoIsIn()
82    {
83        pawnsIn_.clear();
84        for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current)
85        {
86            Pawn* currentPawn = *current;
87            float distance = this->computeDistance(currentPawn);
88            if(distance <= this->maxDistance_)
89            {
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            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::XMLPort(Element& xmlelement, XMLPort::Mode mode)
175    {
176        SUPER(SpaceBoundaries, XMLPort, xmlelement, mode);
177
178        XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode);
179        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
180        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
181    }
182   
183    void SpaceBoundaries::tick(float dt)
184    {
185        this->removeAllBillboards();
186        COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl;
187       
188        float distance;
189        bool humanItem;
190        for( std::list<Pawn*>::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
191        {
192            Pawn* currentPawn = *current;
193            distance = this->computeDistance(currentPawn);
194            humanItem = this->isHumanPlayer(currentPawn);
195            COUT(0) << "Distanz:" << distance << std::endl; // message for debugging
196            if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an!
197            {
198                COUT(0) << "You are leaving the area" << std::endl; // message for debugging
199                if(humanItem)
200                {
201                    COUT(0) << "humanItem ist true" << std::endl;
202                    this->displayWarning("Attention! You are leaving the area!");
203                } else {
204                   
205                }
206            }
207            if( (this->maxDistance_ - distance) < this->showDistance_)
208            {
209                // Zeige Grenze an!
210                this->displayBoundaries(currentPawn);
211            }
212            if(distance > this->maxDistance_)
213            {
214                if(humanItem)
215                {
216                    COUT(0) << "Health should be decreasing!" << std::endl;
217                    this->displayWarning("You are out of the area now!");
218                    currentPawn->removeHealth( (distance - maxDistance_) * this->healthDecrease_);
219                } else {
220                   
221                }
222               
223                this->bounceBack(currentPawn);
224            }
225        }
226        this->checkWhoIsIn();
227    }
228   
229    float SpaceBoundaries::computeDistance(WorldEntity *item)
230    {
231        if(item != NULL)
232        {
233            Vector3 itemPosition = item->getPosition();
234            return (itemPosition.distance(this->getPosition()));
235        } else {
236            return -1;
237        }
238    }
239   
240    void SpaceBoundaries::displayWarning(const std::string warnText)
241    {   
242       
243    }
244   
245    void SpaceBoundaries::displayBoundaries(Pawn *item)
246    {
247       
248        Vector3 direction = item->getPosition() - this->getPosition();
249        direction.normalise();
250       
251        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
252       
253        this->positionBillboard(boundaryPosition);
254    }
255   
256    void SpaceBoundaries::bounceBack(Pawn *item)
257    {
258        Vector3 normal = item->getPosition() - this->getPosition();
259        if( item->getVelocity().dotProduct(normal) > 0 ) // Greife nur ein, falls sich das Pawn nach Aussen bewegt.
260        {
261            float dampingFactor = 0.5;
262       
263            normal.normalise();
264            Vector3 velocity = item->getVelocity();
265            velocity = velocity.reflect(normal);
266            Vector3 acceleration = item->getAcceleration();
267            acceleration = acceleration.reflect(normal);
268           
269            item->lookAt( velocity + this->getPosition() );
270           
271            item->setAcceleration(acceleration * dampingFactor);
272            item->setVelocity(velocity * dampingFactor);
273        }
274    }
275   
276    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
277    {
278        if(item != NULL)
279        {
280            if(item->getPlayer())
281            {
282                return item->getPlayer()->isHumanPlayer();
283            }
284        }
285        return false;
286    }
287   
288}
Note: See TracBrowser for help on using the repository browser.