-
Notifications
You must be signed in to change notification settings - Fork 4
/
Objects.hpp
106 lines (85 loc) · 4.67 KB
/
Objects.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef TNT_DOO_ECS_BASE_OBJECTS_SYSTEM_HPP
#define TNT_DOO_ECS_BASE_OBJECTS_SYSTEM_HPP
#include <nlohmann/json_fwd.hpp>
#include "core/Window.hpp"
#include "doo_ecs/Base.hpp"
#include "math/Vector.hpp"
#include "types/SparseSet.hpp"
namespace tnt::doo
{
/// @brief A struct that holds the basic data of an object.
struct object_data final
{
/// @brief Create a new object data handle from the given params.
/// @param angle_ The initial angle of the object.
/// @param scale_ The initial scale of the object.
/// @param pos_ The initial position of the object.
/// @param parent_ The parent of the new object. Leave it as is if you don't want the object to have a parent.
constexpr object_data(float angle_, Vector const &pos_, Vector const &scale_,
object const &parent_ = null) noexcept
: angle{angle_}, pos{pos_}, scale{scale_}, parent{parent_} {}
float angle; /// < The angle of the object.
Vector scale; /// < The scale of the object.
Vector pos; /// < The position of the object.
object parent; /// < The parent of the object.
};
/// @brief A struct that handles objects data.
inline struct objects_sys final
{
// objects_sys() = default;
// objects_sys(objects_sys const &) = delete;
// objects_sys &operator=(objects_sys const &) = delete;
/// @brief Create a new object in place and add it to the system.
/// Return the id of the newly created @c object.
/// @return tnt::doo::object
object add_object(object_data const &data_) noexcept;
/// @brief Get the data of the object with the given id.
/// @param id The id of the object.
/// @return object_data
[[nodiscard]] object_data get_data(object const &id) const noexcept;
/// @brief Load objects data from a json chunk.
/// @param j The json chunk that contains the objects data.
/// @return The id of the created object.
object from_json(nlohmann::json const &j);
/// @brief Store general data of a specific object to a json chunk.
/// @param id The id of the object to serialize to json.
/// @param j The json chunk where the data will be saved.
void to_json(object const &id, nlohmann::json &j) ;
/// @brief Draw widgets on the given window to modify the datas of the system.
/// @param id The id of the active object.
/// @param win The window where to draw the widgets.
void draw_imgui(object const &id, Window const &win) noexcept;
/// @brief Get the angle of the object in global context.
/// @param id The id of the object.
/// @return float
/// @note The function returns angle[id] if the object has no parent (aka. parent[id] == -1).
[[nodiscard]] float gAngle(object const &id) const noexcept;
/// @brief Get the scale of the object in global context.
/// @param id The id of the object.
/// @return Vector
/// @note The function returns scale[id] if the object has no parent (aka. parent[id] == -1).
[[nodiscard]] Vector gScale(object const &id) const noexcept;
/// @brief Get the position of the object in global context.
/// @param id The id of the object.
/// @return Vector
/// @note The function returns pos[id] if the object has no parent (aka. parent[id] == -1).
[[nodiscard]] Vector gPos(object const &id) const noexcept;
/// @brief Change the parent of the given object and apply all the necessary changes.
/// @param id The id of the child object.
/// @param parent_ The id of the new parent.
void set_parent(object const &id, object const &parent_) noexcept;
/// @brief Remove the desired object from the objects system.
/// @param id The id of the object you want to remove.
/// @note Please call remove() from the other systems first.
void remove(object const &id) noexcept;
/// @brief Remove all the objects from the objects system.
/// @note Please call clear() from the other systems first.
void clear() noexcept;
std::vector<float> angle; /// < The angles of the objects.
tnt::sparse_set<object> active; /// < The id-s of all the active objects.
std::vector<object> parent; /// < The id-s of the parents of the objects.
std::vector<Vector> scale; /// < The scales of the objects.
std::vector<Vector> pos; /// < The positions of the objects.
} objects; /// < An instance of objects_sys.
} // namespace tnt::doo
#endif //!TNT_DOO_ECS_BASE_OBJECTS_SYSTEM_HPP