Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
PlayerControllerComponent.h
Go to the documentation of this file.
1/**
2 * @file PlayerControllerComponent.h
3 * @ingroup mnd_components
4 * @brief First-person Quake/Half-Life-style movement controller.
5 *
6 * Implements ground friction, ground/air acceleration with a per-axis wishspeed
7 * cap (the trick that enables strafe-jumping and bunny-hopping), and an
8 * optional auto-hop while the jump key is held.
9 *
10 * Attach to a GameObject that also has a CameraComponent. Each frame Update()
11 * reads keyboard (WASD + Space) and mouse-delta input from the Engine's
12 * InputManager and drives a KinematicCharacterController.
13 *
14 * @see InputManager, CameraComponent, KinematicCharacterController
15 */
16
17#pragma once
18
19#include <memory>
20
21#include "Constants.h"
22#include "Types.h"
23#include "physics/KinematicCharacterController.h"
24#include "scene/Component.h"
25
26namespace mnd
27{
28
30{
32public:
33 void Init() override;
34 void Update(f32 deltaTime) override;
35
36 [[nodiscard]] bool OnGround() const;
37
38 void SetMoveSpeed(f32 speed);
39 [[nodiscard]] f32 GetMoveSpeed() const;
40
41 [[nodiscard]] f32 GetSensitivity() const { return m_sensitivity; }
42 [[nodiscard]] f32 GetJumpSpeed() const { return m_jumpSpeed; }
43
44 void SetSensitivity(f32 s) { m_sensitivity = s; }
45 void SetJumpSpeed(f32 s) { m_jumpSpeed = s; }
46
47 // Quake tunables --------------------------------------------------------
48 [[nodiscard]] f32 GetGroundAccel() const { return m_groundAccel; }
49 [[nodiscard]] f32 GetAirAccel() const { return m_airAccel; }
50 [[nodiscard]] f32 GetFriction() const { return m_friction; }
51 [[nodiscard]] f32 GetAirCap() const { return m_airCap; }
52 [[nodiscard]] bool GetAutoHop() const { return m_autoHop; }
53
54 void SetGroundAccel(f32 v) { m_groundAccel = v; }
55 void SetAirAccel(f32 v) { m_airAccel = v; }
56 void SetFriction(f32 v) { m_friction = v; }
57 void SetAirCap(f32 v) { m_airCap = v; }
58 void SetAutoHop(bool v) { m_autoHop = v; }
59
60private:
61 /// Quake PM_Friction: shrink horizontal velocity each frame on ground.
62 void ApplyFriction(f32 deltaTime);
63 /// Quake PM_Accelerate: project velocity onto wishdir, add up to wishspeed.
64 void Accelerate(const vec3 &wishDir, f32 wishSpeed, f32 accel, f32 deltaTime);
65
66 f32 m_sensitivity = kDefaultMouseSensitivity;
67 f32 m_moveSpeed = kDefaultMoveSpeed;
68 f32 m_jumpSpeed = kDefaultJumpSpeed;
69 f32 m_yRot = 0.0F;
70 f32 m_xRot = 0.0F;
71
72 f32 m_groundAccel = kQuakeGroundAccel;
73 f32 m_airAccel = kQuakeAirAccel;
74 f32 m_friction = kQuakeFriction;
75 f32 m_airCap = kQuakeAirCap;
76 bool m_autoHop = true; ///< Auto-rejump while Space is held (bhop).
77
78 vec3 m_velocity = vec3(0.0F); ///< Tracked horizontal velocity (XZ; Y unused).
79 bool m_jumpHeldLast = false; ///< Edge-detect Space release for non-auto-hop.
80
81 std::unique_ptr<KinematicCharacterController> m_kinematicController;
82};
83
84} // namespace mnd
Engine-wide compile-time tunables and ANSI colour codes.
constexpr mnd::f32 kQuakeAirAccel
sv_airaccelerate (HL1 = 10; Q3 = 1).
Definition Constants.h:91
constexpr mnd::f32 kDefaultJumpSpeed
Definition Constants.h:86
constexpr mnd::f32 kDefaultMouseSensitivity
Definition Constants.h:80
constexpr mnd::f32 kDefaultMoveSpeed
Definition Constants.h:83
constexpr mnd::f32 kQuakeGroundAccel
sv_accelerate.
Definition Constants.h:90
constexpr mnd::f32 kQuakeAirCap
Definition Constants.h:97
constexpr mnd::f32 kQuakeFriction
sv_friction.
Definition Constants.h:92
Engine-wide primitive type aliases and GLM math imports.
#define COMPONENT(ComponentClass)
Definition Component.h:105
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51
float f32
32-bit IEEE float — the standard GL scalar type.
Definition Types.h:40