Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 14, 2009, 10:17:35 PM (15 years ago)
Author:
rgrieder
Message:

Merged presentation branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/objects/gametypes/Gametype.cc

    r2171 r2662  
    3434
    3535#include "core/CoreIncludes.h"
     36#include "core/ConfigValueIncludes.h"
     37#include "core/Template.h"
     38#include "core/Core.h"
     39#include "overlays/OverlayGroup.h"
    3640#include "objects/infos/PlayerInfo.h"
     41#include "objects/infos/Bot.h"
    3742#include "objects/worldentities/pawns/Spectator.h"
    3843#include "objects/worldentities/SpawnPoint.h"
     44#include "objects/worldentities/Camera.h"
     45#include "Settings.h"
    3946
    4047#include "network/Host.h"
     
    4451    CreateUnloadableFactory(Gametype);
    4552
    46     Gametype::Gametype(BaseObject* creator) : BaseObject(creator)
     53    Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator)
    4754    {
    4855        RegisterObject(Gametype);
    4956
     57        this->setGametype(this);
     58
    5059        this->defaultControllableEntity_ = Class(Spectator);
    5160
    52         this->bStarted_ = false;
    53         this->bEnded_ = false;
    5461        this->bAutoStart_ = false;
    5562        this->bForceSpawn_ = false;
     63        this->numberOfBots_ = 0;
    5664
    5765        this->initialStartCountdown_ = 3;
    58         this->startCountdown_ = 0;
    59         this->bStartCountdownRunning_ = false;
     66
     67        this->setConfigValues();
     68
     69        this->addBots(this->numberOfBots_);
     70
     71        // load the corresponding score board
     72        if (Core::showsGraphics() && this->scoreboardTemplate_ != "")
     73        {
     74            this->scoreboard_ = new OverlayGroup(this);
     75            this->scoreboard_->addTemplate(this->scoreboardTemplate_);
     76            this->scoreboard_->setGametype(this);
     77        }
     78        else
     79            this->scoreboard_ = 0;
     80    }
     81
     82    void Gametype::setConfigValues()
     83    {
     84        SetConfigValue(initialStartCountdown_, 3.0f);
     85        SetConfigValue(bAutoStart_, false);
     86        SetConfigValue(bForceSpawn_, false);
     87        SetConfigValue(numberOfBots_, 0);
     88        SetConfigValue(scoreboardTemplate_, "defaultScoreboard");
    6089    }
    6190
    6291    void Gametype::tick(float dt)
    6392    {
    64         if (this->bStartCountdownRunning_ && !this->bStarted_)
    65             this->startCountdown_ -= dt;
    66 
    67         if (!this->bStarted_)
     93        SUPER(Gametype, tick, dt);
     94
     95        if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_)
     96            this->gtinfo_.startCountdown_ -= dt;
     97
     98        if (!this->gtinfo_.bStarted_)
    6899            this->checkStart();
    69100        else
     
    76107    {
    77108        COUT(0) << "game started" << std::endl;
    78         this->bStarted_ = true;
     109        this->gtinfo_.bStarted_ = true;
    79110
    80111        this->spawnPlayersIfRequested();
     
    84115    {
    85116        COUT(0) << "game ended" << std::endl;
    86         this->bEnded_ = true;
     117        this->gtinfo_.bEnded_ = true;
    87118    }
    88119
    89120    void Gametype::playerEntered(PlayerInfo* player)
    90121    {
    91         this->players_[player] = PlayerState::Joined;
     122        this->players_[player].state_ = PlayerState::Joined;
    92123
    93124        std::string message = player->getName() + " entered the game";
     
    98129    void Gametype::playerLeft(PlayerInfo* player)
    99130    {
    100         std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.find(player);
     131        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
    101132        if (it != this->players_.end())
    102133        {
     
    140171    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
    141172    {
    142     }
    143 
    144     void Gametype::playerScored(PlayerInfo* player)
    145     {
     173        if (victim && victim->getPlayer())
     174        {
     175            std::string message;
     176            if (killer)
     177            {
     178                if (killer->getPlayer())
     179                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
     180                else
     181                    message = victim->getPlayer()->getName() + " was killed";
     182            }
     183            else
     184                message = victim->getPlayer()->getName() + " died";
     185
     186            COUT(0) << message << std::endl;
     187            Host::Broadcast(message);
     188        }
     189
     190        if (victim && victim->getPlayer())
     191        {
     192            std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer());
     193            if (it != this->players_.end())
     194            {
     195                it->second.state_ = PlayerState::Dead;
     196                it->second.killed_++;
     197
     198                // Reward killer
     199                if (killer)
     200                {
     201                    std::map<PlayerInfo*, Player>::iterator itKiller = this->players_.find(killer->getPlayer());
     202                    if (itKiller != this->players_.end())
     203                    {
     204                        this->playerScored(itKiller->second);
     205                    }
     206                    else
     207                        COUT(2) << "Warning: Killing Pawn was not in the playerlist" << std::endl;
     208                }
     209
     210                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
     211                if (victim->getCamera())
     212                {
     213                    entity->setPosition(victim->getCamera()->getWorldPosition());
     214                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
     215                }
     216                else
     217                {
     218                    entity->setPosition(victim->getWorldPosition());
     219                    entity->setOrientation(victim->getWorldOrientation());
     220                }
     221                it->first->startControl(entity);
     222            }
     223            else
     224                COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl;
     225        }
     226    }
     227
     228    void Gametype::playerScored(Player& player)
     229    {
     230        player.frags_++;
    146231    }
    147232
     
    150235        if (this->spawnpoints_.size() > 0)
    151236        {
    152             srand(time(0));
    153             rnd();
    154 
    155237            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
    156238            unsigned int index = 0;
     
    168250    void Gametype::assignDefaultPawnsIfNeeded()
    169251    {
    170         for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
    171         {
    172             if (!it->first->getControllableEntity() && (!it->first->isReadyToSpawn() || !this->bStarted_))
    173             {
    174                 SpawnPoint* spawn = this->getBestSpawnPoint(it->first);
    175                 if (spawn)
    176                 {
    177                     // force spawn at spawnpoint with default pawn
    178                     ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
    179                     spawn->spawn(entity);
    180                     it->first->startControl(entity);
    181                     it->second = PlayerState::Dead;
     252        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     253        {
     254            if (!it->first->getControllableEntity())
     255            {
     256                it->second.state_ = PlayerState::Dead;
     257
     258                if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_)
     259                {
     260                    SpawnPoint* spawn = this->getBestSpawnPoint(it->first);
     261                    if (spawn)
     262                    {
     263                        // force spawn at spawnpoint with default pawn
     264                        ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
     265                        spawn->spawn(entity);
     266                        it->first->startControl(entity);
     267                        it->second.state_ = PlayerState::Dead;
     268                    }
     269                    else
     270                    {
     271                        COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
     272                        abort();
     273                    }
     274                }
     275            }
     276        }
     277    }
     278
     279    void Gametype::checkStart()
     280    {
     281        if (!this->gtinfo_.bStarted_)
     282        {
     283            if (this->gtinfo_.bStartCountdownRunning_)
     284            {
     285                if (this->gtinfo_.startCountdown_ <= 0)
     286                {
     287                    this->gtinfo_.bStartCountdownRunning_ = false;
     288                    this->gtinfo_.startCountdown_ = 0;
     289                    this->start();
     290                }
     291            }
     292            else if (this->players_.size() > 0)
     293            {
     294                if (this->bAutoStart_)
     295                {
     296                    this->start();
    182297                }
    183298                else
    184299                {
    185                     COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
    186                     abort();
    187                 }
    188             }
    189         }
    190     }
    191 
    192     void Gametype::checkStart()
    193     {
    194         if (!this->bStarted_)
    195         {
    196             if (this->bStartCountdownRunning_)
    197             {
    198                 if (this->startCountdown_ <= 0)
    199                 {
    200                     this->bStartCountdownRunning_ = false;
    201                     this->startCountdown_ = 0;
    202                     this->start();
    203                 }
    204             }
    205             else if (this->players_.size() > 0)
    206             {
    207                 if (this->bAutoStart_)
    208                 {
    209                     this->start();
    210                 }
    211                 else
    212                 {
    213300                    bool allplayersready = true;
    214                     for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     301                    bool hashumanplayers = false;
     302                    for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
    215303                    {
    216304                        if (!it->first->isReadyToSpawn())
    217305                            allplayersready = false;
    218                     }
    219                     if (allplayersready)
    220                     {
    221                         this->startCountdown_ = this->initialStartCountdown_;
    222                         this->bStartCountdownRunning_ = true;
     306                        if (it->first->isHumanPlayer())
     307                            hashumanplayers = true;
     308                    }
     309                    if (allplayersready && hashumanplayers)
     310                    {
     311                        this->gtinfo_.startCountdown_ = this->initialStartCountdown_;
     312                        this->gtinfo_.bStartCountdownRunning_ = true;
    223313                    }
    224314                }
     
    229319    void Gametype::spawnPlayersIfRequested()
    230320    {
    231         for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     321        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
    232322            if (it->first->isReadyToSpawn() || this->bForceSpawn_)
    233323                this->spawnPlayer(it->first);
     
    236326    void Gametype::spawnDeadPlayersIfRequested()
    237327    {
    238         for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
    239             if (it->second == PlayerState::Dead)
     328        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
     329            if (it->second.state_ == PlayerState::Dead)
    240330                if (it->first->isReadyToSpawn() || this->bForceSpawn_)
    241331                    this->spawnPlayer(it->first);
     
    248338        {
    249339            player->startControl(spawnpoint->spawn());
    250             this->players_[player] = PlayerState::Alive;
     340            this->players_[player].state_ = PlayerState::Alive;
    251341        }
    252342        else
     
    256346        }
    257347    }
     348
     349    void Gametype::addBots(unsigned int amount)
     350    {
     351        for (unsigned int i = 0; i < amount; ++i)
     352            new Bot(this);
     353    }
     354
     355    void Gametype::killBots(unsigned int amount)
     356    {
     357        unsigned int i = 0;
     358        for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); )
     359        {
     360            if (it->getGametype() == this)
     361            {
     362                delete (*(it++));
     363                ++i;
     364            }
     365        }
     366    }
    258367}
Note: See TracChangeset for help on using the changeset viewer.