Player input properly separated from editor input and server
This commit is contained in:
@@ -23,13 +23,17 @@ public:
|
||||
m_SpeedMultiplier = m_Config->Get<float>("Editor.CameraSpeed", 3.f);
|
||||
}
|
||||
|
||||
virtual const glm::vec3 Movement() const override
|
||||
{
|
||||
return m_Movement * m_SpeedMultiplier;
|
||||
}
|
||||
virtual const glm::vec3 Movement() const override { return m_Movement * m_SpeedMultiplier; }
|
||||
|
||||
void Enable() { m_Enabled = true; }
|
||||
void Disable() { m_Enabled = false; }
|
||||
|
||||
virtual bool OnCommand(const Events::InputCommand& e) override
|
||||
{
|
||||
if (!m_MouseLocked) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if (glm::abs(e.Value) > 0 && (io.WantCaptureKeyboard || io.WantCaptureMouse)) {
|
||||
return false;
|
||||
@@ -64,11 +68,16 @@ public:
|
||||
|
||||
protected:
|
||||
ConfigFile* m_Config;
|
||||
bool m_Enabled = false;
|
||||
float m_SpeedMultiplier = 1.f;
|
||||
|
||||
EventRelay<EventContext, Events::MousePress> m_EMousePress;
|
||||
bool OnMousePress(const Events::MousePress& e)
|
||||
{
|
||||
if (!m_Enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.Button == GLFW_MOUSE_BUTTON_2) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if (!io.WantCaptureMouse) {
|
||||
@@ -80,6 +89,10 @@ protected:
|
||||
EventRelay<EventContext, Events::MouseRelease> m_EMouseRelease;
|
||||
bool OnMouseRelease(const Events::MouseRelease& e)
|
||||
{
|
||||
if (!m_Enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.Button == GLFW_MOUSE_BUTTON_2) {
|
||||
UnlockMouse();
|
||||
}
|
||||
@@ -88,6 +101,10 @@ protected:
|
||||
EventRelay<EventContext, Events::MouseScroll> m_EMouseScroll;
|
||||
bool OnMouseScroll(const Events::MouseScroll& e)
|
||||
{
|
||||
if (!m_Enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_SpeedMultiplier += e.DeltaY * (0.1f * m_SpeedMultiplier);
|
||||
m_Config->Set("Editor.CameraSpeed", m_SpeedMultiplier);
|
||||
m_Config->SaveToDisk();
|
||||
|
||||
@@ -18,7 +18,7 @@ public:
|
||||
}
|
||||
|
||||
virtual const glm::vec3 Movement() const { return m_Movement; }
|
||||
virtual const glm::vec3 Orientation() const { return m_Orientation; }
|
||||
virtual const glm::vec3 Rotation() const { return m_Rotation; }
|
||||
|
||||
void LockMouse()
|
||||
{
|
||||
@@ -40,21 +40,19 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_MouseLocked) {
|
||||
if (e.Command == "Pitch") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation.x += -val;
|
||||
m_Orientation.x = glm::clamp(m_Orientation.x, -glm::half_pi<float>(), glm::half_pi<float>());
|
||||
//m_Orientation = m_Orientation * glm::angleAxis<float>(-val, glm::vec3(1.f, 0, 0));
|
||||
return true;
|
||||
}
|
||||
if (e.Command == "Pitch") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Rotation.x += -val;
|
||||
m_Rotation.x = glm::clamp(m_Rotation.x, -glm::half_pi<float>(), glm::half_pi<float>());
|
||||
//m_Rotation = m_Rotation * glm::angleAxis<float>(-val, glm::vec3(1.f, 0, 0));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (e.Command == "Yaw") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation.y += -val;
|
||||
//m_Orientation = glm::angleAxis<float>(-val, glm::vec3(0, 1.f, 0)) * m_Orientation;
|
||||
return true;
|
||||
}
|
||||
if (e.Command == "Yaw") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Rotation.y += -val;
|
||||
//m_Rotation = glm::angleAxis<float>(-val, glm::vec3(0, 1.f, 0)) * m_Rotation;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (e.Command == "Forward" || e.Command == "Right") {
|
||||
@@ -75,11 +73,16 @@ public:
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void Reset()
|
||||
{
|
||||
m_Rotation = glm::vec3(0.f, 0.f, 0.f);
|
||||
}
|
||||
|
||||
protected:
|
||||
const int m_PlayerID;
|
||||
bool m_MouseLocked = false;
|
||||
glm::vec3 m_Orientation;
|
||||
glm::vec3 m_Rotation;
|
||||
glm::vec3 m_Movement;
|
||||
|
||||
EventRelay<EventContext, Events::LockMouse> m_ELockMouse;
|
||||
|
||||
@@ -72,8 +72,8 @@ void EditorSystem::Update(double dt)
|
||||
|
||||
ComponentWrapper& cameraTransform = m_EditorCamera["Transform"];
|
||||
glm::vec3& ori = cameraTransform["Orientation"];
|
||||
ori.x = m_EditorCameraInputController->Orientation().x;
|
||||
ori.y = m_EditorCameraInputController->Orientation().y;
|
||||
ori.x = m_EditorCameraInputController->Rotation().x;
|
||||
ori.y = m_EditorCameraInputController->Rotation().y;
|
||||
glm::vec3& pos = cameraTransform["Position"];
|
||||
pos += m_EditorCameraInputController->Movement() * glm::inverse(glm::quat(ori)) * (float)actualDelta;
|
||||
}
|
||||
@@ -81,6 +81,8 @@ void EditorSystem::Update(double dt)
|
||||
|
||||
void EditorSystem::Enable()
|
||||
{
|
||||
m_EditorCameraInputController->Enable();
|
||||
m_EventBroker->Publish(Events::UnlockMouse());
|
||||
Events::SetCamera e;
|
||||
e.CameraEntity = m_EditorCamera;
|
||||
m_EventBroker->Publish(e);
|
||||
@@ -90,6 +92,8 @@ void EditorSystem::Enable()
|
||||
|
||||
void EditorSystem::Disable()
|
||||
{
|
||||
m_EditorCameraInputController->Disable();
|
||||
m_EventBroker->Publish(Events::LockMouse());
|
||||
Events::SetCamera e;
|
||||
e.CameraEntity = m_ActualCamera;
|
||||
m_EventBroker->Publish(e);
|
||||
@@ -179,6 +183,10 @@ bool EditorSystem::OnWidgetDelta(const Events::WidgetDelta& e)
|
||||
|
||||
bool EditorSystem::OnInputCommand(const Events::InputCommand& e)
|
||||
{
|
||||
if (e.PlayerID != -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.Command == "ToggleEditor" && e.Value > 0) {
|
||||
if (m_Enabled) {
|
||||
Disable();
|
||||
|
||||
@@ -27,16 +27,17 @@ void PlayerMovementSystem::Update(double dt)
|
||||
EntityWrapper cameraEntity = player.FirstChildByName("Camera");
|
||||
if (cameraEntity.Valid()) {
|
||||
glm::vec3& cameraOrientation = cameraEntity["Transform"]["Orientation"];
|
||||
cameraOrientation.x = controller->Orientation().x;
|
||||
cameraOrientation.x += controller->Rotation().x;
|
||||
}
|
||||
|
||||
ComponentWrapper& cTransform = player["Transform"];
|
||||
glm::vec3& ori = cTransform["Orientation"];
|
||||
ori.y = controller->Orientation().y;
|
||||
ori.y += controller->Rotation().y;
|
||||
|
||||
glm::vec3& pos = cTransform["Position"];
|
||||
pos += controller->Movement() * glm::inverse(glm::quat(ori)) * (float)player["Player"]["MovementSpeed"] * (float)dt;
|
||||
|
||||
|
||||
controller->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user