Walking sound logic moved to PlayerMovementSystem and makes use of "wishDirection" to alleviate the problem where m_DistanceMoved were reset when it was not supposed to.

This commit is contained in:
stiffly
2016-02-08 14:23:53 +01:00
parent 3a5c0a2762
commit 8e86cc2123
4 changed files with 53 additions and 29 deletions
@@ -5,6 +5,7 @@
#include "Input/FirstPersonInputController.h"
#include <imgui/imgui.h>
#include "Events/EDoubleJump.h"
#include "../Engine/Sound/EPlaySoundOnEntity.h"
class PlayerMovementSystem : public ImpureSystem, PureSystem
{
@@ -19,6 +20,19 @@ private:
// State
std::unordered_map<EntityWrapper, FirstPersonInputController<PlayerMovementSystem>*> m_PlayerInputControllers;
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
// Walking logic
// Keeps track of how far the player has walked within this "key press session".
float m_DistanceMoved = 0.0f;
// How far a step is (How often the step sound will be played).
const float m_PlayerStepLength = 1.75f;
// Determine what sound file to play.
bool m_LeftFoot = false;
// To get a difference when calculating the walking state.
glm::vec3 m_LastPosition = glm::vec3();
// The logic for making the sound play when player is moving
void playerStep(double dt);
EventRelay<PlayerMovementSystem, Events::PlayerSpawned> m_EPlayerSpawned;
bool OnPlayerSpawned(Events::PlayerSpawned& e);
-2
View File
@@ -30,8 +30,6 @@ public:
virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComponent, double dt) override;
virtual void Update(double dt) override;
private:
// The logic for making the sound play when player is moving
void playerStep(double dt);
EntityWrapper m_LocalPlayer = EntityWrapper();
World* m_World = nullptr;
+39 -1
View File
@@ -56,6 +56,12 @@ void PlayerMovementSystem::Update(double dt)
} else {
wishSpeed = playerMovementSpeed;
}
if (player.ID == m_LocalPlayer.ID) {
if (glm::length(wishDirection) == 0) {
// If no key is pressed, reset the distance moved since last step.
m_DistanceMoved = 0;
}
}
glm::vec3& velocity = cPhysics["Velocity"];
ImGui::Text("velocity: (%f, %f, %f)", velocity.x, velocity.y, velocity.z);
glm::vec3 groundVelocity(0.f, 0.f, 0.f);
@@ -135,6 +141,7 @@ void PlayerMovementSystem::Update(double dt)
controller->Reset();
}
playerStep(dt);
}
void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
@@ -169,10 +176,41 @@ void PlayerMovementSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp
position += velocity * (float)dt;
}
void PlayerMovementSystem::playerStep(double dt)
{
if (!m_LocalPlayer.Valid()) {
return;
}
// Position of the local player, used see how far a player has moved.
glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"];
// Velocity of the local player, used to see if a player is airborne.
glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"];
m_DistanceMoved += glm::length(pos - m_LastPosition);
// Set the last position for next iteration
m_LastPosition = pos;
bool isAirborne = vel.y != 0;
if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) {
// Player moved a step's distance
// Create footstep sound
Events::PlaySoundOnEntity e;
EntityID child = m_World->CreateEntity(m_LocalPlayer.ID);
m_World->AttachComponent(child, "Transform");
m_World->AttachComponent(child, "SoundEmitter");
e.EmitterID = child;
e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav";
m_LeftFoot = !m_LeftFoot;
m_EventBroker->Publish(e);
m_DistanceMoved = 0.f;
}
}
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);
if (e.PlayerID == -1) {
// Keep track of the local player
m_LocalPlayer = e.Player;
}
return true;
}
-26
View File
@@ -25,32 +25,6 @@ void SoundSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cComp
void SoundSystem::Update(double dt)
{
playerStep(dt);
}
void SoundSystem::playerStep(double dt)
{
if (!m_LocalPlayer.Valid()) {
return;
}
// Position of the local player, used see how far a player has moved.
glm::vec3 pos = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Transform")["Position"];
// Velocity of the local player, used to see if a player is airborne.
glm::vec3 vel = (glm::vec3)m_World->GetComponent(m_LocalPlayer.ID, "Physics")["Velocity"];
m_DistanceMoved += glm::length(pos - m_LastPosition);
// Set the last position for next iteration
m_LastPosition = pos;
bool isAirborne = vel.y != 0;
if (m_DistanceMoved > m_PlayerStepLength && !isAirborne) {
// Player moved a step's distance
// Create footstep sound
Events::PlaySoundOnEntity e;
e.EmitterID = createChildEmitter(m_LocalPlayer);
e.FilePath = m_LeftFoot ? "Audio/footstep/footstep2.wav" : "Audio/footstep/footstep3.wav";
m_LeftFoot = !m_LeftFoot;
m_EventBroker->Publish(e);
m_DistanceMoved = 0.f;
}
}
bool SoundSystem::OnPlayerSpawned(const Events::PlayerSpawned &e)