Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
PlayerControllerComponent.cpp
Go to the documentation of this file.
1#include "scene/components/PlayerControllerComponent.h"
2#include "input/InputManager.h"
3#include "Engine.h"
4#include <GLFW/glfw3.h>
5#include <glm/gtc/matrix_transform.hpp>
6#include <glm/vec4.hpp>
7
8namespace eng
9{
11 {
12 m_kinematicController = std::make_unique<KinematicCharacterController>(0.4f, 1.2f, m_owner->GetWorldPosition());
13 }
14
16 {
17 auto& inputManager = Engine::GetInstance().GetInputManager();
18 auto rotation = m_owner->GetRotation();
19
20 if (inputManager.IsMousePositionChanged())
21 {
22 const auto& oldPos = inputManager.GetMousePositionOld();
23 const auto& currentPos = inputManager.GetMousePositionCurrent();
24
25 float deltaX = currentPos.x - oldPos.x;
26 float deltaY = currentPos.y - oldPos.y;
27
28 // rot around Y axis
29 float yDeltaAngle = -deltaX * m_sensitivity * deltaTime;
30 m_yRot += yDeltaAngle;
31 glm::quat yRot = glm::angleAxis(glm::radians(m_yRot), glm::vec3(0.0f, 1.0f, 0.0f));
32
33 // rot around X axis
34 float xDeltaAngle = -deltaY * m_sensitivity * deltaTime;
35 m_xRot += xDeltaAngle;
36 m_xRot = std::clamp(m_xRot, -89.0f, 89.0f);
37 glm::quat xRot = glm::angleAxis(glm::radians(m_xRot), glm::vec3(1.0f, 0.0f, 0.0f));
38
39 rotation = glm::normalize(yRot * xRot);
40
41 m_owner->SetRotation(rotation);
42 }
43
44 glm::vec3 front = rotation * glm::vec3(0.0f, 0.0f, -1.0f);
45 glm::vec3 right = rotation * glm::vec3(1.0f, 0.0f, 0.0f);
46
47 glm::vec3 move(0.0f);
48 // Left/Right movement
49 if (inputManager.IsKeyPressed(GLFW_KEY_A))
50 {
51 move -= right;
52 }
53 else if (inputManager.IsKeyPressed(GLFW_KEY_D))
54 {
55 move += right;
56 }
57 // Vertical movement
58 if (inputManager.IsKeyPressed(GLFW_KEY_W))
59 {
60 move += front;
61 }
62 else if (inputManager.IsKeyPressed(GLFW_KEY_S))
63 {
64 move -= front;
65 }
66
67 if (inputManager.IsKeyPressed(GLFW_KEY_SPACE))
68 {
69 m_kinematicController->Jump(glm::vec3(0.0f, 5.0f, 0.0f));
70 }
71
72 if (glm::dot(move, move) > 0)
73 {
74 move = glm::normalize(move);
75 }
76 m_kinematicController->Walk(move * m_moveSpeed * deltaTime);
77
78 m_owner->SetPosition(m_kinematicController->GetPosition());
79 }
80
82 {
83 if (m_kinematicController)
84 {
85 return m_kinematicController->OnGround();
86 }
87 return false;
88 }
89}
GameObject * m_owner
Definition Component.h:33
InputManager & GetInputManager()
Definition Engine.cpp:190
static Engine & GetInstance()
Definition Engine.cpp:50
const glm::quat & GetRotation() const
glm::vec3 GetWorldPosition() const
void SetPosition(const glm::vec3 &pos)
void SetRotation(const glm::quat &rot)
void Update(float deltaTime) override