Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/toonshader/src/orxonox/objects/infos/PlayerInfo.cc @ 3825

Last change on this file since 3825 was 2839, checked in by landauf, 16 years ago

added AI for the Pong gametype
improved Pong gameplay

  • Property svn:eol-style set to native
File size: 5.5 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_ = CLIENTID_UNKNOWN;
45        this->bHumanPlayer_ = false;
46        this->bLocalPlayer_ = false;
47        this->bReadyToSpawn_ = false;
48        this->bSetUnreadyAfterSpawn_ = true;
49        this->controller_ = 0;
50        this->controllableEntity_ = 0;
51        this->controllableEntityID_ = CLIENTID_UNKNOWN;
52
53        this->registerVariables();
54    }
55
56    PlayerInfo::~PlayerInfo()
57    {
58        if (this->BaseObject::isInitialized())
59        {
60            this->stopControl(this->controllableEntity_);
61
62            if (this->controller_)
63            {
64                delete this->controller_;
65                this->controller_ = 0;
66            }
67
68            if (this->getGametype())
69                this->getGametype()->playerLeft(this);
70        }
71    }
72
73    void PlayerInfo::registerVariables()
74    {
75        registerVariable(this->name_,                 variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
76        registerVariable(this->controllableEntityID_, variableDirection::toclient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
77        registerVariable(this->bReadyToSpawn_,        variableDirection::toserver);
78    }
79
80    void PlayerInfo::changedName()
81    {
82        SUPER(PlayerInfo, changedName);
83
84        if (this->isInitialized() && this->getGametype())
85            this->getGametype()->playerChangedName(this);
86    }
87
88    void PlayerInfo::changedGametype()
89    {
90        if (this->isInitialized())
91        {
92            if (this->getOldGametype())
93            {
94                if (this->getGametype())
95                    this->getOldGametype()->playerSwitched(this, this->getGametype());
96                else
97                    this->getOldGametype()->playerLeft(this);
98            }
99
100            if (this->getGametype())
101            {
102                if (this->getOldGametype())
103                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
104                else
105                    this->getGametype()->playerEntered(this);
106            }
107        }
108    }
109
110    void PlayerInfo::createController()
111    {
112        if (this->controller_)
113        {
114            delete this->controller_;
115            this->controller_ = 0;
116        }
117        this->controller_ = this->defaultController_.fabricate(this);
118        assert(this->controller_);
119        this->controller_->setPlayer(this);
120        if (this->controllableEntity_)
121            this->controller_->setControllableEntity(this->controllableEntity_);
122        this->changedController();
123    }
124
125    void PlayerInfo::startControl(ControllableEntity* entity, bool callback)
126    {
127        if (entity == this->controllableEntity_)
128            return;
129
130        if (this->controllableEntity_)
131            this->stopControl(this->controllableEntity_, callback);
132
133        this->controllableEntity_ = entity;
134
135        if (entity)
136        {
137            this->controllableEntityID_ = entity->getObjectID();
138            entity->setPlayer(this);
139            this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
140        }
141        else
142        {
143            this->controllableEntityID_ = OBJECTID_UNKNOWN;
144        }
145
146        if (this->controller_)
147            this->controller_->setControllableEntity(entity);
148
149        this->changedControllableEntity();
150    }
151
152    void PlayerInfo::stopControl(ControllableEntity* entity, bool callback)
153    {
154        if (entity && this->controllableEntity_ == entity)
155        {
156            this->controllableEntity_ = 0;
157            this->controllableEntityID_ = OBJECTID_UNKNOWN;
158
159            if (this->controller_)
160                this->controller_->setControllableEntity(0);
161
162            if (callback)
163                entity->removePlayer();
164
165            this->changedControllableEntity();
166        }
167    }
168
169    void PlayerInfo::networkcallback_changedcontrollableentityID()
170    {
171        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
172        {
173            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
174            ControllableEntity* entity = dynamic_cast<ControllableEntity*>(temp);
175
176            this->startControl(entity);
177        }
178        else
179        {
180            this->stopControl(this->controllableEntity_);
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.