Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1/*
2 Wizard Engine
3 Copyright (C) 2023-2024 Zana Domán
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef WIZARD_ENGINE_ENTITY_HPP
23#define WIZARD_ENGINE_ENTITY_HPP
24
27
28namespace wze {
34class entity : public component {
35 private:
36 std::vector<std::weak_ptr<component>> _components;
37 float _x;
38 float _y;
39 float _angle;
40 std::array<float, 4> _transformation_matrix;
41 float _x_offset;
42 float _y_offset;
43 float _angle_offset;
44 bool _attach_x;
45 bool _attach_y;
46 bool _attach_angle;
47 bool _x_angle_lock;
48 bool _y_angle_lock;
49
55 void update_x(component& instance) const;
56
62 void update_y(component& instance) const;
63
69 void update_angle(component& instance) const;
70
77 template <void (entity::*... updater)(component&) const> void update() {
78 components().erase(
79 std::remove_if(
80 components().begin(), components().end(),
81 [&](std::weak_ptr<component> const& component) -> bool {
82 std::shared_ptr<class component> locked;
83
84 locked = component.lock();
85 if (locked) {
86 ((this->*updater)(*locked), ...);
87 return false;
88 }
89
90 return true;
91 }),
92 components().end());
93 }
94
95 public:
102 [[nodiscard]] virtual std::vector<std::weak_ptr<component>> const&
103 components() const;
104
111 [[nodiscard]] virtual std::vector<std::weak_ptr<component>>& components();
112
119 [[nodiscard]] virtual float x() const;
120
127 void set_x(float x) override;
128
135 [[nodiscard]] virtual float y() const;
136
143 void set_y(float y) override;
144
151 [[nodiscard]] virtual float angle() const;
152
159 void set_angle(float angle) override;
160
167 [[nodiscard]] virtual std::array<float, 4> const&
168 transformation_matrix() const;
169
176 [[nodiscard]] float x_offset() const override;
177
184 virtual void set_x_offset(float x_offset);
185
192 [[nodiscard]] float y_offset() const override;
193
200 virtual void set_y_offset(float y_offset);
201
208 [[nodiscard]] float angle_offset() const override;
209
216 virtual void set_angle_offset(float angle_offset);
217
224 [[nodiscard]] bool attach_x() const override;
225
232 virtual void set_attach_x(bool attach_x);
233
240 [[nodiscard]] bool attach_y() const override;
241
248 virtual void set_attach_y(bool attach_y);
249
256 [[nodiscard]] bool attach_angle() const override;
257
264 virtual void set_attach_angle(bool attach_angle);
265
272 [[nodiscard]] bool x_angle_lock() const override;
273
280 virtual void set_x_angle_lock(bool x_angle_lock);
281
288 [[nodiscard]] bool y_angle_lock() const override;
289
296 virtual void set_y_angle_lock(bool y_angle_lock);
297
315 explicit entity(
316 std::vector<std::weak_ptr<component>> const& components = {},
317 float x = 0, float y = 0, float angle = 0, float x_offset = 0,
318 float y_offset = 0, float angle_offset = 0, bool attach_x = true,
319 bool attach_y = true, bool attach_angle = true,
320 bool x_angle_lock = true, bool y_angle_lock = true);
321
328 virtual void recompose();
329
335 ~entity() override = default;
336};
337} /* namespace wze */
338
339#endif /* WIZARD_ENGINE_ENTITY_HPP */
Interface to make an object composable.
Export header of the Wizard Engine.
Wizard Engine.