Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
CameraManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <GLFW/glfw3.h>
4#include <memory>
5
8#include "core/Core.hpp"
9#include "entity/Entity.hpp"
11
13class ICameraBehavior;
14}
15
17
25 public:
26 explicit CameraManager(Engine::Core &core) : _core(core) {}
27
28 ~CameraManager() = default;
29
37 {
38 if (!entity.IsAlive())
39 {
40 throw CameraMovementError("Camera entity is invalid");
41 }
42
44 {
45 throw CameraMovementError("Camera entity must have both Transform and Camera components");
46 }
47
48 _cameraEntity = entity;
49 }
50
58 {
59 if (!_cameraEntity.has_value() || !_cameraEntity->IsAlive())
60 {
62 fmt::format("Camera entity is invalid: {}", _cameraEntity.has_value() ? "not alive" : "not set"));
63 }
64
65 auto &registry = _core.GetRegistry();
67 {
68 throw CameraMovementError("Camera entity is missing required components");
69 }
70
71 return *_cameraEntity;
72 }
73
79 bool HasValidCamera() const
80 {
81 if (!_cameraEntity.has_value() || !_cameraEntity->IsAlive())
82 {
83 return false;
84 }
85
86 auto &registry = _core.GetRegistry();
88 }
89
95 void SetMovementSpeed(float speed) { _movementSpeed = speed; }
96
102 float GetMovementSpeed() const { return _movementSpeed; }
103
109 void SetMouseSensitivity(float sensitivity) { _mouseSensitivity = sensitivity; }
110
116 float GetMouseSensitivity() const { return _mouseSensitivity; }
117
124 void SetLastMousePosition(double x, double y)
125 {
126 _lastMouseX = x;
127 _lastMouseY = y;
128 }
129
135 double GetLastMouseX() const { return _lastMouseX; }
136
142 double GetLastMouseY() const { return _lastMouseY; }
143
149 void SetMouseDragging(bool dragging) { _isMouseDragging = dragging; }
150
156 bool IsMouseDragging() const { return _isMouseDragging; }
157
163 void SetOriginRotation(const glm::quat &rotation) { _originRotation = rotation; }
164
170 const glm::quat &GetOriginRotation() const { return _originRotation; }
171
177 void SetWasCursorMasked(bool masked) { _wasCursorMasked = masked; }
178
184 bool WasCursorMasked() const { return _wasCursorMasked; }
185
192
198 void SetJoystickId(int joystickId) { _joystickId = joystickId; }
199
205 int GetJoystickId() const { return _joystickId; }
206
212 void SetBehavior(std::shared_ptr<CameraMovement::Utils::ICameraBehavior> behavior)
213 {
214 _behavior = std::move(behavior);
215 }
216
222 std::shared_ptr<CameraMovement::Utils::ICameraBehavior> GetBehavior() const { return _behavior; }
223
224 private:
226 std::optional<Engine::Entity> _cameraEntity = std::nullopt;
227 float _movementSpeed = 5.0f;
228 float _mouseSensitivity = 0.002f;
229 double _lastMouseX = 0.0;
230 double _lastMouseY = 0.0;
231 bool _isMouseDragging = false;
232 bool _wasCursorMasked = false;
233 glm::quat _originRotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
234 int _joystickId = GLFW_JOYSTICK_1;
235 std::shared_ptr<CameraMovement::Utils::ICameraBehavior> _behavior = nullptr;
236};
237
238} // namespace CameraMovement::Resource
Definition CameraMovementError.hpp:24
bool _isMouseDragging
Definition CameraManager.hpp:231
double _lastMouseX
Definition CameraManager.hpp:229
Engine::Core & _core
Definition CameraManager.hpp:225
float GetMouseSensitivity() const
Get the mouse sensitivity of the camera.
Definition CameraManager.hpp:116
void SetMovementSpeed(float speed)
Set the movement speed of the camera.
Definition CameraManager.hpp:95
CameraManager(Engine::Core &core)
Definition CameraManager.hpp:26
std::shared_ptr< CameraMovement::Utils::ICameraBehavior > GetBehavior() const
Get the current camera behavior instance.
Definition CameraManager.hpp:222
void SetMouseSensitivity(float sensitivity)
Set the mouse sensitivity of the camera.
Definition CameraManager.hpp:109
glm::quat _originRotation
Definition CameraManager.hpp:233
bool IsMouseDragging() const
Check if the mouse is being dragged.
Definition CameraManager.hpp:156
void SetLastMousePosition(double x, double y)
Set the last mouse position.
Definition CameraManager.hpp:124
float GetMovementSpeed() const
Get the movement speed of the camera.
Definition CameraManager.hpp:102
const glm::quat & GetOriginRotation() const
Get the origin rotation.
Definition CameraManager.hpp:170
void SetActiveCamera(Engine::Entity entity)
Set the active camera entity.
Definition CameraManager.hpp:36
float _mouseSensitivity
Definition CameraManager.hpp:228
Engine::Core & GetCore()
Get the core reference.
Definition CameraManager.hpp:191
float _movementSpeed
Definition CameraManager.hpp:227
bool _wasCursorMasked
Definition CameraManager.hpp:232
double _lastMouseY
Definition CameraManager.hpp:230
void SetWasCursorMasked(bool masked)
Set whether the cursor was masked in the previous frame.
Definition CameraManager.hpp:177
int GetJoystickId() const
Get the joystick ID used for camera control.
Definition CameraManager.hpp:205
void SetBehavior(std::shared_ptr< CameraMovement::Utils::ICameraBehavior > behavior)
Set the camera behavior instance.
Definition CameraManager.hpp:212
std::optional< Engine::Entity > _cameraEntity
Definition CameraManager.hpp:226
int _joystickId
Definition CameraManager.hpp:234
double GetLastMouseX() const
Get the last mouse X position.
Definition CameraManager.hpp:135
void SetMouseDragging(bool dragging)
Set whether the mouse is being dragged.
Definition CameraManager.hpp:149
bool WasCursorMasked() const
Check if the cursor was masked in the previous frame.
Definition CameraManager.hpp:184
void SetOriginRotation(const glm::quat &rotation)
Set the origin rotation (used for mouse dragging).
Definition CameraManager.hpp:163
std::shared_ptr< CameraMovement::Utils::ICameraBehavior > _behavior
Definition CameraManager.hpp:235
Engine::Entity GetActiveCamera() const
Get the active camera entity.
Definition CameraManager.hpp:57
void SetJoystickId(int joystickId)
Set the joystick ID to use for camera control.
Definition CameraManager.hpp:198
double GetLastMouseY() const
Get the last mouse Y position.
Definition CameraManager.hpp:142
bool HasValidCamera() const
Check if the camera entity is set and valid.
Definition CameraManager.hpp:79
Base interface for camera behaviors.
Definition CameraBehavior.hpp:18
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
bool IsAlive() const
Check if the entity is alive.
Definition Entity.cpp:3
bool HasComponents() const
Check if entity have one or multiple component's type.
Definition Entity.hpp:109
Definition CameraControlSystemManager.hpp:9
Definition CameraManager.hpp:12
Definition Camera.hpp:10
Definition Transform.hpp:18