Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/infos/PlayerInfo.cc @ 2327

Last change on this file since 2327 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

  • Property svn:eol-style set to native
File size: 5.0 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include <cassert>
30
31#include "OrxonoxStableHeaders.h"
32#include "PlayerInfo.h"
33
34#include "core/CoreIncludes.h"
35#include "network/ClientInformation.h"
36#include "objects/gametypes/Gametype.h"
37
38namespace orxonox
39{
40    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
41    {
42        RegisterObject(PlayerInfo);
43
44        this->clientID_ = network::CLIENTID_UNKNOWN;
45        this->bHumanPlayer_ = false;
46        this->bLocalPlayer_ = false;
47        this->bReadyToSpawn_ = false;
48        this->controller_ = 0;
49        this->controllableEntity_ = 0;
50        this->controllableEntityID_ = network::CLIENTID_UNKNOWN;
51
52        this->registerVariables();
53    }
54
55    PlayerInfo::~PlayerInfo()
56    {
57        if (this->isInitialized())
58        {
59            this->stopControl(this->controllableEntity_);
60
61            if (this->controller_)
62            {
63                delete this->controller_;
64                this->controller_ = 0;
65            }
66        }
67    }
68
69    void PlayerInfo::registerVariables()
70    {
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));
73        REGISTERDATA  (this->bReadyToSpawn_,        network::direction::toserver);
74    }
75
76    void PlayerInfo::changedName()
77    {
78        if (this->isReady() && this->getGametype())
79            this->getGametype()->playerChangedName(this);
80    }
81
82    void PlayerInfo::changedGametype()
83    {
84        if (this->isReady())
85        {
86            if (this->getOldGametype())
87            {
88                if (this->getGametype())
89                    this->getOldGametype()->playerSwitched(this, this->getGametype());
90                else
91                    this->getOldGametype()->playerLeft(this);
92            }
93
94            if (this->getGametype())
95            {
96                if (this->getOldGametype())
97                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
98                else
99                    this->getGametype()->playerEntered(this);
100            }
101        }
102    }
103
104    void PlayerInfo::createController()
105    {
106        this->controller_ = this->defaultController_.fabricate(this);
107        assert(this->controller_);
108        this->controller_->setPlayer(this);
109        if (this->controllableEntity_)
110            this->controller_->setControllableEntity(this->controllableEntity_);
111    }
112
113    void PlayerInfo::startControl(ControllableEntity* entity)
114    {
115        if (this->controllableEntity_)
116            this->stopControl(this->controllableEntity_);
117
118        this->controllableEntity_ = entity;
119
120        if (entity)
121        {
122            this->controllableEntityID_ = entity->getObjectID();
123            entity->setPlayer(this);
124            this->bReadyToSpawn_ = false;
125        }
126        else
127        {
128            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
129        }
130
131        if (this->controller_)
132            this->controller_->setControllableEntity(entity);
133    }
134
135    void PlayerInfo::stopControl(ControllableEntity* entity, bool callback)
136    {
137        if (entity && this->controllableEntity_ == entity)
138        {
139            this->controllableEntity_ = 0;
140            this->controllableEntityID_ = network::OBJECTID_UNKNOWN;
141
142            if (this->controller_)
143                this->controller_->setControllableEntity(0);
144
145            if (callback)
146                entity->removePlayer();
147        }
148    }
149
150    void PlayerInfo::networkcallback_changedcontrollableentityID()
151    {
152        if (this->controllableEntityID_ != network::OBJECTID_UNKNOWN)
153        {
154            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
155            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
156
157            this->startControl(entity);
158        }
159        else
160        {
161            this->stopControl(this->controllableEntity_);
162        }
163    }
164}
Note: See TracBrowser for help on using the repository browser.