Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/infos/PlayerInfo.cc @ 1948

Last change on this file since 1948 was 1947, checked in by rgrieder, 16 years ago

updated msvc files.

File size: 5.6 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 "OrxonoxStableHeaders.h"
30#include "PlayerInfo.h"
31
32#include <OgreSceneManager.h>
33
34#include "core/CoreIncludes.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/XMLPort.h"
37
38#include "network/Host.h"
39
40#include "Settings.h"
41#include "GraphicsEngine.h"
42#include "objects/gametypes/Gametype.h"
43
44namespace orxonox
45{
46    CreateUnloadableFactory(PlayerInfo);
47
48    PlayerInfo::PlayerInfo()
49    {
50        RegisterObject(PlayerInfo);
51
52        this->ping_ = -1;
53        this->bLocalPlayer_ = Settings::isStandalone();
54        this->bLocalPlayer_ = false;
55        this->bHumanPlayer_ = false;
56        this->bFinishedSetup_ = false;
57
58        this->setConfigValues();
59        this->registerVariables();
60
61        COUT(0) << "created PlayerInfo (" << this->getObjectID() << ")" << std::endl;
62    }
63
64    PlayerInfo::~PlayerInfo()
65    {
66        Gametype* gametype = Gametype::getCurrentGametype();
67        if (gametype)
68            gametype->removePlayer(this);
69        COUT(0) << "destroyed PlayerInfo (" << this->getObjectID() << ")" << std::endl;
70    }
71
72    void PlayerInfo::setConfigValues()
73    {
74        SetConfigValue(playerName_, "Player").callback(this, &PlayerInfo::checkName);
75    }
76
77    void PlayerInfo::checkName()
78    {
79std::cout << "# PI(" << this->getObjectID() << "): checkName: " << this->bLocalPlayer_ << std::endl;
80        if (this->bLocalPlayer_ && Settings::isMaster())
81            this->setName(this->playerName_);
82    }
83
84    void PlayerInfo::changedName()
85    {
86std::cout << "# PI(" << this->getObjectID() << "): changedName to " << this->getName() << std::endl;
87    }
88
89    void PlayerInfo::registerVariables()
90    {
91        REGISTERSTRING(name_,         network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
92        REGISTERSTRING(playerName_,   network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName));
93        REGISTERDATA(clientID_,       network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID));
94        REGISTERDATA(ping_,           network::direction::toclient);
95        REGISTERDATA(bHumanPlayer_,   network::direction::toclient);
96        REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup));
97    }
98
99    void PlayerInfo::clientChangedName()
100    {
101std::cout << "# PI(" << this->getObjectID() << "): clientChangedName() to " << this->playerName_ << std::endl;
102        this->setName(this->playerName_);
103    }
104
105    void PlayerInfo::checkClientID()
106    {
107std::cout << "# PI(" << this->getObjectID() << "): checkClientID()" << std::endl;
108        this->bHumanPlayer_ = true;
109
110        if (this->clientID_ == network::Host::getPlayerID())
111        {
112std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): it's the client's ID" << std::endl;
113            this->bLocalPlayer_ = true;
114//std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): name: " << this->getName() << std::endl;
115
116            if (Settings::isClient())
117            {
118std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're on a client: set object mode to bidirectional" << std::endl;
119                this->setObjectMode(network::direction::bidirectional);
120                this->playerName_ += "blub";
121std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): proposed name: " << this->playerName_ << std::endl;
122            }
123            else
124            {
125std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're not on a client: finish setup" << std::endl;
126                this->clientChangedName();
127                this->bFinishedSetup_ = true;
128                this->finishedSetup();
129            }
130        }
131    }
132
133    void PlayerInfo::finishedSetup()
134    {
135std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): " << this->bFinishedSetup_ << std::endl;
136        if (Settings::isClient())
137        {
138std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a client: finish setup" << std::endl;
139            this->bFinishedSetup_ = true;
140        }
141        else if (this->bFinishedSetup_)
142        {
143std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: add player" << std::endl;
144            Gametype* gametype = Gametype::getCurrentGametype();
145            if (gametype)
146                gametype->addPlayer(this);
147        }
148        else
149        {
150std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: client not yet finished" << std::endl;
151        }
152    }
153}
Note: See TracBrowser for help on using the repository browser.