Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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