Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 27, 2008, 4:08:51 AM (17 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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.cc

    r2006 r2019  
    2727 */
    2828
     29#include <cassert>
     30
    2931#include "OrxonoxStableHeaders.h"
    3032#include "PlayerInfo.h"
    3133
    32 #include <OgreSceneManager.h>
    33 
    3434#include "core/CoreIncludes.h"
    35 #include "core/ConfigValueIncludes.h"
    36 #include "core/XMLPort.h"
    37 #include "core/Core.h"
    38 
    39 #include "network/Host.h"
    4035#include "network/ClientInformation.h"
    41 
    42 #include "GraphicsEngine.h"
    4336#include "objects/gametypes/Gametype.h"
    44 #include "objects/worldentities/ControllableEntity.h"
    45 #include "objects/controllers/HumanController.h"
    4637
    4738namespace orxonox
    4839{
    49     CreateUnloadableFactory(PlayerInfo);
    50 
    51     PlayerInfo::PlayerInfo()
     40    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
    5241    {
    5342        RegisterObject(PlayerInfo);
    5443
    55         this->ping_ = -1;
    5644        this->clientID_ = network::CLIENTID_UNKNOWN;
     45        this->bHumanPlayer_ = false;
    5746        this->bLocalPlayer_ = false;
    58         this->bHumanPlayer_ = false;
    59         this->bFinishedSetup_ = false;
    60         this->gametype_ = 0;
     47        this->bReadyToSpawn_ = false;
     48        this->controller_ = 0;
     49        this->controllableEntity_ = 0;
     50        this->controllableEntityID_ = network::CLIENTID_UNKNOWN;
    6151
    62         this->pawn_ = 0;
    63         this->pawnID_ = network::OBJECTID_UNKNOWN;
    64         this->controller_ = 0;
    65         this->setDefaultController(Class(HumanController));
    66 
    67         this->setConfigValues();
    6852        this->registerVariables();
    6953    }
     
    7357        if (this->isInitialized())
    7458        {
    75             if (this->gametype_)
    76                 this->gametype_->removePlayer(this);
     59            this->stopControl(this->controllableEntity_);
    7760
    7861            if (this->controller_)
     62            {
    7963                delete this->controller_;
    80 
    81             if (this->pawn_)
    82                 this->pawn_->removePlayer();
     64                this->controller_ = 0;
     65            }
    8366        }
    8467    }
    8568
    86     void PlayerInfo::setConfigValues()
     69    void PlayerInfo::registerVariables()
    8770    {
    88         SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick);
    89     }
    90 
    91     void PlayerInfo::checkNick()
    92     {
    93         if (this->bLocalPlayer_)
    94         {
    95             this->playerName_ = this->nick_;
    96 
    97             if (Core::isMaster())
    98                 this->setName(this->playerName_);
    99         }
     71        REGISTERSTRING(this->name_,                 network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
     72        REGISTERDATA  (this->controllableEntityID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
    10073    }
    10174
    10275    void PlayerInfo::changedName()
    10376    {
    104         if (this->gametype_)
    105             this->gametype_->playerChangedName(this);
     77        if (this->isReady() && this->getGametype())
     78            this->getGametype()->playerChangedName(this);
    10679    }
    10780
    108     void PlayerInfo::registerVariables()
     81    void PlayerInfo::changedGametype()
    10982    {
    110         REGISTERSTRING(name_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
    111         REGISTERSTRING(playerName_,   network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName));
    112         REGISTERDATA(clientID_,       network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID));
    113         REGISTERDATA(ping_,           network::direction::toclient);
    114         REGISTERDATA(bHumanPlayer_,   network::direction::toclient);
    115         REGISTERDATA(pawnID_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn));
    116         REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup));
    117     }
     83        if (this->isReady())
     84        {
     85            if (this->getOldGametype())
     86            {
     87                if (this->getGametype())
     88                    this->getOldGametype()->playerSwitched(this, this->getGametype());
     89                else
     90                    this->getOldGametype()->playerLeft(this);
     91            }
    11892
    119     void PlayerInfo::clientChangedName()
    120     {
    121         this->setName(this->playerName_);
    122     }
    123 
    124     void PlayerInfo::checkClientID()
    125     {
    126         this->bHumanPlayer_ = true;
    127 
    128         if (this->clientID_ == network::Host::getPlayerID())
    129         {
    130             this->takeLocalControl();
    131 
    132             if (Core::isClient())
    133                 this->setObjectMode(network::direction::bidirectional);
    134             else
     93            if (this->getGametype())
    13594            {
    136                 this->clientChangedName();
    137                 this->bFinishedSetup_ = true;
    138                 this->finishedSetup();
     95                if (this->getOldGametype())
     96                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
     97                else
     98                    this->getGametype()->playerEntered(this);
    13999            }
    140100        }
    141101    }
    142102
    143     void PlayerInfo::finishedSetup()
     103    void PlayerInfo::createController()
    144104    {
    145         if (Core::isClient())
    146             this->bFinishedSetup_ = true;
    147         else if (this->bFinishedSetup_)
     105        this->controller_ = this->defaultController_.fabricate(this);
     106        assert(this->controller_);
     107        this->controller_->setPlayer(this);
     108        if (this->controllableEntity_)
     109            this->controller_->setControllableEntity(this->controllableEntity_);
     110    }
     111
     112    void PlayerInfo::startControl(ControllableEntity* entity)
     113    {
     114        if (this->controllableEntity_)
     115            this->stopControl(this->controllableEntity_);
     116
     117        this->controllableEntity_ = entity;
     118
     119        if (entity)
    148120        {
    149             if (this->gametype_)
    150                 this->gametype_->addPlayer(this);
     121            this->controllableEntityID_ = entity->getObjectID();
     122            entity->setPlayer(this);
     123        }
     124        else
     125        {
     126            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
     127        }
     128
     129        if (this->controller_)
     130            this->controller_->setControllableEntity(entity);
     131    }
     132
     133    void PlayerInfo::stopControl(ControllableEntity* entity)
     134    {
     135        if (entity && this->controllableEntity_ == entity)
     136        {
     137            this->controllableEntity_ = 0;
     138            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
     139
     140            if (this->controller_)
     141                this->controller_->setControllableEntity(0);
     142
     143            entity->removePlayer();
    151144        }
    152145    }
    153146
    154     void PlayerInfo::startControl(ControllableEntity* pawn)
     147    void PlayerInfo::networkcallback_changedcontrollableentityID()
    155148    {
    156         pawn->setPlayer(this);
    157         this->pawn_ = pawn;
    158         this->pawnID_ = pawn->getObjectID();
     149        if (this->controllableEntityID_ != network::OBJECTID_UNKNOWN)
     150        {
     151            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
     152            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
    159153
    160         if (this->controller_)
    161             this->controller_->setPawn(this->pawn_);
    162     }
    163 
    164     void PlayerInfo::stopControl()
    165     {
    166         if (this->pawn_)
    167             this->pawn_->removePlayer();
    168         this->pawn_ = 0;
    169         this->pawnID_ = network::OBJECTID_UNKNOWN;
    170     }
    171 
    172     void PlayerInfo::takeLocalControl()
    173     {
    174         this->bLocalPlayer_ = true;
    175         this->playerName_ = this->nick_;
    176         this->createController();
    177     }
    178 
    179     void PlayerInfo::createController()
    180     {
    181         this->controller_ = this->defaultController_.fabricate();
    182         this->controller_->setPawn(this->pawn_);
    183     }
    184 
    185     void PlayerInfo::updatePawn()
    186     {
    187         this->pawn_ = dynamic_cast<ControllableEntity*>(network::Synchronisable::getSynchronisable(this->pawnID_));
    188         if (this->pawn_ && (this->pawn_->getPlayer() != this))
    189             this->pawn_->setPlayer(this);
     154            this->startControl(entity);
     155        }
     156        else
     157        {
     158            this->stopControl(this->controllableEntity_);
     159        }
    190160    }
    191161}
Note: See TracChangeset for help on using the changeset viewer.