Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/infos/PlayerInfo.cc @ 3131

Last change on this file since 3131 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 6.1 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 "PlayerInfo.h"
32
33#include "core/CoreIncludes.h"
34#include "network/ClientInformation.h"
35#include "objects/gametypes/Gametype.h"
36
37namespace orxonox
38{
39    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
40    {
41        RegisterObject(PlayerInfo);
42
43        this->clientID_ = CLIENTID_UNKNOWN;
44        this->bHumanPlayer_ = false;
45        this->bLocalPlayer_ = false;
46        this->bReadyToSpawn_ = false;
47        this->bSetUnreadyAfterSpawn_ = true;
48        this->controller_ = 0;
49        this->controllableEntity_ = 0;
50        this->controllableEntityID_ = CLIENTID_UNKNOWN;
51
52        this->gtinfo_ = 0;
53        this->gtinfoID_ = OBJECTID_UNKNOWN;
54        this->updateGametypeInfo();
55
56        this->registerVariables();
57    }
58
59    PlayerInfo::~PlayerInfo()
60    {
61        if (this->BaseObject::isInitialized())
62        {
63            this->stopControl();
64
65            if (this->controller_)
66            {
67                delete this->controller_;
68                this->controller_ = 0;
69            }
70
71            if (this->getGametype())
72                this->getGametype()->playerLeft(this);
73        }
74    }
75
76    void PlayerInfo::registerVariables()
77    {
78        registerVariable(this->name_,                 variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
79        registerVariable(this->controllableEntityID_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
80        registerVariable(this->bReadyToSpawn_,        variableDirection::toserver);
81        registerVariable(this->gtinfoID_,             variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID));
82    }
83
84    void PlayerInfo::changedName()
85    {
86        SUPER(PlayerInfo, changedName);
87
88        if (this->isInitialized() && this->getGametype())
89            this->getGametype()->playerChangedName(this);
90    }
91
92    void PlayerInfo::changedGametype()
93    {
94        this->updateGametypeInfo();
95
96        if (this->isInitialized())
97        {
98            if (this->getOldGametype())
99            {
100                if (this->getGametype())
101                    this->getOldGametype()->playerSwitched(this, this->getGametype());
102                else
103                    this->getOldGametype()->playerLeft(this);
104            }
105
106            if (this->getGametype())
107            {
108                if (this->getOldGametype())
109                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
110                else
111                    this->getGametype()->playerEntered(this);
112            }
113        }
114    }
115
116    void PlayerInfo::updateGametypeInfo()
117    {
118        this->gtinfo_ = 0;
119        this->gtinfoID_ = OBJECTID_UNKNOWN;
120
121        if (this->getGametype() && this->getGametype()->getGametypeInfo())
122        {
123            this->gtinfo_ = this->getGametype()->getGametypeInfo();
124            this->gtinfoID_ = this->gtinfo_->getObjectID();
125        }
126    }
127
128    void PlayerInfo::createController()
129    {
130        if (this->controller_)
131        {
132            delete this->controller_;
133            this->controller_ = 0;
134        }
135        this->controller_ = this->defaultController_.fabricate(this);
136        assert(this->controller_);
137        this->controller_->setPlayer(this);
138        if (this->controllableEntity_)
139            this->controller_->setControllableEntity(this->controllableEntity_);
140        this->changedController();
141    }
142
143    void PlayerInfo::startControl(ControllableEntity* entity)
144    {
145        if (!entity || entity == this->controllableEntity_)
146            return;
147
148        if (this->controllableEntity_)
149            this->stopControl();
150
151        this->controllableEntity_ = entity;
152        this->controllableEntityID_ = entity->getObjectID();
153
154        entity->setPlayer(this);
155
156        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
157
158        if (this->controller_)
159            this->controller_->setControllableEntity(entity);
160
161        this->changedControllableEntity();
162    }
163
164    void PlayerInfo::stopControl()
165    {
166        ControllableEntity* entity = this->controllableEntity_;
167
168        if (!entity)
169            return;
170
171        this->controllableEntity_ = 0;
172        this->controllableEntityID_ = OBJECTID_UNKNOWN;
173
174        if (this->controller_)
175            this->controller_->setControllableEntity(0);
176
177        entity->removePlayer();
178
179        this->changedControllableEntity();
180    }
181
182    void PlayerInfo::networkcallback_changedcontrollableentityID()
183    {
184        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
185        {
186            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
187            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
188            this->startControl(entity);
189        }
190        else
191        {
192            this->stopControl();
193        }
194    }
195
196    void PlayerInfo::networkcallback_changedgtinfoID()
197    {
198        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
199        {
200            this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
201
202            if (!this->gtinfo_)
203                this->gtinfoID_ = OBJECTID_UNKNOWN;
204        }
205    }
206}
Note: See TracBrowser for help on using the repository browser.