Only set spectator camera if player haven't spawned already. Don't spawn player if they switch to class pick screen.

This commit is contained in:
William Moberg
2016-03-07 15:05:37 +01:00
parent 0dfe00b406
commit 282e0bf7c4
2 changed files with 43 additions and 26 deletions
+13 -3
View File
@@ -18,6 +18,8 @@ bool PlayerDeathSystem::OnPlayerDeath(Events::PlayerDeath& e)
return false; return false;
} }
LOG_DEBUG("------ Player #%i died.", e.Player.ID);
createDeathEffect(e.Player); createDeathEffect(e.Player);
// Delete player // Delete player
@@ -36,6 +38,7 @@ void PlayerDeathSystem::createDeathEffect(EntityWrapper player)
auto playerModel = player.FirstChildByName("PlayerModel"); auto playerModel = player.FirstChildByName("PlayerModel");
if (!playerModel.Valid() || !playerModel.HasComponent("Model") || !playerModel.HasComponent("Animation")) { if (!playerModel.Valid() || !playerModel.HasComponent("Model") || !playerModel.HasComponent("Animation")) {
if (player == LocalPlayer) { if (player == LocalPlayer) {
LOG_DEBUG("------ LocalPlayer's death effect could not be spawned, setting to spectator instead of death cam.");
setSpectatorCamera(); setSpectatorCamera();
} }
return; return;
@@ -57,6 +60,7 @@ void PlayerDeathSystem::createDeathEffect(EntityWrapper player)
//camera (with lifetime) behind the player //camera (with lifetime) behind the player
if (player == LocalPlayer) { if (player == LocalPlayer) {
LOG_DEBUG("------ LocalPlayer's death effect spawning, setting to death camera.");
m_LocalPlayerDeathEffect = deathEffectEW; m_LocalPlayerDeathEffect = deathEffectEW;
auto cam = deathEffectEW.FirstChildByName("Camera"); auto cam = deathEffectEW.FirstChildByName("Camera");
Events::SetCamera eSetCamera; Events::SetCamera eSetCamera;
@@ -71,8 +75,14 @@ bool PlayerDeathSystem::OnEntityDeleted(Events::EntityDeleted& e)
if (m_LocalPlayerDeathEffect.ID != e.DeletedEntity) { if (m_LocalPlayerDeathEffect.ID != e.DeletedEntity) {
return false; return false;
} }
setSpectatorCamera(); // If the player hasn't spawned already, activate the spectator camera.
if (!LocalPlayer.Valid()) {
LOG_DEBUG("------ LocalPlayer's death effect removed, setting to spectator.");
setSpectatorCamera();
} else {
LOG_DEBUG("------ LocalPlayer's death effect removed, player is already spawned, not setting spectator.");
}
return true; return true;
} }
@@ -80,7 +90,7 @@ void PlayerDeathSystem::setSpectatorCamera()
{ {
// Look for the spectator camera entity in the level. // Look for the spectator camera entity in the level.
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera"); EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera");
if (!spectatorCam.Valid() || !spectatorCam.HasComponent("Camera") || LocalPlayer.Valid()) { if (!spectatorCam.Valid() || !spectatorCam.HasComponent("Camera")) {
return; return;
} }
Events::SetCamera eSetCamera; Events::SetCamera eSetCamera;
+30 -23
View File
@@ -59,13 +59,15 @@ void PlayerSpawnSystem::Update(double dt)
return; return;
} }
int numSpawnedPlayers = 0; int numHandledRequests = 0;
int playersStillPickingClass = 0;
const int numRequestsToHandle = (int)m_SpawnRequests.size();
for (auto it = m_SpawnRequests.begin(); it != m_SpawnRequests.end(); ++it) { for (auto it = m_SpawnRequests.begin(); it != m_SpawnRequests.end(); ++it) {
// TODO: -1 Signifies no class picked, enum here later? // TODO: -1 Signifies no class picked, enum here later?
// Increase num spawned players if they didn't pick class yet since it is valid // It is valid if they didn't pick class yet
// but don't spawn anything, goto next spawnrequest. // but don't spawn anything, goto next spawnrequest.
if (it->Class == -1) { if (it->Class == -1) {
++numSpawnedPlayers; ++playersStillPickingClass;
break; break;
} }
for (auto& cPlayerSpawn : *playerSpawns) { for (auto& cPlayerSpawn : *playerSpawns) {
@@ -78,10 +80,11 @@ void PlayerSpawnSystem::Update(double dt)
if (spawner.HasComponent("Team")) { if (spawner.HasComponent("Team")) {
auto cSpawnerTeam = spawner["Team"]; auto cSpawnerTeam = spawner["Team"];
if ((int)cSpawnerTeam["Team"] != it->Team) { if ((int)cSpawnerTeam["Team"] != it->Team) {
// Increase num spawned players if someone picks spectator since it is valid // It is valid if someone picks spectator
// but don't spawn anything, goto next spawnrequest. // but don't spawn anything, goto next spawnrequest.
if (it->Team == (int)cSpawnerTeam["Team"].Enum("Spectator")) { if (it->Team == (int)cSpawnerTeam["Team"].Enum("Spectator")) {
++numSpawnedPlayers; ++numHandledRequests;
it = m_SpawnRequests.erase(it);
break; break;
} }
continue; continue;
@@ -103,7 +106,7 @@ void PlayerSpawnSystem::Update(double dt)
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
Events::LockMouse lock; Events::LockMouse lock;
m_EventBroker->Publish(lock); m_EventBroker->Publish(lock);
++numSpawnedPlayers; ++numHandledRequests;
it = m_SpawnRequests.erase(it); it = m_SpawnRequests.erase(it);
break; break;
} }
@@ -111,17 +114,18 @@ void PlayerSpawnSystem::Update(double dt)
break; break;
} }
} }
if (numSpawnedPlayers != (int)m_SpawnRequests.size()) { if (numHandledRequests != numRequestsToHandle - playersStillPickingClass) {
LOG_DEBUG("%i players were supposed to be spawned or set as spectator, but only %i was handled.", (int)m_SpawnRequests.size(), numSpawnedPlayers); LOG_DEBUG("%i players were supposed to be spawned or set as spectator, but only %i was handled.", numRequestsToHandle, numHandledRequests);
} else { } else {
LOG_DEBUG("%i players were spawned or set as spectator.", numSpawnedPlayers); std::string dbg = numHandledRequests != 0 ? std::to_string(numHandledRequests) + " players were spawned or set as spectator. " : "";
dbg += playersStillPickingClass != 0 ? std::to_string(playersStillPickingClass) + " players are still picking class and will be spawned later." : "";
LOG_DEBUG(dbg.c_str());
} }
} }
bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e) bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
{ {
bool removeRequest = e.Command == "SwapToClassPick" || e.Command == "SwapToTeamPick"; if (e.Command != "PickTeam" && e.Command != "PickClass" && e.Command != "SwapToTeamPick" && e.Command != "SwapToClassPick") {
if (e.Command != "PickTeam" && e.Command != "PickClass" && !removeRequest) {
return false; return false;
} }
@@ -139,28 +143,29 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
auto iter = m_SpawnRequests.begin(); auto iter = m_SpawnRequests.begin();
for (; iter != m_SpawnRequests.end(); ++iter) { for (; iter != m_SpawnRequests.end(); ++iter) {
if (iter->PlayerID == e.PlayerID) { if (iter->PlayerID == e.PlayerID) {
// If player wants to switch class, remove their spawn request. // If player wants to switch team, remove their spawn request.
if (removeRequest) { if (e.Command == "SwapToTeamPick") {
m_SpawnRequests.erase(iter); m_SpawnRequests.erase(iter);
} else if (e.Command == "SwapToClassPick") {
// If player wants to switch class, remove their selected class so they don't spawn.
iter->Class = -1;
} else {
break;
} }
break; return true;
} }
} }
// Return if we were only supposed to remove a request, not add one. //If we get here we got a PickTeam or PickClass, so add or alter a spawn request.
if (removeRequest) {
return true;
}
//If we get here, add or alter a spawn request.
if (iter != m_SpawnRequests.end()) { if (iter != m_SpawnRequests.end()) {
// If player is in queue to spawn, then change their team affiliation or class in the request. // If player is in queue to spawn, then change their team affiliation or class in the request.
if (e.Command == "PickTeam") { if (e.Command == "PickTeam") {
iter->Team = (ComponentInfo::EnumType)e.Value; iter->Team = (ComponentInfo::EnumType)e.Value;
LOG_INFO("old request swap Team"); LOG_DEBUG("old spawn request setting Team");
} else { } else {
iter->Class = (ComponentInfo::EnumType)e.Value; iter->Class = (ComponentInfo::EnumType)e.Value;
LOG_INFO("old request swap Class"); LOG_DEBUG("old spawn request setting Class");
} }
} else if (m_PlayerEntities.count(e.PlayerID) == 0 || !m_PlayerEntities[e.PlayerID].Valid()) { } else if (m_PlayerEntities.count(e.PlayerID) == 0 || !m_PlayerEntities[e.PlayerID].Valid()) {
// If player is not in queue to spawn, then create a spawn request, // If player is not in queue to spawn, then create a spawn request,
@@ -170,11 +175,13 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
if (e.Command == "PickTeam") { if (e.Command == "PickTeam") {
req.Team = (ComponentInfo::EnumType)e.Value; req.Team = (ComponentInfo::EnumType)e.Value;
req.Class = -1; // TODO: -1 Signifies no class picked, enum here later? req.Class = -1; // TODO: -1 Signifies no class picked, enum here later?
LOG_INFO("new request set Team"); LOG_DEBUG("new spawn request setting Team");
} else { } else {
// Should never get here, since you should have picked a team before you ever get a chance to pick class.
LOG_WARNING("Sequence error: Should not be able to pick class before team");
req.Team = 1; // TODO: 1 Signifies spectator, should probably have real enum here later. req.Team = 1; // TODO: 1 Signifies spectator, should probably have real enum here later.
req.Class = (ComponentInfo::EnumType)e.Value; req.Class = (ComponentInfo::EnumType)e.Value;
LOG_INFO("new request set Class"); LOG_DEBUG("new spawn request setting Class");
} }
m_SpawnRequests.push_back(req); m_SpawnRequests.push_back(req);
} else { } else {