Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding/src/orxonox/gametypes/LastManStanding.cc @ 7581

Last change on this file since 7581 was 7581, checked in by jo, 14 years ago

Almost finished. Yeay.

  • Property svn:eol-style set to native
File size: 9.2 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 *      Johannes Ritz
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LastManStanding.h"
30
31#include "core/CoreIncludes.h"
32#include "network/Host.h"
33#include "infos/PlayerInfo.h"
34#include "worldentities/pawns/Pawn.h"
35#include "core/ConfigValueIncludes.h"
36#include "util/Convert.h"
37
38namespace orxonox
39{
40    CreateUnloadableFactory(LastManStanding);
41
42    LastManStanding::LastManStanding(BaseObject* creator) : Gametype(creator)
43    {
44        RegisterObject(LastManStanding);
45        this->bForceSpawn_=true;
46        this->lives=4;
47        this->playersAlive=0;
48        this->timeRemaining=20.0f;
49        this->setHUDTemplate("LastmanstandingHUD");
50    }
51
52    void LastManStanding::spawnDeadPlayersIfRequested()
53    {
54        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
55            if (it->second.state_ == PlayerState::Dead)
56            {
57                bool alive = (0<playerLives_[it->first]);
58                if (alive&&(it->first->isReadyToSpawn() || this->bForceSpawn_))
59                    this->spawnPlayer(it->first);
60             }
61    }
62
63
64    void LastManStanding::setConfigValues()
65    {
66        SetConfigValue(lives, 4);
67        SetConfigValue(timeRemaining, 20.0f);
68    }
69
70    bool LastManStanding::allowPawnDamage(Pawn* victim, Pawn* originator)
71    {
72        if (originator && originator->getPlayer())// only for safety
73        {
74            this->timeToAct_[originator->getPlayer()]=timeRemaining;
75        }
76
77        return true;
78    }
79
80    bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator)
81    {
82        if (!victim||!victim->getPlayer())// only for safety
83            return true;
84        playerLives_[victim->getPlayer()]=playerLives_[victim->getPlayer()]-1;
85        if (playerLives_[victim->getPlayer()]<=0)//if player lost all lives
86        {
87            this->playersAlive--;
88            const std::string& message = victim->getPlayer()->getName() + " has lost all lives";
89            COUT(0) << message << std::endl;
90            Host::Broadcast(message);
91        }
92        return true;
93    }
94
95    void LastManStanding::start()
96    {
97        Gametype::start();
98
99        std::string message("Try to survive!");
100        COUT(0) << message << std::endl;
101        Host::Broadcast(message);
102    }
103
104    void LastManStanding::end()
105    {
106        Gametype::end();
107       
108        for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it)
109        {
110            if (it->first->getClientID() == CLIENTID_UNKNOWN)
111                continue;
112
113            if (it->second > 0)
114                this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID());
115            else
116                this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID());
117        }
118    }
119
120    void LastManStanding::playerEntered(PlayerInfo* player)
121    {
122        if (!player)// only for safety
123            return;
124        Gametype::playerEntered(player);
125
126        playerLives_[player]=lives;
127        this->playersAlive++;
128        this->timeToAct_[player]=timeRemaining;
129        const std::string& message = player->getName() + " entered the game";
130        COUT(0) << message << std::endl;
131        Host::Broadcast(message);
132        //Update: EachPlayer's "Players in Game"-HUD
133        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
134        {
135            if (it->first->getClientID() == CLIENTID_UNKNOWN)
136                continue;
137            const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive);
138            this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
139        }
140       
141    }
142
143    bool LastManStanding::playerLeft(PlayerInfo* player)
144    {
145        bool valid_player = Gametype::playerLeft(player);
146
147        if (valid_player)
148        {
149            this->playersAlive--;
150            //this->playerLives_[player].erase (player); not necessary?
151            //
152            const std::string& message = player->getName() + " left the game";
153            COUT(0) << message << std::endl;
154            Host::Broadcast(message);
155            //Update: EachPlayer's "Players in Game"-HUD
156            for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
157            {
158                if (it->first->getClientID() == CLIENTID_UNKNOWN)
159                    continue;
160                const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive);
161                this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
162            }
163        }
164
165        return valid_player;
166    }
167
168    bool LastManStanding::playerChangedName(PlayerInfo* player)
169    {
170        bool valid_player = Gametype::playerChangedName(player);
171
172        if (valid_player)
173        {
174            const std::string& message = player->getOldName() + " changed name to " + player->getName();
175            COUT(0) << message << std::endl;
176            Host::Broadcast(message);
177        }
178
179        return valid_player;
180    }
181
182    void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
183    {
184        if (!player)
185            return;
186        //Update: Individual Players "lifes"-HUD
187        std::map<PlayerInfo*, Player>::iterator it2 = this->players_.find(player);
188        if (it2 != this->players_.end())
189        {
190            const std::string& message = "Your Lives: " +multi_cast<std::string>(playerLives_[player]);
191            this->gtinfo_->sendFadingMessage(message,it2->first->getClientID());
192
193        }
194    }
195
196    void LastManStanding::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn)
197    {
198        //Update: EachPlayer's "Players in Game"-HUD
199        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
200        {
201            if (it->first->getClientID() == CLIENTID_UNKNOWN)
202                continue;
203            const std::string& message1 = "Remaining Players : "+ multi_cast<std::string>(playersAlive);
204            this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
205        }
206   
207    }
208
209    void LastManStanding::pawnKilled(Pawn* victim, Pawn* killer)
210    {
211        if (victim && victim->getPlayer())
212        {
213            std::string message;
214            if (killer)
215            {
216                if (killer->getPlayer())
217                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
218                else
219                    message = victim->getPlayer()->getName() + " was killed";
220            }
221            else
222                message = victim->getPlayer()->getName() + " died";
223
224            COUT(0) << message << std::endl;
225            Host::Broadcast(message);
226        }
227
228        Gametype::pawnKilled(victim, killer);
229    }
230
231    const int LastManStanding::playerGetLives(PlayerInfo* player)
232    {
233        if (player)
234            return  playerLives_[player];
235        else
236            return 0;
237    }
238
239    void LastManStanding::killPlayer(PlayerInfo* player)
240    {
241        if(!player)
242            return;
243        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
244        if (it != this->players_.end())
245        {
246            if(!player->getControllableEntity())
247                {return;}
248            Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity());
249            if(!pawn)
250                {return;}
251            pawn->kill();
252            this->timeToAct_[player]=timeRemaining+3.0f;//reset timer
253        }
254    }
255   
256    void LastManStanding::tick(float dt)
257    {
258        SUPER(LastManStanding, tick, dt);
259        if(this->hasStarted()&&(!this->hasEnded()))
260        {
261            if ((this->hasStarted()&&(playersAlive<=1)))//last player remaining
262            {
263            this->end();
264            }
265            for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it)
266            {       
267                it->second-=dt;
268                if (it->second<0.0f)
269                {
270                    it->second=timeRemaining+3.0f;//reset timer
271                    if (playerGetLives(it->first)>0)
272                        this->killPlayer(it->first);
273                }
274            }
275        }
276    }
277
278}
Note: See TracBrowser for help on using the repository browser.