Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 27, 2008, 4:08:51 AM (16 years ago)
Author:
landauf
Message:

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

Location:
code/branches/objecthierarchy/src/orxonox/objects/gametypes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.cc

    r2006 r2019  
    3030#include "Gametype.h"
    3131
     32#include <cstdlib>
     33#include <ctime>
     34
    3235#include "core/CoreIncludes.h"
    3336#include "objects/infos/PlayerInfo.h"
    3437#include "objects/worldentities/pawns/Spectator.h"
     38#include "objects/worldentities/SpawnPoint.h"
    3539
    3640#include "network/Host.h"
     
    4044    CreateUnloadableFactory(Gametype);
    4145
    42     Gametype::Gametype()
     46    Gametype::Gametype(BaseObject* creator) : BaseObject(creator)
    4347    {
    4448        RegisterObject(Gametype);
    4549
    4650        this->defaultPawn_ = Class(Spectator);
     51        this->bStarted_ = false;
     52        this->bEnded_ = false;
     53        this->bAutoStart_ = false;
    4754
    4855        COUT(0) << "created Gametype" << std::endl;
    4956    }
    5057
    51     void Gametype::addPlayer(PlayerInfo* player)
     58    void Gametype::tick(float dt)
     59    {
     60        if (!this->bStarted_)
     61            this->checkStart();
     62
     63        this->assignDefaultPawnsIfNeeded();
     64    }
     65
     66    void Gametype::start()
     67    {
     68        COUT(0) << "game started" << std::endl;
     69        this->bStarted_ = true;
     70
     71        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     72            this->spawnPlayer(*it);
     73    }
     74
     75    void Gametype::end()
     76    {
     77        COUT(0) << "game ended" << std::endl;
     78        this->bEnded_ = true;
     79    }
     80
     81    void Gametype::playerEntered(PlayerInfo* player)
    5282    {
    5383        this->players_.insert(player);
    54         this->playerJoined(player);
    55 
    56         ControllableEntity* newpawn = this->defaultPawn_.fabricate();
    57         player->startControl(newpawn);
    58     }
    59 
    60     void Gametype::removePlayer(PlayerInfo* player)
    61     {
    62         if (this->players_.find(player) != this->players_.end())
    63         {
    64             player->stopControl();
    65             this->players_.erase(player);
    66             this->playerLeft(player);
    67         }
    68     }
    69 
    70     void Gametype::playerJoined(PlayerInfo* player)
    71     {
     84
    7285        std::string message = player->getName() + " entered the game";
    7386        COUT(0) << message << std::endl;
     
    7790    void Gametype::playerLeft(PlayerInfo* player)
    7891    {
    79         std::string message = player->getName() + " left the game";
    80         COUT(0) << message << std::endl;
    81         network::Host::Broadcast(message);
     92        std::set<PlayerInfo*>::iterator it = this->players_.find(player);
     93        if (it != this->players_.end())
     94        {
     95            this->players_.erase(it);
     96
     97            std::string message = player->getName() + " left the game";
     98            COUT(0) << message << std::endl;
     99            network::Host::Broadcast(message);
     100        }
     101    }
     102
     103    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
     104    {
     105    }
     106
     107    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
     108    {
    82109    }
    83110
     
    94121        }
    95122    }
     123
     124    void Gametype::playerSpawned(PlayerInfo* player)
     125    {
     126    }
     127
     128    void Gametype::playerDied(PlayerInfo* player)
     129    {
     130    }
     131
     132    void Gametype::playerScored(PlayerInfo* player)
     133    {
     134    }
     135
     136    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
     137    {
     138        if (this->spawnpoints_.size() > 0)
     139        {
     140            srand(time(0));
     141            rnd();
     142
     143            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
     144            unsigned int index = 0;
     145            for (std::set<SpawnPoint*>::iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
     146            {
     147                if (index == randomspawn)
     148                    return (*it);
     149
     150                ++index;
     151            }
     152        }
     153        return 0;
     154    }
     155
     156    void Gametype::assignDefaultPawnsIfNeeded() const
     157    {
     158        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     159        {
     160            if (!(*it)->getControllableEntity() && (!(*it)->isReadyToSpawn() || !this->bStarted_))
     161            {
     162                SpawnPoint* spawn = this->getBestSpawnPoint(*it);
     163                if (spawn)
     164                {
     165                    // force spawn at spawnpoint with default pawn
     166                    ControllableEntity* newpawn = this->defaultPawn_.fabricate(spawn);
     167                    spawn->spawn(newpawn);
     168                    (*it)->startControl(newpawn);
     169                }
     170                else
     171                {
     172                    COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
     173                    abort();
     174                }
     175            }
     176        }
     177    }
     178
     179    void Gametype::checkStart()
     180    {
     181        if (!this->bStarted_)
     182        {
     183            if (this->players_.size() > 0)
     184            {
     185                if (this->bAutoStart_)
     186                {
     187                    this->start();
     188                }
     189                else
     190                {
     191                    bool allplayersready = true;
     192                    for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     193                    {
     194                        if (!(*it)->isReadyToSpawn())
     195                            allplayersready = false;
     196                    }
     197                    if (allplayersready)
     198                        this->start();
     199                }
     200            }
     201        }
     202    }
     203
     204    void Gametype::spawnPlayer(PlayerInfo* player)
     205    {
     206        if (player->isReadyToSpawn())
     207        {
     208            SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
     209            if (spawnpoint)
     210            {
     211                player->startControl(spawnpoint->spawn());
     212            }
     213            else
     214            {
     215                COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
     216                abort();
     217            }
     218        }
     219    }
    96220}
  • code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.h

    r2006 r2019  
    3737#include "core/Identifier.h"
    3838#include "objects/worldentities/ControllableEntity.h"
     39#include "objects/Tickable.h"
    3940
    4041namespace orxonox
    4142{
    42     class _OrxonoxExport Gametype : public BaseObject
     43    class _OrxonoxExport Gametype : public BaseObject, public Tickable
    4344    {
    4445        friend class PlayerInfo;
    4546
    4647        public:
    47             Gametype();
     48            Gametype(BaseObject* creator);
    4849            virtual ~Gametype() {}
     50
     51            virtual void tick(float dt);
     52
     53            virtual void start();
     54            virtual void end();
     55            virtual void playerEntered(PlayerInfo* player);
     56            virtual void playerLeft(PlayerInfo* player);
     57            virtual void playerSwitched(PlayerInfo* player, Gametype* newgametype);
     58            virtual void playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype);
     59            virtual void playerChangedName(PlayerInfo* player);
     60            virtual void playerSpawned(PlayerInfo* player);
     61            virtual void playerDied(PlayerInfo* player);
     62            virtual void playerScored(PlayerInfo* player);
    4963
    5064            inline const std::set<PlayerInfo*>& getPlayers() const
    5165                { return this->players_; }
    5266
    53         protected:
    54             virtual void playerJoined(PlayerInfo* player);
    55             virtual void playerLeft(PlayerInfo* player);
    56 
    57             virtual void playerChangedName(PlayerInfo* player);
     67            inline void registerSpawnPoint(SpawnPoint* spawnpoint)
     68                { this->spawnpoints_.insert(spawnpoint); }
    5869
    5970        private:
     71            virtual SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const;
     72
    6073            void addPlayer(PlayerInfo* player);
    6174            void removePlayer(PlayerInfo* player);
    6275
     76            void assignDefaultPawnsIfNeeded() const;
     77            void checkStart();
     78            void spawnPlayer(PlayerInfo* player);
     79
     80            bool bStarted_;
     81            bool bEnded_;
     82            bool bAutoStart_;
    6383            std::set<PlayerInfo*> players_;
     84            std::set<SpawnPoint*> spawnpoints_;
    6485            SubclassIdentifier<ControllableEntity> defaultPawn_;
    6586    };
Note: See TracChangeset for help on using the changeset viewer.