Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/miniprojects/src/orxonox/objects/infos/PlayerInfo.cc @ 2789

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