Merge pull request #155 from teamfisk/Respawning

Looks good man! 🇺🇸 🍔 😎
This commit is contained in:
Joachim Pettersson
2016-03-02 17:55:32 +01:00
12 changed files with 5867 additions and 27 deletions
+3
View File
@@ -6,6 +6,7 @@
#include "ObjectPool.h"
#include "ComponentPool.h"
#include "EventBroker.h"
struct EntityWrapper;
class World
{
@@ -49,6 +50,8 @@ public:
void SetName(EntityID entity, const std::string& name);
// Get the textual name of an entity
std::string GetName(EntityID entity) const;
// Get the first entity in the world with the name.
EntityWrapper GetFirstEntityByName(const std::string& name);
private:
EventBroker* m_EventBroker = nullptr;
+5
View File
@@ -11,6 +11,7 @@
#include "Core/EntityFileParser.h"
#include "Core/EPlayerDeath.h"
#include "Core/EEntityDeleted.h"
class PlayerDeathSystem : public ImpureSystem
{
@@ -20,8 +21,12 @@ public:
virtual void Update(double dt) override;
private:
EntityWrapper m_LocalPlayerDeathEffect;
EventRelay<PlayerDeathSystem, Events::PlayerDeath> m_OnPlayerDeath;
bool OnPlayerDeath(Events::PlayerDeath& e);
EventRelay<PlayerDeathSystem, Events::EntityDeleted> m_EEntityDeleted;
bool OnEntityDeleted(Events::EntityDeleted& e);
void createDeathEffect(EntityWrapper player);
+1 -1
View File
@@ -4,7 +4,7 @@ LoadMap=
; if true -> Pool allocation is not used when calling Allocate/Free, just use regular dynamic allocation.
; if false -> Use pool allocation.
DisableMemoryPool=false
RespawnTime = 8.0
RespawnTime = -1.0
EditorEnabled=false
OutOfBodyExperience=false
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,219 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Entity name="SpectatorCamera" xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
<Components>
<c:Camera/>
<c:Transform>
<Position X="0" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity name="SpectatorHUD">
<Components>
<c:Transform>
<Position X="0" Y="0" Z="-0.5"/>
</c:Transform>
</Components>
<Children>
<Entity name="RespawnTimer">
<Components>
<c:Text>
<Content>Time to respawn: 0</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Color A="1" B="0" G="1" R="0.784313738"/>
<Alignment>
<Left/>
</Alignment>
</c:Text>
<c:Transform>
<Position X="-0.115000000" Y="0.15304254" Z="0"/>
<Scale X="0.0250000004" Y="0.0250000004" Z="1"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity name="CapturePointHUD">
<Components>
<c:Transform>
<Position X="0" Y="0.190541431" Z="0"/>
<Scale X="0.0599999987" Y="0.0599999987" Z="0.0599999987"/>
</c:Transform>
</Components>
<Children>
<Entity name="BackgroundHexagon">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon.png</DiffuseTexture>
<GlowMap></GlowMap>
<Color A="0.300000012" B="1" G="1" R="1"/>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0.919596612" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity name="ProgressionHexagon">
<Components>
<c:CapturePointHUD>
<CapturePointNumber>2</CapturePointNumber>
</c:CapturePointHUD>
<c:Fill>
<Color A="0.699999988" B="1" G="0.200000003" R="0"/>
</c:Fill>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon_Rotated.png</DiffuseTexture>
<GlowMap></GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="0.00999999978"/>
<Scale X="0.800000012" Y="0.800000012" Z="0.800000012"/>
<Orientation X="0" Y="0" Z="1.57079637"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity name="BackgroundHexagon">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon.png</DiffuseTexture>
<GlowMap></GlowMap>
<Color A="0.300000012" B="1" G="1" R="1"/>
</c:Sprite>
<c:Transform>
<Position X="0.811270177" Y="0.440691769" Z="-0.100000001"/>
</c:Transform>
</Components>
<Children>
<Entity name="ProgressionHexagon">
<Components>
<c:CapturePointHUD>
<CapturePointNumber>3</CapturePointNumber>
</c:CapturePointHUD>
<c:Fill>
<Color A="0.699999988" B="1" G="0.200000003" R="0"/>
</c:Fill>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon_Rotated.png</DiffuseTexture>
<GlowMap></GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="0.00999999978"/>
<Scale X="0.800000012" Y="0.800000012" Z="0.800000012"/>
<Orientation X="0" Y="0" Z="1.57079637"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity name="BackgroundHexagon">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon.png</DiffuseTexture>
<GlowMap></GlowMap>
<Color A="0.699999988" B="1" G="0.200000003" R="0"/>
</c:Sprite>
<c:Transform>
<Position X="1.62788737" Y="0.919596612" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity name="ProgressionHexagon">
<Components>
<c:CapturePointHUD>
<CapturePointNumber>4</CapturePointNumber>
</c:CapturePointHUD>
<c:Fill>
<Color A="0.699999988" B="1" G="0.200000003" R="0"/>
</c:Fill>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon_Rotated.png</DiffuseTexture>
<GlowMap></GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="0.00999999978"/>
<Scale X="0.800000012" Y="0.800000012" Z="0.800000012"/>
<Orientation X="0" Y="0" Z="1.57079637"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity name="BackgroundHexagon">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon.png</DiffuseTexture>
<GlowMap></GlowMap>
<Color A="0.300000012" B="1" G="1" R="1"/>
</c:Sprite>
<c:Transform>
<Position X="-0.820431828" Y="0.441878349" Z="-0.100000001"/>
</c:Transform>
</Components>
<Children>
<Entity name="ProgressionHexagon">
<Components>
<c:CapturePointHUD>
<CapturePointNumber>1</CapturePointNumber>
</c:CapturePointHUD>
<c:Fill>
<Percentage>0.59265931447347009</Percentage>
<Color A="0.699999988" B="0" G="0" R="1"/>
</c:Fill>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon_Rotated.png</DiffuseTexture>
<GlowMap></GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="0.00999999978"/>
<Scale X="0.800000012" Y="0.800000012" Z="0.800000012"/>
<Orientation X="0" Y="0" Z="4.71238899"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity name="BackgroundHexagon">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon.png</DiffuseTexture>
<GlowMap></GlowMap>
<Color A="0.699999988" B="0" G="0" R="1"/>
</c:Sprite>
<c:Transform>
<Position X="-1.58304751" Y="0.919596612" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity name="ProgressionHexagon">
<Components>
<c:CapturePointHUD/>
<c:Fill>
<Color A="0.699999988" B="1" G="0.200000003" R="0"/>
</c:Fill>
<c:Sprite>
<DiffuseTexture>Textures/Core/UnitHexagon_Rotated.png</DiffuseTexture>
<GlowMap></GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="0.00999999978"/>
<Scale X="0.800000012" Y="0.800000012" Z="0.800000012"/>
<Orientation X="0" Y="0" Z="1.57079637"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
+6
View File
@@ -54,6 +54,12 @@
<xs:element ref="c:DefenderWeapon" minOccurs="0"/>
<xs:element ref="c:CapturePointArrowHUD" minOccurs="0"/>
<xs:element ref="c:FloatingEffect" minOccurs="0"/>
<xs:element ref="c:AmmoPickup" minOccurs="0"/>
<xs:element ref="c:HealthPickup" minOccurs="0"/>
<xs:element ref="c:CapturePointGameMode" minOccurs="0"/>
<xs:element ref="c:DoubleJump" minOccurs="0"/>
<xs:element ref="c:HealthHUD" minOccurs="0"/>
<xs:element ref="c:SpriteIndicator" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
+13 -3
View File
@@ -1,6 +1,6 @@
#include "Core/EntityFileParser.h"
EntityFileParser::EntityFileParser(const EntityFile* entityFile)
EntityFileParser::EntityFileParser(const EntityFile* entityFile)
: m_EntityFile(entityFile)
{
m_Handler.SetStartEntityCallback(std::bind(&EntityFileParser::onStartEntity, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
@@ -33,13 +33,20 @@ void EntityFileParser::onStartEntity(EntityID entity, EntityID parent, const std
void EntityFileParser::onStartComponent(EntityID entity, const std::string& component)
{
EntityID realEntity = m_EntityIDMapper.at(entity);
m_World->AttachComponent(realEntity, component);
if (m_World->GetComponentPools().count(component) != 0) {
EntityID realEntity = m_EntityIDMapper.at(entity);
m_World->AttachComponent(realEntity, component);
} else {
LOG_ERROR("Tried to attach unregistered component \"%s\"! to entity #%i. Ignoring.", component.c_str(), entity);
}
//LOG_DEBUG("Attached component of type \"%s\" to entity #%i (%i)", component.c_str(), entity, realEntity);
}
void EntityFileParser::onStartComponentField(EntityID entity, const std::string& componentType, const std::string& fieldName, const std::map<std::string, std::string>& attributes)
{
if (m_World->GetComponentPools().count(componentType) == 0) {
return;
}
EntityID realEntity = m_EntityIDMapper.at(entity);
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
auto fieldIt = component.Info.Fields.find(fieldName);
@@ -61,6 +68,9 @@ void EntityFileParser::onStartComponentField(EntityID entity, const std::string&
void EntityFileParser::onFieldData(EntityID entity, const std::string& componentType, const std::string& fieldName, const char* fieldData)
{
if (m_World->GetComponentPools().count(componentType) == 0) {
return;
}
EntityID realEntity = m_EntityIDMapper.at(entity);
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
auto fieldIt = component.Info.Fields.find(fieldName);
+2 -2
View File
@@ -33,8 +33,8 @@ void _LOG(_LOG_LEVEL logLevel, const char* file, const char* func, unsigned int
va_end(args);
if (logLevel == LOG_LEVEL_ERROR) {
//std::cerr << file << ":" << line << " " << func << std::endl;
//std::cerr << _LOG_LEVEL_PREFIX[logLevel] << message << std::endl;
std::cerr << file << ":" << line << " " << func << std::endl;
std::cerr << _LOG_LEVEL_PREFIX[logLevel] << message << std::endl;
} else {
std::cout << _LOG_LEVEL_PREFIX[logLevel] << message << std::endl;
}
+18
View File
@@ -1,6 +1,7 @@
#include "Core/World.h"
#include "Core/EEntityDeleted.h"
#include "Core/EComponentDeleted.h"
#include "Core/EntityWrapper.h"
World::~World()
{
@@ -151,6 +152,23 @@ std::string World::GetName(EntityID entity) const
}
}
EntityWrapper World::GetFirstEntityByName(const std::string& name)
{
auto itPair = GetDirectChildren(EntityID_Invalid);
for (auto it = itPair.first; it != itPair.second; it++) {
EntityID childEntityID = it->second;
EntityWrapper childEntity(this, childEntityID);
if (!childEntity.Valid()) {
continue;
}
EntityWrapper entityWithName = childEntity.Name() == name ? childEntity : childEntity.FirstChildByName(name);
if (entityWithName.Valid()) {
return entityWithName;
}
}
return EntityWrapper::Invalid;
}
EntityID World::generateEntityID()
{
// TODO: Make EntityID generation smarter
+2 -1
View File
@@ -265,7 +265,8 @@ EntityWrapper EditorSystem::importEntity(EntityWrapper parent, boost::filesystem
EntityFileParser fp(entityFile);
EntityID newEntity = fp.MergeEntities(parent.World, parent.ID);
return EntityWrapper(parent.World, newEntity);
} catch (const std::exception&) {
} catch (const std::exception& e) {
LOG_ERROR("Failed to import entity \"%s\": \"%s\"", filePath.string().c_str(), e.what());
return EntityWrapper::Invalid;
}
}
+20
View File
@@ -4,6 +4,7 @@ PlayerDeathSystem::PlayerDeathSystem(SystemParams params)
: System(params)
{
EVENT_SUBSCRIBE_MEMBER(m_OnPlayerDeath, &PlayerDeathSystem::OnPlayerDeath);
EVENT_SUBSCRIBE_MEMBER(m_EEntityDeleted, &PlayerDeathSystem::OnEntityDeleted);
}
void PlayerDeathSystem::Update(double dt)
@@ -59,9 +60,28 @@ void PlayerDeathSystem::createDeathEffect(EntityWrapper player)
//camera (with lifetime) behind the player
if (player == LocalPlayer) {
m_LocalPlayerDeathEffect = deathEffectEW;
auto cam = deathEffectEW.FirstChildByName("Camera");
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = cam;
m_EventBroker->Publish(eSetCamera);
}
}
bool PlayerDeathSystem::OnEntityDeleted(Events::EntityDeleted& e)
{
// We only care about when the local players death effect is removed.
if (m_LocalPlayerDeathEffect.ID != e.DeletedEntity) {
return false;
}
// Look for the spectator camera entity in the level.
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera");
if (!spectatorCam.Valid() || !spectatorCam.HasComponent("Camera") || LocalPlayer.Valid()) {
return false;
}
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = spectatorCam;
m_EventBroker->Publish(eSetCamera);
return true;
}
+46 -20
View File
@@ -10,7 +10,7 @@ PlayerSpawnSystem::PlayerSpawnSystem(SystemParams params)
ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini");
m_NetworkEnabled = config->Get("Networking.StartNetwork", false);
m_ForcedRespawnTime = config->Get("Debug.RespawnTime", -1.0f);
m_DbgConfigForceRespawn = m_ForcedRespawnTime > 0;
m_DbgConfigForceRespawn = m_ForcedRespawnTime > 0 && IsServer;
}
void PlayerSpawnSystem::Update(double dt)
@@ -26,7 +26,21 @@ void PlayerSpawnSystem::Update(double dt)
// Increase timer.
double& timer = (double&)modeComponent["RespawnTime"];
timer += dt;
double maxRespawnTime = m_DbgConfigForceRespawn ? m_ForcedRespawnTime : (double)modeComponent["MaxRespawnTime"];
if (m_DbgConfigForceRespawn) {
(double&)modeComponent["MaxRespawnTime"] = m_ForcedRespawnTime;
}
double maxRespawnTime = (double)modeComponent["MaxRespawnTime"];
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera");
if (spectatorCam.Valid()) {
EntityWrapper HUD = spectatorCam.FirstChildByName("SpectatorHUD");
if (HUD.Valid()) {
EntityWrapper respawnTimer = spectatorCam.FirstChildByName("RespawnTimer");
if (respawnTimer.Valid()) {
//Update respawn time in the HUD element.
respawnTimer["Text"]["Content"] = std::to_string(1 + (int)(maxRespawnTime - timer));
}
}
}
if (timer < maxRespawnTime) {
return;
}
@@ -54,7 +68,14 @@ void PlayerSpawnSystem::Update(double dt)
// If the spawner has a team affiliation, check it
if (spawner.HasComponent("Team")) {
if ((int)spawner["Team"]["Team"] != req.Team) {
auto cSpawnerTeam = spawner["Team"];
if ((int)cSpawnerTeam["Team"] != req.Team) {
// Increase num spawned players if someone picks spectator, since it is valid to pick spectator
// but don't spawn anything, goto next spawnrequest.
if (req.Team == (int)cSpawnerTeam["Team"].Enum("Spectator")) {
++numSpawnedPlayers;
break;
}
continue;
}
}
@@ -75,9 +96,9 @@ void PlayerSpawnSystem::Update(double dt)
}
}
if (numSpawnedPlayers != (int)m_SpawnRequests.size()) {
LOG_DEBUG("%i players were supposed to be spawned, but %i was spawned.", (int)m_SpawnRequests.size(), numSpawnedPlayers);
LOG_DEBUG("%i players were supposed to be spawned or set as spectator, but only %i was handled.", (int)m_SpawnRequests.size(), numSpawnedPlayers);
} else {
LOG_DEBUG("%i players were spawned.", numSpawnedPlayers);
LOG_DEBUG("%i players were spawned or set as spectator.", numSpawnedPlayers);
}
m_SpawnRequests.clear();
}
@@ -88,24 +109,29 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
return false;
}
if (e.Value == 0) {
return false;
}
// A dead client should be able to swap to the spectator camera.
if (IsClient && !LocalPlayer.Valid()) {
// Set the spectator camera as active, if it exists.
// Find the camera.
EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera");
if (spectatorCam.Valid() && spectatorCam.HasComponent("Camera")) {
Events::SetCamera eSetCamera;
eSetCamera.CameraEntity = spectatorCam;
m_EventBroker->Publish(eSetCamera);
}
}
// Team picks should be processed ONLY server-side!
// Don't make a spawn request if we're the client.
if (!IsServer && m_NetworkEnabled) {
return false;
}
if (e.Value == 0) {
return false;
}
//TODO: Spectating?
//Right now, return if someone picks spectator.
//1 signifies spectator here, could not get Playerteam component since it may be invalid or without team comp.
if ((ComponentInfo::EnumType)e.Value == 1) {
return false;
}
//Check if the player already requested spawn.
// Check if the player already requested spawn.
auto iter = m_SpawnRequests.begin();
for (; iter != m_SpawnRequests.end(); ++iter) {
if (iter->PlayerID == e.PlayerID) {
@@ -114,11 +140,11 @@ bool PlayerSpawnSystem::OnInputCommand(Events::InputCommand& e)
}
if (iter != m_SpawnRequests.end()) {
//If player is in queue to spawn, then change their team affiliation in the request.
// If player is in queue to spawn, then change their team affiliation in the request.
iter->Team = (ComponentInfo::EnumType)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,
//but only if they are spectating and/or just connected.
// If player is not in queue to spawn, then create a spawn request,
// but only if they are spectating and/or just connected.
SpawnRequest req;
req.PlayerID = e.PlayerID;
req.Team = (ComponentInfo::EnumType)e.Value;