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