Class enum instead of hardcoded ints.

Removed superfluous .Valid() checks.

Check for spectator spawnrequest in case they somehow has a class, so we don't get "x players were supposed to be spawned but failed" if it happens.
This commit is contained in:
William Moberg
2016-03-09 11:35:32 +01:00
parent b782e73556
commit 534d6639e5
4 changed files with 29 additions and 19 deletions
+9 -1
View File
@@ -15,11 +15,19 @@ public:
virtual void Update(double dt) override;
private:
// This enum must correspond to the command values for PickTeam buttons.
enum class PlayerClass
{
None = 0,
Assault,
Defender,
Sniper
};
struct SpawnRequest
{
int PlayerID;
ComponentInfo::EnumType Team;
ComponentInfo::EnumType Class;
PlayerClass Class;
};
bool m_NetworkEnabled = false;
+2 -2
View File
@@ -35,7 +35,7 @@ void PlayerDeathSystem::createDeathEffect(EntityWrapper player)
//components that we need from player
auto playerModel = player.FirstChildByName("PlayerModel");
if (!playerModel.Valid() || !playerModel.HasComponent("Model") || !playerModel.HasComponent("Animation")) {
if (!playerModel.HasComponent("Model") || !playerModel.HasComponent("Animation")) {
if (player == LocalPlayer) {
setSpectatorCamera();
}
@@ -82,7 +82,7 @@ void PlayerDeathSystem::setSpectatorCamera()
{
// Look for the spectator camera entity in the level.
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera");
if (!spectatorCam.Valid() || !spectatorCam.HasComponent("Camera")) {
if (!spectatorCam.HasComponent("Camera")) {
return;
}
Events::SetCamera eSetCamera;
+16 -14
View File
@@ -63,10 +63,9 @@ void PlayerSpawnSystem::Update(double dt)
int playersSpectating = 0;
const int numRequestsToHandle = (int)m_SpawnRequests.size();
for (auto it = m_SpawnRequests.begin(); it != m_SpawnRequests.end(); ++it) {
// TODO: -1 Signifies no class picked, or they are a spectator, enum here later?
// It is valid if they didn't pick class yet
// but don't spawn anything, goto next spawnrequest.
if (it->Class == -1) {
if (it->Class == PlayerClass::None) {
++playersSpectating;
continue;
}
@@ -80,6 +79,11 @@ void PlayerSpawnSystem::Update(double dt)
if (spawner.HasComponent("Team")) {
auto cSpawnerTeam = spawner["Team"];
if ((int)cSpawnerTeam["Team"] != it->Team) {
// If they somehow has a valid class as spectator, don't spawn them.
if (it->Team == cSpawnerTeam["Team"].Enum("Spectator")) {
++playersSpectating;
break;
}
continue;
}
}
@@ -136,7 +140,7 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
if (iter->PlayerID == e.PlayerID) {
// If player wants to switch team or class , remove their selected class so they don't spawn.
if (e.Command == "SwapToTeamPick" || e.Command == "SwapToClassPick") {
iter->Class = -1;
iter->Class = PlayerClass::None;
return true;
}
break;
@@ -150,7 +154,7 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
if (e.Command == "PickTeam") {
iter->Team = (ComponentInfo::EnumType)e.Value;
} else {
iter->Class = (ComponentInfo::EnumType)e.Value;
iter->Class = static_cast<PlayerClass>(e.Value);
}
} 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,
@@ -159,12 +163,12 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
req.PlayerID = e.PlayerID;
if (e.Command == "PickTeam") {
req.Team = (ComponentInfo::EnumType)e.Value;
req.Class = -1; // TODO: -1 Signifies no class picked, enum here later?
req.Class = PlayerClass::None;
} 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.Class = (ComponentInfo::EnumType)e.Value;
req.Class = static_cast<PlayerClass>(e.Value);
}
m_SpawnRequests.push_back(req);
} else {
@@ -229,10 +233,6 @@ bool PlayerSpawnSystem::OnPlayerDeath(Events::PlayerDeath& e)
return false;
}
ComponentWrapper cTeam = e.Player["Team"];
// A spectator can't die anyway
if ((ComponentInfo::EnumType)cTeam["Team"] == cTeam["Team"].Enum("Spectator")) {
return false;
}
if (m_PlayerIDs.count(e.Player.ID) == 0) {
return false;
@@ -241,13 +241,15 @@ bool PlayerSpawnSystem::OnPlayerDeath(Events::PlayerDeath& e)
SpawnRequest req;
req.PlayerID = m_PlayerIDs.at(e.Player.ID);
req.Team = cTeam["Team"];
// TODO: Better than temp class state code.
// TODO: Something better than temp class state code, if we ever add class enums in .xml
if (e.Player.HasComponent("DashAbility")) {
req.Class = 1; // Assault
req.Class = PlayerClass::Assault;
} else if (e.Player.HasComponent("SprintAbility")) {
req.Class = 3; // Sniper
req.Class = PlayerClass::Sniper;
} else if (e.Player.HasComponent("ShieldAbility")) {
req.Class = PlayerClass::Defender;
} else {
req.Class = 2; // Defender
req.Class = PlayerClass::None;
}
m_SpawnRequests.push_back(req);
+2 -2
View File
@@ -15,7 +15,7 @@ void SpectatorCameraSystem::Update(double dt)
if (!m_CamSetToTeamPick && IsClient) {
// Find the class pick camera and set them to it, since they need to pick a team before they can leave the screen.
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("PickTeamCamera");
if (spectatorCam.Valid() && spectatorCam.HasComponent("Camera")) {
if (spectatorCam.HasComponent("Camera")) {
m_CamSetToTeamPick = true;
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = spectatorCam;
@@ -59,7 +59,7 @@ bool SpectatorCameraSystem::OnInputCommand(const Events::InputCommand& e)
}
EntityWrapper spectatorCam = m_World->GetFirstEntityByName(camName);
// Set the camera as active, if it exists.
if (spectatorCam.Valid() && spectatorCam.HasComponent("Camera")) {
if (spectatorCam.HasComponent("Camera")) {
// Set the class pick button visible if a blue or red team is picked, else invisible.
EntityWrapper HUD;
if (camName == "SpectatorCamera") {