Merge remote-tracking branch 'origin/PlayerMovement' into Networking
This commit is contained in:
@@ -1,5 +1,45 @@
|
||||
#include "Systems/PlayerMovementSystem.h"
|
||||
|
||||
PlayerMovementSystem::PlayerMovementSystem(World* world, EventBroker* eventBroker)
|
||||
: System(world, eventBroker)
|
||||
, PureSystem("Player")
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &PlayerMovementSystem::OnPlayerSpawned);
|
||||
}
|
||||
|
||||
PlayerMovementSystem::~PlayerMovementSystem()
|
||||
{
|
||||
for (auto& kv : m_PlayerInputControllers) {
|
||||
delete kv.second;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerMovementSystem::Update(double dt)
|
||||
{
|
||||
for (auto& kv : m_PlayerInputControllers) {
|
||||
EntityWrapper player = kv.first;
|
||||
auto& controller = kv.second;
|
||||
|
||||
if (!player.Valid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
EntityWrapper cameraEntity = player.FirstChildByName("Camera");
|
||||
if (cameraEntity.Valid()) {
|
||||
glm::vec3& cameraOrientation = cameraEntity["Transform"]["Orientation"];
|
||||
cameraOrientation.x = controller->Orientation().x;
|
||||
}
|
||||
|
||||
ComponentWrapper& cTransform = player["Transform"];
|
||||
glm::vec3& ori = cTransform["Orientation"];
|
||||
ori.y = controller->Orientation().y;
|
||||
|
||||
glm::vec3& pos = cTransform["Position"];
|
||||
pos += controller->Movement() * glm::inverse(glm::quat(ori)) * (float)player["Player"]["MovementSpeed"] * (float)dt;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
ComponentWrapper& cTransform = entity["Transform"];
|
||||
@@ -10,9 +50,17 @@ void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp
|
||||
|
||||
glm::vec3& velocity = cPhysics["Velocity"];
|
||||
if (cPhysics["Gravity"]) {
|
||||
velocity.y -= 9.82 * dt;
|
||||
velocity.y -= 9.82f * (float)dt;
|
||||
}
|
||||
|
||||
glm::vec3& position = cTransform["Position"];
|
||||
position += velocity * (float)dt;
|
||||
}
|
||||
}
|
||||
|
||||
bool PlayerMovementSystem::OnPlayerSpawned(Events::PlayerSpawned& e)
|
||||
{
|
||||
// When a player spawns, create an input controller for them
|
||||
m_PlayerInputControllers[e.Player] = new FirstPersonInputController<PlayerMovementSystem>(m_EventBroker, e.PlayerID);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ void PlayerSpawnSystem::Update(double dt)
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& team : m_SpawnRequests) {
|
||||
for (auto& req : m_SpawnRequests) {
|
||||
for (auto& cPlayerSpawn : *playerSpawns) {
|
||||
EntityWrapper spawner(m_World, cPlayerSpawn.EntityID);
|
||||
if (!spawner.HasComponent("Spawner")) {
|
||||
@@ -22,7 +22,7 @@ void PlayerSpawnSystem::Update(double dt)
|
||||
|
||||
// If the spawner has a team affiliation, check it
|
||||
if (spawner.HasComponent("Team")) {
|
||||
if ((int)spawner["Team"]["Team"] != team) {
|
||||
if ((int)spawner["Team"]["Team"] != req.Team) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,22 @@ void PlayerSpawnSystem::Update(double dt)
|
||||
// Spawn the player!
|
||||
EntityWrapper player = SpawnerSystem::Spawn(spawner);
|
||||
// Set the player team affiliation
|
||||
player["Team"]["Team"] = team;
|
||||
player["Team"]["Team"] = req.Team;
|
||||
|
||||
// Publish a PlayerSpawned event
|
||||
Events::PlayerSpawned e;
|
||||
e.PlayerID = req.PlayerID;
|
||||
e.Player = player;
|
||||
e.Spawner = spawner;
|
||||
m_EventBroker->Publish(e);
|
||||
|
||||
// Set the camera to the correct entity
|
||||
EntityWrapper cameraEntity = player.FirstChildByName("Camera");
|
||||
if (cameraEntity.Valid()) {
|
||||
Events::SetCamera e;
|
||||
e.CameraEntity = cameraEntity;
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
m_SpawnRequests.clear();
|
||||
@@ -43,7 +58,10 @@ bool PlayerSpawnSystem::OnInputCommand(const Events::InputCommand& e)
|
||||
}
|
||||
|
||||
if (e.Value != 0) {
|
||||
m_SpawnRequests.push_back((int)e.Value);
|
||||
SpawnRequest req;
|
||||
req.PlayerID = e.PlayerID;
|
||||
req.Team = (ComponentInfo::EnumType)e.Value;
|
||||
m_SpawnRequests.push_back(req);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user