Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

endversion

File size: 11.5 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        COUT(4) << "Groesse der Pawn-Liste 'SpaceBoundaries::pawnsIn_': " << (int) pawnsIn_.size() << std::endl;
214       
215        float distance;
216        bool humanItem;
217        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
218        {
219            Pawn* currentPawn = current->get();
220            if( currentPawn && currentPawn->getNode() ) 
221            {
222                distance = this->computeDistance(currentPawn);
223                humanItem = this->isHumanPlayer(currentPawn);
224                COUT(5) << "Distanz:" << distance << std::endl; // message for debugging
225                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an!
226                {
227                    if(humanItem)
228                    {
229                        COUT(5) << "humanItem ist true" << std::endl;
230                        this->displayWarning("Attention! You are close to the boundary!");
231                    }
232                }
233                if(/* humanItem &&*/ abs(this->maxDistance_ - distance) < this->showDistance_ )
234                {
235                    this->displayBoundaries(currentPawn); // Zeige Grenze an!
236                }
237                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
238                {
239                    if( humanItem )
240                    {
241                        COUT(5) << "Health should be decreasing!" << std::endl;
242                        this->displayWarning("You are out of the area now!");
243                    }
244                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
245                }
246                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.
247                {
248                    this->conditionalBounceBack(currentPawn, distance, dt);
249                }
250                if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) )
251                {
252                    this->conditionalBounceBack(currentPawn, distance, dt);
253                }
254            }
255        }
256    }
257   
258    float SpaceBoundaries::computeDistance(WorldEntity *item)
259    {
260        if(item != NULL)
261        {
262            Vector3 itemPosition = item->getPosition();
263            return (itemPosition.distance(this->getPosition()));
264        } else {
265            return -1;
266        }
267    }
268   
269    void SpaceBoundaries::displayWarning(const std::string warnText)
270    {   
271        // TODO
272    }
273   
274    void SpaceBoundaries::displayBoundaries(Pawn *item)
275    {
276       
277        Vector3 direction = item->getPosition() - this->getPosition();
278        direction.normalise();
279       
280        Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_;
281       
282        this->positionBillboard(boundaryPosition);
283    }
284   
285    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
286    {
287        Vector3 normal = item->getPosition() - this->getPosition();
288        normal.normalise();
289        Vector3 velocity = item->getVelocity();
290        float normalSpeed = item->getVelocity().dotProduct(normal);
291       
292        /* Checke, ob das Pawn innerhalb des nächsten Ticks, das erlaubte Gebiet verlassen würde.
293           Falls ja: Spicke es zurück. */
294        if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure"
295        {
296            bounceBack(item, &normal, &velocity);
297        } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure"
298        {
299            normal = normal * (-1);
300            bounceBack(item, &normal, &velocity);
301        }
302    }
303   
304    void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity)
305    {
306        float dampingFactor = 0.5;
307        *velocity = velocity->reflect(*normal);
308        Vector3 acceleration = item->getAcceleration();
309        acceleration = acceleration.reflect(*normal);
310       
311        item->lookAt( *velocity + this->getPosition() );
312       
313        item->setAcceleration(acceleration * dampingFactor);
314        item->setVelocity(*velocity * dampingFactor);
315       
316        item->setPosition( item->getPosition() - *normal * 10 ); // Setze das SpaceShip noch etwas von der Grenze weg.
317    }
318   
319    bool SpaceBoundaries::isHumanPlayer(Pawn *item)
320    {
321        if(item != NULL)
322        {
323            if(item->getPlayer())
324            {
325                return item->getPlayer()->isHumanPlayer();
326            }
327        }
328        return false;
329    }
330   
331}
Note: See TracBrowser for help on using the repository browser.