Working Spawner, SpawnPoint, PlayerSpawn and Team components, along with their systems.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "Core/EntityWrapper.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
const EntityWrapper EntityWrapper::Invalid = EntityWrapper(nullptr, EntityID_Invalid);
|
||||
|
||||
bool EntityWrapper::operator==(const EntityWrapper& e)
|
||||
{
|
||||
return (this->World == e.World) && (this->ID == e.ID);
|
||||
|
||||
@@ -63,7 +63,7 @@ void InputProxy::Process()
|
||||
e.Command = command;
|
||||
e.Value = currentValue;
|
||||
m_EventBroker->Publish(e);
|
||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
m_LastCommandValues[command] = currentValue;
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ void InputProxy::Process()
|
||||
}
|
||||
//e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
||||
m_EventBroker->Publish(e);
|
||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
}
|
||||
m_CommandQueue.clear();
|
||||
}
|
||||
|
||||
@@ -8,17 +8,29 @@ PlayerSpawnSystem::PlayerSpawnSystem(EventBroker* eventBroker)
|
||||
|
||||
void PlayerSpawnSystem::Update(World* world, double dt)
|
||||
{
|
||||
auto componentPools = world->GetComponentPools();
|
||||
auto spawnerPool = componentPools.find("Spawner");
|
||||
if (spawnerPool == componentPools.end()) {
|
||||
auto playerSpawns = world->GetComponents("PlayerSpawn");
|
||||
if (playerSpawns == nullptr) {
|
||||
return;
|
||||
}
|
||||
;
|
||||
|
||||
for (auto& team : m_SpawnRequests) {
|
||||
for (auto& spawner : *spawnerPool->second) {
|
||||
Events::SpawnerSpawn e;
|
||||
e.Spawner = EntityWrapper(world, spawner.EntityID);
|
||||
m_EventBroker->Publish(e);
|
||||
for (auto& cPlayerSpawn : *playerSpawns) {
|
||||
EntityWrapper spawner(world, cPlayerSpawn.EntityID);
|
||||
if (!spawner.HasComponent("Spawner")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the spawner has a team affiliation, check it
|
||||
if (spawner.HasComponent("Team")) {
|
||||
if ((int)spawner["Team"]["Team"] != team) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Spawn the player!
|
||||
EntityWrapper player = SpawnerSystem::Spawn(spawner);
|
||||
// Set the player team affiliation
|
||||
player["Team"]["Team"] = team;
|
||||
}
|
||||
}
|
||||
m_SpawnRequests.clear();
|
||||
|
||||
@@ -5,43 +5,57 @@ SpawnerSystem::SpawnerSystem(EventBroker* eventBroker) : System(eventBroker)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_OnSpawnerSpawn, &SpawnerSystem::OnSpawnerSpawn);
|
||||
}
|
||||
|
||||
bool SpawnerSystem::OnSpawnerSpawn(Events::SpawnerSpawn& e)
|
||||
EntityWrapper SpawnerSystem::Spawn(EntityWrapper spawner, EntityWrapper parent /*= EntityWrapper::Invalid*/)
|
||||
{
|
||||
EntityWrapper& spawner = e.Spawner;
|
||||
// Spawn the entity in the parent's world if it exists, otherwise in the spawner's world
|
||||
World* world = parent.World;
|
||||
if (world == nullptr) {
|
||||
world = spawner.World;
|
||||
}
|
||||
|
||||
// Find any SpawnPoints existing as children of spawner
|
||||
auto children = spawner.World->GetChildren(spawner.ID);
|
||||
std::vector<EntityID> spawnPoints;
|
||||
std::vector<EntityWrapper> spawnPoints;
|
||||
for (auto kv = children.first; kv != children.second; ++kv) {
|
||||
const EntityID& child = kv->second;
|
||||
if (spawner.World->HasComponent(child, "SpawnPoint")) {
|
||||
spawnPoints.push_back(child);
|
||||
spawnPoints.push_back(EntityWrapper(spawner.World, child));
|
||||
}
|
||||
}
|
||||
|
||||
EntityID spawnPoint = spawner.ID;
|
||||
// Choose a random SpawnPoint
|
||||
EntityWrapper spawnPoint = spawner;
|
||||
if (!spawnPoints.empty()) {
|
||||
// Select a random spawn point
|
||||
static std::random_device randomDevice;
|
||||
static std::mt19937 randomGenerator(randomDevice());
|
||||
std::uniform_int_distribution<> distribution(0, std::distance(spawnPoints.begin(), spawnPoints.end()) - 1);
|
||||
auto randomSpawnPointIt = spawnPoints.begin();
|
||||
std::advance(randomSpawnPointIt, distribution(randomGenerator));
|
||||
spawnPoint = *randomSpawnPointIt;
|
||||
if (spawnPoints.size() > 1) {
|
||||
static std::random_device randomDevice;
|
||||
static std::mt19937 randomGenerator(randomDevice());
|
||||
std::uniform_int_distribution<> distribution(0, std::distance(spawnPoints.begin(), spawnPoints.end()) - 1);
|
||||
auto randomSpawnPointIt = spawnPoints.begin();
|
||||
std::advance(randomSpawnPointIt, distribution(randomGenerator));
|
||||
spawnPoint = *randomSpawnPointIt;
|
||||
} else {
|
||||
spawnPoint = spawnPoints.front();
|
||||
}
|
||||
}
|
||||
spawnEntity(spawner, e.Parent.ID, Transform::AbsolutePosition(spawner.World, spawnPoint));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpawnerSystem::spawnEntity(EntityWrapper spawner, EntityID parent, glm::vec3 position)
|
||||
{
|
||||
|
||||
// Load the entity file and parse it
|
||||
const std::string& entityFilePath = spawner["Spawner"]["EntityFile"];
|
||||
auto entityFile = ResourceManager::Load<EntityFile>(entityFilePath);
|
||||
if (entityFile == nullptr) {
|
||||
return;
|
||||
return EntityWrapper::Invalid;
|
||||
}
|
||||
|
||||
EntityFileParser parser(entityFile);
|
||||
EntityWrapper spawnedEntity(spawner.World, parser.MergeEntities(spawner.World, parent));
|
||||
spawnedEntity["Transform"]["Position"] = position;
|
||||
EntityWrapper spawnedEntity(world, parser.MergeEntities(world, parent.ID));
|
||||
|
||||
// Set its position and orientation to that of the SpawnPoint
|
||||
spawnedEntity["Transform"]["Position"] = Transform::AbsolutePosition(spawnPoint.World, spawnPoint.ID);
|
||||
spawnedEntity["Transform"]["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(spawnPoint.World, spawnPoint.ID));
|
||||
|
||||
return spawnedEntity;
|
||||
}
|
||||
|
||||
bool SpawnerSystem::OnSpawnerSpawn(Events::SpawnerSpawn& e)
|
||||
{
|
||||
EntityWrapper spawnedEntity = Spawn(e.Spawner, e.Parent);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user