Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

HUD bug removed.

  • Property svn:eol-style set to native
File size: 8.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 *      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) : Deathmatch(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        return true;
77    }
78
79    bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator)
80    {
81        if (!victim||!victim->getPlayer())// only for safety
82            return true;
83        playerLives_[victim->getPlayer()]=playerLives_[victim->getPlayer()]-1;
84        if (playerLives_[victim->getPlayer()]<=0)//if player lost all lives
85        {
86            this->playersAlive--;
87            const std::string& message = victim->getPlayer()->getName() + " has lost all lives";
88            COUT(0) << message << std::endl;
89            Host::Broadcast(message);
90        }
91        return true;
92    }
93
94    void LastManStanding::end()
95    {
96        Gametype::end();
97       
98        for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it)
99        {
100            if (it->first->getClientID() == CLIENTID_UNKNOWN)
101                continue;
102
103            if (it->second > 0)
104                this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID());
105            else
106                this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID());
107        }
108    }
109
110    void LastManStanding::playerEntered(PlayerInfo* player)
111    {
112        if (!player)// only for safety
113            return;
114        Deathmatch::playerEntered(player);
115
116        playerLives_[player]=lives;
117        this->playersAlive++;
118        this->timeToAct_[player]=timeRemaining;
119        //Update: EachPlayer's "Players in Game"-HUD
120        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
121        {
122            if (it->first->getClientID() == CLIENTID_UNKNOWN)
123                continue;
124            const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive);
125            this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
126        }
127    }
128
129    bool LastManStanding::playerLeft(PlayerInfo* player)
130    {
131        bool valid_player = Deathmatch::playerLeft(player);
132        if (valid_player)
133        {
134            this->playersAlive--;
135            //this->playerLives_[player].erase (player); not necessary?
136            //Update: EachPlayer's "Players in Game"-HUD
137            for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
138            {
139                if (it->first->getClientID() == CLIENTID_UNKNOWN)
140                    continue;
141                const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive);
142                this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
143            }
144        }
145
146        return valid_player;
147    }
148
149    void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
150    {
151        if (!player)
152            return;
153        this->timeToAct_[player]=timeRemaining+3.0f;//reset timer
154        //Update: Individual Players "lifes"-HUD
155        std::map<PlayerInfo*, Player>::iterator it2 = this->players_.find(player);
156        if (it2 != this->players_.end())
157        {
158            const std::string& message = "Your Lives: " +multi_cast<std::string>(playerLives_[player]);
159            this->gtinfo_->sendFadingMessage(message,it2->first->getClientID());
160            const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive);
161            this->gtinfo_->sendStaticMessage(message1,it2->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
162        }
163    }
164
165    void LastManStanding::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn)
166    {
167        //Update: EachPlayer's "Players in Game"-HUD
168        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
169        {
170            if (it->first->getClientID() == CLIENTID_UNKNOWN)
171                continue;
172            const std::string& message1 = "Remaining Players : "+ multi_cast<std::string>(playersAlive);
173            this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f));
174        }
175    }
176
177    const int LastManStanding::playerGetLives(PlayerInfo* player)
178    {
179        if (player)
180            return  playerLives_[player];
181        else
182            return 0;
183    }
184
185    void LastManStanding::killPlayer(PlayerInfo* player)
186    {
187        if(!player)
188            return;
189        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
190        if (it != this->players_.end())
191        {
192            if(!player->getControllableEntity())
193                {return;}
194            Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity());
195            if(!pawn)
196                {return;}
197            pawn->kill();
198            this->timeToAct_[player]=timeRemaining+3.0f;//reset timer
199        }
200    }
201   
202    void LastManStanding::tick(float dt)
203    {
204        SUPER(LastManStanding, tick, dt);
205        if(this->hasStarted()&&(!this->hasEnded()))
206        {
207            if ((this->hasStarted()&&(playersAlive<=1)))//last player remaining
208            {
209            this->end();
210            }
211            for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it)
212            {       
213                it->second-=dt;
214                if (it->second<timeRemaining/6)//Warning message
215                {
216                    const std::string& message = "Camper Warning! Don't forget to shoot.";
217                    this->gtinfo_->sendFadingMessage(message,it->first->getClientID());
218                }
219                if (it->second<0.0f)
220                {
221                    it->second=timeRemaining+3.0f;//reset timer
222                    if (playerGetLives(it->first)>0)
223                        this->killPlayer(it->first);
224                }
225            }
226        }
227    }
228
229}
Note: See TracBrowser for help on using the repository browser.