GPR5100 - Rollback
Loading...
Searching...
No Matches
physics_manager.h
1#pragma once
2#include "game_globals.h"
3#include "engine/component.h"
4#include "engine/entity.h"
5#include "maths/angle.h"
6#include "maths/vec2.h"
7
8#include <SFML/System/Time.hpp>
9
10#include "utils/action_utility.h"
11
12namespace game
13{
14enum class BodyType
15{
16 DYNAMIC,
17 STATIC
18};
19
23struct Body
24{
25 core::Vec2f position = core::Vec2f::zero();
26 core::Vec2f velocity = core::Vec2f::zero();
27 core::Degree angularVelocity = core::Degree(0.0f);
28 core::Degree rotation = core::Degree(0.0f);
29 BodyType bodyType = BodyType::DYNAMIC;
30};
31
35struct Box
36{
37 core::Vec2f extends = core::Vec2f::one();
38 bool isTrigger = false;
39};
40
46{
47public:
48 virtual ~OnTriggerInterface() = default;
49 virtual void OnTrigger(core::Entity entity1, core::Entity entity2) = 0;
50};
51
55class BodyManager : public core::ComponentManager<Body, static_cast<core::EntityMask>(core::ComponentType::BODY2D)>
56{
57public:
58 using ComponentManager::ComponentManager;
59};
60
64class BoxManager : public core::ComponentManager<Box, static_cast<core::EntityMask>(core::ComponentType::BOX_COLLIDER2D)>
65{
66public:
67 using ComponentManager::ComponentManager;
68};
69
75{
76public:
77 explicit PhysicsManager(core::EntityManager& entityManager);
78 void FixedUpdate(sf::Time dt);
79 [[nodiscard]] const Body& GetBody(core::Entity entity) const;
80 void SetBody(core::Entity entity, const Body& body);
81 void AddBody(core::Entity entity);
82
83 void AddBox(core::Entity entity);
84 void SetBox(core::Entity entity, const Box& box);
85 [[nodiscard]] const Box& GetBox(core::Entity entity) const;
90 void RegisterTriggerListener(OnTriggerInterface& onTriggerInterface);
91 void CopyAllComponents(const PhysicsManager& physicsManager);
92private:
93 core::EntityManager& entityManager_;
94 BodyManager bodyManager_;
95 BoxManager boxManager_;
97};
98
99}
Action is an utility class loosely based on the observer pattern and close to C# Action class.
Definition: action_utility.h:36
ComponentManager is a class that owns Component in a contiguous array. Component indexing is done wit...
Definition: component.h:33
Degree is an utility class that describes degree angles (0 to 360). It can be easily converted to Rad...
Definition: angle.h:56
Manages the entities in an array using bitwise operations to know if it has components.
Definition: entity.h:35
BodyManager is a ComponentManager that holds all the Body in the world.
Definition: physics_manager.h:56
BoxManager is a ComponentManager that holds all the Box in the world.
Definition: physics_manager.h:65
OnTriggerInterface is an interface for classes that needs to be called when two boxes are in contact....
Definition: physics_manager.h:46
PhysicsManager is a class that holds both BodyManager and BoxManager and manages the physics fixed up...
Definition: physics_manager.h:75
void RegisterTriggerListener(OnTriggerInterface &onTriggerInterface)
RegisterTriggerListener is a method that stores an OnTriggerInterface in the PhysicsManager that will...
Definition: physics_manager.cpp:104
std::uint32_t Entity
Entity is the type used to define an game world entity. An Entity is just an index,...
Definition: entity.h:17
Vec2f is a utility class that represents a mathematical 2d vector.
Definition: vec2.h:12
Body is a class that represents a rigid body.
Definition: physics_manager.h:24
Box is a class that represents an axis-aligned box collider.
Definition: physics_manager.h:36