From 32f73201c5cc1dfca7513722889c9e332e905fdc Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 23 Jan 2016 12:15:24 +0100 Subject: [PATCH] Basic jumping! --- .../Engine/Input/FirstPersonInputController.h | 93 +++++++++++++++++++ .../Input/FirstPersonInputController.cpp | 85 ----------------- src/Game/Systems/PlayerMovementSystem.cpp | 9 ++ 3 files changed, 102 insertions(+), 85 deletions(-) diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h index 4a55bf39..426670c4 100644 --- a/include/Engine/Input/FirstPersonInputController.h +++ b/include/Engine/Input/FirstPersonInputController.h @@ -13,6 +13,8 @@ public: virtual const glm::vec3 Movement() const { return m_Movement; } virtual const glm::vec3 Rotation() const { return m_Rotation; } + virtual bool Jumping() const { return m_Jumping; } + virtual bool Crouching() const { return m_Crouching; } void LockMouse(); void UnlockMouse(); @@ -24,6 +26,8 @@ protected: bool m_MouseLocked = false; glm::vec3 m_Rotation; glm::vec3 m_Movement; + bool m_Jumping = false; + bool m_Crouching = false; EventRelay m_ELockMouse; bool OnLockMouse(const Events::LockMouse& e); @@ -31,4 +35,93 @@ protected: bool OnUnlockMouse(const Events::UnlockMouse& e); }; +template +FirstPersonInputController::FirstPersonInputController(EventBroker* eventBroker, int playerID) + : InputController(eventBroker) + , m_PlayerID(playerID) +{ + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &FirstPersonInputController::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &FirstPersonInputController::OnUnlockMouse); +} + +template +void FirstPersonInputController::Reset() +{ + m_Rotation = glm::vec3(0.f, 0.f, 0.f); + m_Jumping = false; +} + +template +void FirstPersonInputController::LockMouse() +{ + Events::LockMouse e; + m_EventBroker->Publish(e); + m_MouseLocked = true; +} + +template +void FirstPersonInputController::UnlockMouse() +{ + Events::UnlockMouse e; + m_EventBroker->Publish(e); + m_MouseLocked = false; +} + +template +bool FirstPersonInputController::OnCommand(const Events::InputCommand& e) +{ + if (m_PlayerID != e.PlayerID) { + return false; + } + + 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(), glm::half_pi()); + } + + if (e.Command == "Yaw") { + float val = glm::radians(e.Value); + m_Rotation.y += -val; + } + + if (e.Command == "Forward" || e.Command == "Right") { + if (e.Command == "Forward") { + float val = glm::clamp(e.Value, -1.f, 1.f); + m_Movement.z = -val; + } + if (e.Command == "Right") { + float val = glm::clamp(e.Value, -1.f, 1.f); + m_Movement.x = val; + } + if (glm::length2(m_Movement) > 0) { + m_Movement = glm::normalize(m_Movement); + } + } + + if (e.Command == "Jump") { + m_Jumping = e.Value > 0; + } + + if (e.Command == "Crouch") { + m_Crouching = e.Value > 0; + } + + return true; +} + +template +bool FirstPersonInputController::OnUnlockMouse(const Events::UnlockMouse& e) +{ + m_MouseLocked = false; + return true; +} + +template +bool FirstPersonInputController::OnLockMouse(const Events::LockMouse& e) +{ + m_MouseLocked = true; + return true; +} + #endif \ No newline at end of file diff --git a/src/Engine/Input/FirstPersonInputController.cpp b/src/Engine/Input/FirstPersonInputController.cpp index cdacc606..d3757dd0 100644 --- a/src/Engine/Input/FirstPersonInputController.cpp +++ b/src/Engine/Input/FirstPersonInputController.cpp @@ -1,87 +1,2 @@ #include "Input/FirstPersonInputController.h" -template -FirstPersonInputController::FirstPersonInputController(EventBroker* eventBroker, int playerID) - : InputController(eventBroker) - , m_PlayerID(playerID) -{ - EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &FirstPersonInputController::OnLockMouse); - EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &FirstPersonInputController::OnUnlockMouse); -} - -template -void FirstPersonInputController::Reset() -{ - m_Rotation = glm::vec3(0.f, 0.f, 0.f); -} - -template -void FirstPersonInputController::LockMouse() -{ - Events::LockMouse e; - m_EventBroker->Publish(e); - m_MouseLocked = true; -} - -template -void FirstPersonInputController::UnlockMouse() -{ - Events::UnlockMouse e; - m_EventBroker->Publish(e); - m_MouseLocked = false; -} - -template -bool FirstPersonInputController::OnCommand(const Events::InputCommand& e) -{ - if (m_PlayerID != e.PlayerID) { - return false; - } - - 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(), glm::half_pi()); - //m_Rotation = m_Rotation * glm::angleAxis(-val, glm::vec3(1.f, 0, 0)); - return true; - } - - if (e.Command == "Yaw") { - float val = glm::radians(e.Value); - m_Rotation.y += -val; - //m_Rotation = glm::angleAxis(-val, glm::vec3(0, 1.f, 0)) * m_Rotation; - return true; - } - - if (e.Command == "Forward" || e.Command == "Right") { - if (e.Command == "Forward") { - float val = glm::clamp(e.Value, -1.f, 1.f); - m_Movement.z = -val; - return true; - } - if (e.Command == "Right") { - float val = glm::clamp(e.Value, -1.f, 1.f); - m_Movement.x = val; - return true; - } - if (glm::length2(m_Movement) > 0) { - m_Movement = glm::normalize(m_Movement); - } - } - - return false; -} - -template -bool FirstPersonInputController::OnUnlockMouse(const Events::UnlockMouse& e) -{ - m_MouseLocked = false; - return true; -} - -template -bool FirstPersonInputController::OnLockMouse(const Events::LockMouse& e) -{ - m_MouseLocked = true; - return true; -} diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index 843601e8..10fc29f6 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -28,6 +28,8 @@ void PlayerMovementSystem::Update(double dt) if (cameraEntity.Valid()) { glm::vec3& cameraOrientation = cameraEntity["Transform"]["Orientation"]; cameraOrientation.x += controller->Rotation().x; + // Limit camera pitch so we don't break our necks + cameraOrientation.x = glm::clamp(cameraOrientation.x, -glm::half_pi(), glm::half_pi()); } ComponentWrapper& cTransform = player["Transform"]; @@ -36,6 +38,13 @@ void PlayerMovementSystem::Update(double dt) glm::vec3& pos = cTransform["Position"]; pos += controller->Movement() * glm::inverse(glm::quat(ori)) * (float)player["Player"]["MovementSpeed"] * (float)dt; + + if (player.HasComponent("Physics")) { + if (controller->Jumping()) { + glm::vec3& velocity = player["Physics"]["Velocity"]; + velocity.y += 10.f; + } + } controller->Reset(); }