Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8404


Ignore:
Timestamp:
May 5, 2011, 4:14:07 PM (14 years ago)
Author:
kmaurus
Message:

some adaptations and improvements

Location:
code/branches/spaceboundaries/src/orxonox/worldentities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/spaceboundaries/src/orxonox/worldentities/SpaceBoundaries.cc

    r8301 r8404  
    5050        this->setShowDistance(2500);
    5151        this->setHealthDecrease(1);
     52        this->setReaction(0);
    5253       
    5354        RegisterObject(SpaceBoundaries);
     
    6263        delete this->centerRadar_;
    6364       
    64         if(this->boundary_ != NULL)
    65         {
    66             delete this->boundary_;
    67         }
    68        
    6965        this->pawnsIn_.clear();
    7066       
     
    106102        {
    107103            Billboard *tmp = new Billboard(this);
    108             setBillboardOptions( tmp );
     104            this->setBillboardOptions( tmp );
    109105            tmp->setPosition(position);
    110106            billboardAdministration tmp2 = { true, tmp };
     
    171167        return this->healthDecrease_;
    172168    }
     169   
     170    void SpaceBoundaries::setReaction(int mode)
     171    {
     172        this->reaction_ = mode;
     173    }
     174    int SpaceBoundaries::getReaction()
     175    {
     176        return this->reaction_;
     177    }
    173178
    174179    void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     
    179184        XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode);
    180185        XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode);
     186        XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode);
    181187    }
    182188   
    183189    void SpaceBoundaries::tick(float dt)
    184190    {
     191        this->checkWhoIsIn();
    185192        this->removeAllBillboards();
    186193        COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl;
     
    188195        float distance;
    189196        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();
     197        for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ )
     198        {
     199            Pawn* currentPawn = current->get();
     200            if( currentPawn && currentPawn->getNode() )
     201            {
     202                distance = this->computeDistance(currentPawn);
     203                humanItem = this->isHumanPlayer(currentPawn);
     204                COUT(0) << "Distanz:" << distance << std::endl; // message for debugging
     205                if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an!
     206                {
     207                    COUT(0) << "You are near by the boundaries!" << std::endl; // message for debugging
     208                    if(humanItem)
     209                    {
     210                        COUT(0) << "humanItem ist true" << std::endl;
     211                        this->displayWarning("Attention! You are near by the boundaries!");
     212                    }
     213                }
     214                if( (this->maxDistance_ - distance) < this->showDistance_ )
     215                {
     216                    this->displayBoundaries(currentPawn); // Zeige Grenze an!
     217                }
     218                if(distance > this->maxDistance_ && (this->reaction_ == 1) )
     219                {
     220                    if( humanItem )
     221                    {
     222                        COUT(0) << "Health should be decreasing!" << std::endl;
     223                        this->displayWarning("You are out of the area now!");
     224                    }
     225                    currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_);
     226                }
     227                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.
     228                {
     229                    this->conditionalBounceBack(currentPawn, distance, dt);
     230                }
     231            }
     232        }
    227233    }
    228234   
     
    254260    }
    255261   
    256     void SpaceBoundaries::bounceBack(Pawn *item)
     262    void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt)
    257263    {
    258264        Vector3 normal = item->getPosition() - this->getPosition();
    259         if( item->getVelocity().dotProduct(normal) > 0 ) // Greife nur ein, falls sich das Pawn nach Aussen bewegt.
     265        normal.normalise();
     266        Vector3 velocity = item->getVelocity();
     267        float normalSpeed = item->getVelocity().dotProduct(normal);
     268       
     269        /* Checke, ob das Pawn innerhalb des nächsten Ticks, das erlaubte Gebiet verlassen würde.
     270           Falls ja: Spicke es zurück. */
     271        if( currentDistance + normalSpeed * dt > this->maxDistance_ )
    260272        {
    261273            float dampingFactor = 0.5;
    262        
    263             normal.normalise();
    264             Vector3 velocity = item->getVelocity();
    265274            velocity = velocity.reflect(normal);
    266275            Vector3 acceleration = item->getAcceleration();
  • code/branches/spaceboundaries/src/orxonox/worldentities/SpaceBoundaries.h

    r8301 r8404  
    2727 */
    2828 
    29  /* TODO:   - Sobald Bots im Spiel sind, stürzt das Programm relativ bald ab!!!
    30  */
    31  
    32  
    33  /* REALISIERUNGSIDEEN:
    34    
    35     Mehrere Instanzen:
    36         Im Konstruktor schauen, wer innerhalb der eigenen Grenzen ist und diese in eine Liste geben, die in jeder tick-Funktion
    37         durchgearbeitet wird.
    38         Moeglichkeit bereitstellen, ein Pawn durch ein Portal einer anderen Instanz von SpaceBoundaries zuzuweisen.
    39         Schauen, wie es zu handhaben ist, wenn ein neuer Spieler oder Bot nachtraeglich ins Spiel kommt.
    40  
     29 /* TODO:   - Textmessages und Billboards sollen teils nur bei einem humanPlayer angezeigt werden, nicht bei allen (vgl. Netzwerk-Spiel mit mehreren humanPlayers)
     30                beachte hierzu folgende statische Funktion: 'static unsigned int  Host::getPlayerID()'
     31                (file:///home/kmaurus/orxonox/spaceBoundaries/build/doc/api/html/classorxonox_1_1_host.html#9c1e3b39e3b42e467dfbf42902911ce2)
     32               
     33            - Kommentieren (Betrachte als Beispiel/Vorbild 'libraries/core/WeakPtr.h')
     34           
     35            - Wiki-SpaceBoundaries-Eintrag aktualisieren
    4136 */
    4237
     
    4439#define _SpaceBoundaries_H__
    4540
    46 /* Einige, spezifische include-Statements */
     41
    4742#include "core/CoreIncludes.h"
     43#include "core/WeakPtr.h"
    4844#include "tools/interfaces/Tickable.h"
    4945#include "interfaces/RadarViewable.h"
     
    5652#include <vector>
    5753
     54namespace orxonox
     55{
     56
    5857/**
    59 @brief SpaceBoundaries gives level creators the possibility to bar Pawns from leaving a defined area.
     58@brief SpaceBoundaries gives level creators the possibility to bar Pawns from leaving a defined area (until now this area is a ball).
    6059
    6160       Five attributes can/should be defined in the XML-File:
    62        - 'position' : absolute position of the SpaceBoundaries class. 'warnDistance' and 'maxDistance' refer to this 'position'.
    6361       - 'warnDistance' : If the distance between the pawn of the human player and 'position' is bigger than 'warnDistance', a message is displayed to
    6462                          inform the player that he'll soon be leaving the allowed area.
    6563       - 'maxDistance' : defines the area, where a pawn is allowed to be (radius of a ball).
    6664       - 'showDistance' : If the distance between the pawn and the boundary of the allowed area is smaller than 'showDistance', the boundary is shown.
    67        - 'healthDecrease' : a measure to define how fast the health of a pawn should decrease after leaving the allowed area.
    68                             Empfohlene Werte: 0.1 (langsame Health-Verminderung) bis 5 (sehr schnelle Health-Verminderung)
     65       - 'healthDecrease' : a measure to define how fast the health of a pawn should decrease after leaving the allowed area (unnecessary if 'reactionMode' == 0).
     66                            Recommended values: 0.1 (slow health decrease) to 5 (very fast health decrease)
     67       - 'reactionMode' : Integer-Value. Defines what effect appears if a space ship has crossed the boundaries.
     68                            0: Reflect the space ship (default).
     69                            1: Decrease Health of the space ship after having left the allowed area.
    6970*/
    7071
    71 namespace orxonox
    72 {
    7372    class _OrxonoxExport SpaceBoundaries : public StaticEntity, public Tickable
    7473    {
     
    7978            void checkWhoIsIn(); //!< Update the list 'pawnsIn_'.
    8079           
    81             void positionBillboard(const Vector3 position);
     80            void positionBillboard(const Vector3 position); //!< Display a Billboard at the position 'position'.
    8281            void setBillboardOptions(Billboard *billy);
    83             void removeAllBillboards();
     82            void removeAllBillboards(); //!< Hide all all elements of '*billboard_' and set their attribute 'usedYet' to 0.
    8483           
    8584            void setMaxDistance(float r);
     
    9493            void setHealthDecrease(float amount);
    9594            float getHealthDecrease();
     95           
     96            void setReaction(int mode);
     97            int getReaction();
    9698
    9799            void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     
    102104            struct billboardAdministration{ bool usedYet; Billboard* billy; };
    103105           
    104             std::list<Pawn*> pawnsIn_; //!< List of the pawns that this instance of SpaceBoundaries has to handle.
     106            std::list<WeakPtr<Pawn> > pawnsIn_; //!< List of the pawns that this instance of SpaceBoundaries has to handle.
    105107           
    106108            std::vector<billboardAdministration> billboards_;
    107109       
     110            int reaction_; //!< Werte: 0, 1. 0: Reflektion an Boundaries (Standard). 1: Health-Abzug-Modus.
    108111            float maxDistance_; //!< maximal zulaessige Entfernung von 'this->getPosition()'.
    109112            float warnDistance_; //!< Entfernung von 'this->getPosition()', ab der eine Warnung angezeigt wird, dass man bald das zulaessige Areal verlaesst.
     
    113116                                   //!< Empfohlene Werte: 0.1 (langsame Health-Verminderung) bis 5 (sehr schnelle Health-Verminderung)
    114117           
    115             Billboard *boundary_;
    116            
    117118            RadarViewable* centerRadar_; //!< Repraesentation von SpaceBoundaries auf dem Radar.
    118119       
     
    120121            void displayWarning(const std::string warnText);
    121122            void displayBoundaries(Pawn *item);
    122             void bounceBack(Pawn *item);
     123            void conditionalBounceBack(Pawn *item, float currentDistance, float dt);
    123124            bool isHumanPlayer(Pawn *item);
    124125           
Note: See TracChangeset for help on using the changeset viewer.