From 9731cd9c5effd95393ad336eadb6c97fe602bd3a Mon Sep 17 00:00:00 2001 From: Linda Andersson Date: Wed, 26 Feb 2014 14:55:29 +0100 Subject: [PATCH] Spawning players on rand spawnpoint when they connect --- .../GameClient/GameClientState/GameState.cpp | 22 ++++++++-- Code/Game/GameLogic/Level.cpp | 44 +++++++++++-------- Code/Game/GameLogic/Player.cpp | 16 +++++-- Code/Game/GameLogic/Player.h | 1 + .../GameServer/Implementation/GameClient.cpp | 4 ++ 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/Code/Game/GameClient/GameClientState/GameState.cpp b/Code/Game/GameClient/GameClientState/GameState.cpp index 4b3e7cb8..d8e0c0e9 100644 --- a/Code/Game/GameClient/GameClientState/GameState.cpp +++ b/Code/Game/GameClient/GameClientState/GameState.cpp @@ -153,6 +153,11 @@ void GameState::InitiatePlayer( int id, const std::string &modelName, const floa this->privData->camera.UpdateOrientation(); } } + else + { + int i = 0; + // some error loading model + } } GameClientState::ClientState GameState::Update( float deltaTime ) @@ -739,11 +744,20 @@ const GameClientState::NetEvent & GameState::DataRecieved( const GameClientState { //Remove the disconnected player Protocol_ObjectDisconnectPlayer decoded(data); - auto object = this->privData->dynamicObjects->find( decoded.objectID ); - if( object != this->privData->dynamicObjects->end() ) + C_Player *player; + player = (this->privData->players)[decoded.objectID]; + + if( player ) { - object->second = nullptr; - this->privData->dynamicObjects->erase( object ); + if( this->privData->myId == decoded.objectID ) + { + // dont delete my player + } + if( player ) + { + player->SetVisible(false); + (this->privData->players)[decoded.objectID].Release(); + } } } return GameClientState::event_processed; diff --git a/Code/Game/GameLogic/Level.cpp b/Code/Game/GameLogic/Level.cpp index be970c04..385571af 100644 --- a/Code/Game/GameLogic/Level.cpp +++ b/Code/Game/GameLogic/Level.cpp @@ -368,6 +368,9 @@ void Level::AddPlayerToTeam(Player *player, int teamID) } void Level::AddPlayerToGame(Player *player) { + int i = rand() % spawnPoints.Size(); + Float3 spawnPoint = spawnPoints[i]; + player->ResetPlayer(spawnPoint); for(int i = 0; i < (int)this->playerObjects.Size(); i++) { if (!this->playerObjects[i]) @@ -409,28 +412,31 @@ void Level::Update(float deltaTime) for(int i = 0; i < (int)this->playerObjects.Size(); i++) { - if(this->playerObjects[i]->getAffectingPlayer() != NULL) + if(this->playerObjects[i]) { - - } - - if (this->playerObjects[i]->GetState() == PLAYER_STATE::PLAYER_STATE_DEAD) - { - // true when timer reaches 0 - if(this->playerObjects[i]->deathTimerTick(deltaTime)) - RespawnPlayer(this->playerObjects[i]); - } - else if (this->playerObjects[i]->GetState() == PLAYER_STATE::PLAYER_STATE_DIED) - { - this->playerObjects[i]->setDeathTimer(DEATH_TIMER); - // HACK to avoid crasch. affected by tag is NULL - //((Game*)&Game::Instance())->onDeadFnc(this->playerObjects[i], this->playerObjects[i], DEATH_TIMER); // add killer ID - Player* killer = this->playerObjects[i]->getAffectingPlayer(); - if(!killer) //if there is no killer then you commited suicide + if(this->playerObjects[i]->getAffectingPlayer() != NULL) { - killer = this->playerObjects[i]; + + } + + if (this->playerObjects[i]->GetState() == PLAYER_STATE::PLAYER_STATE_DEAD) + { + // true when timer reaches 0 + if(this->playerObjects[i]->deathTimerTick(deltaTime)) + RespawnPlayer(this->playerObjects[i]); + } + else if (this->playerObjects[i]->GetState() == PLAYER_STATE::PLAYER_STATE_DIED) + { + this->playerObjects[i]->setDeathTimer(DEATH_TIMER); + // HACK to avoid crasch. affected by tag is NULL + //((Game*)&Game::Instance())->onDeadFnc(this->playerObjects[i], this->playerObjects[i], DEATH_TIMER); // add killer ID + Player* killer = this->playerObjects[i]->getAffectingPlayer(); + if(!killer) //if there is no killer then you commited suicide + { + killer = this->playerObjects[i]; + } + ((Game*)&Game::Instance())->onDeadFnc(this->playerObjects[i], killer, DEATH_TIMER); // add killer ID } - ((Game*)&Game::Instance())->onDeadFnc(this->playerObjects[i], killer, DEATH_TIMER); // add killer ID } } diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index 49babcc3..85477fc9 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -15,6 +15,8 @@ Player::Player() AffectedObjects.Reserve(15); this->weapon = NULL; this->teamID = -1; + this->playerScore.killScore = 0; + this->playerScore.deathScore = 0; } Player::Player(Oyster::Physics::ICustomBody *rigidBody, void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, int teamID) @@ -24,6 +26,8 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, void (*EventOnCollision) Player::initPlayerData(); AffectedObjects.Reserve(15); this->teamID = teamID; + this->playerScore.killScore = 0; + this->playerScore.deathScore = 0; } Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustomBody::SubscriptMessage (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), ObjectSpecialType type, int objectID, int teamID) @@ -33,6 +37,8 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustom Player::initPlayerData(); AffectedObjects.Reserve(15); this->teamID = teamID; + this->playerScore.killScore = 0; + this->playerScore.deathScore = 0; } Player::~Player(void) @@ -47,8 +53,6 @@ void Player::initPlayerData() { this->playerStats.hp = MAX_HP; this->playerStats.movementSpeed = BASIC_SPEED; - this->playerScore.killScore = 0; - this->playerScore.deathScore = 0; this->playerState = PLAYER_STATE_IDLE; this->lookDir = Oyster::Math::Float3(0,0,-1); @@ -317,7 +321,13 @@ void Player::Inactivate() { //this-> } - +void Player::ResetPlayer( Oyster::Math::Float3 spawnPos) +{ + Player::initPlayerData(); + this->rigidBody->SetPosition(spawnPos); + this->playerScore.killScore = 0; + this->playerScore.deathScore = 0; +} Oyster::Math::Float3 Player::GetPosition() const { return (Oyster::Math::Float3) this->rigidBody->GetState().centerPos; diff --git a/Code/Game/GameLogic/Player.h b/Code/Game/GameLogic/Player.h index edb4cc79..965abfd9 100644 --- a/Code/Game/GameLogic/Player.h +++ b/Code/Game/GameLogic/Player.h @@ -82,6 +82,7 @@ namespace GameLogic bool IsIdle(); void Inactivate(); + void ResetPlayer( Oyster::Math::Float3 spawnPos); Oyster::Math::Float3 GetPosition() const; Oyster::Math::Float3 GetLookDir() const; diff --git a/Code/Game/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp index 74795ce6..0c33ba89 100644 --- a/Code/Game/GameServer/Implementation/GameClient.cpp +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -25,6 +25,10 @@ GameClient::GameClient(Utility::DynamicMemory::SmartPointerplayer) + { + this->player->Inactivate(); + } this->isReady = false; this->character = L"char_orca.dan"; this->alias = L"Unknown";