Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
entity.cpp
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// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
23#define __WIZARD_ENGINE_INTERNAL__
24
27
28void wze::entity::update_x(component& instance) const {
29 if (!instance.attach_x()) {
30 return;
31 }
32 if (instance.x_angle_lock()) {
33 instance.set_x(x() + math::transform_x(instance.x_offset(),
34 instance.y_offset(),
35 transformation_matrix()));
36 } else {
37 instance.set_x(x() + instance.x_offset());
38 }
39}
40
41void wze::entity::update_y(component& instance) const {
42 if (!instance.attach_y()) {
43 return;
44 }
45 if (instance.y_angle_lock()) {
46 instance.set_y(y() + math::transform_y(instance.x_offset(),
47 instance.y_offset(),
48 transformation_matrix()));
49 } else {
50 instance.set_y(y() + instance.y_offset());
51 }
52}
53
54void wze::entity::update_angle(component& instance) const {
55 if (instance.attach_angle()) {
56 instance.set_angle(angle() + instance.angle_offset());
57 }
58}
59
60std::vector<std::weak_ptr<wze::component>> const&
61wze::entity::components() const {
62 return _components;
63}
64
65std::vector<std::weak_ptr<wze::component>>& wze::entity::components() {
66 return _components;
67}
68
69float wze::entity::x() const {
70 return _x;
71}
72
73void wze::entity::set_x(float x) {
74 _x = x;
75 update<&entity::update_x>();
76}
77
78float wze::entity::y() const {
79 return _y;
80}
81
82void wze::entity::set_y(float y) {
83 _y = y;
84 update<&entity::update_y>();
85}
86
87float wze::entity::angle() const {
88 return _angle;
89}
90
91void wze::entity::set_angle(float angle) {
92 _angle = angle;
93 _transformation_matrix = math::transformation_matrix(this->angle(), 1);
94 update<&entity::update_x, &entity::update_y, &entity::update_angle>();
95}
96
97std::array<float, 4> const& wze::entity::transformation_matrix() const {
98 return _transformation_matrix;
99}
100
101float wze::entity::x_offset() const {
102 return _x_offset;
103}
104
105void wze::entity::set_x_offset(float x_offset) {
106 _x_offset = x_offset;
107}
108
109float wze::entity::y_offset() const {
110 return _y_offset;
111}
112
113void wze::entity::set_y_offset(float y_offset) {
114 _y_offset = y_offset;
115}
116
117float wze::entity::angle_offset() const {
118 return _angle_offset;
119}
120
121void wze::entity::set_angle_offset(float angle_offset) {
122 _angle_offset = angle_offset;
123}
124
125bool wze::entity::attach_x() const {
126 return _attach_x;
127}
128
129void wze::entity::set_attach_x(bool attach_x) {
130 _attach_x = attach_x;
131}
132
133bool wze::entity::attach_y() const {
134 return _attach_y;
135}
136
137void wze::entity::set_attach_y(bool attach_y) {
138 _attach_y = attach_y;
139}
140
141bool wze::entity::attach_angle() const {
142 return _attach_angle;
143}
144
145void wze::entity::set_attach_angle(bool attach_angle) {
146 _attach_angle = attach_angle;
147}
148
149bool wze::entity::x_angle_lock() const {
150 return _x_angle_lock;
151}
152
153void wze::entity::set_x_angle_lock(bool x_angle_lock) {
154 _x_angle_lock = x_angle_lock;
155}
156
157bool wze::entity::y_angle_lock() const {
158 return _y_angle_lock;
159}
160
161void wze::entity::set_y_angle_lock(bool y_angle_lock) {
162 _y_angle_lock = y_angle_lock;
163}
164
165wze::entity::entity(std::vector<std::weak_ptr<component>> const& components,
166 float x, float y, float angle, float x_offset,
167 float y_offset, float angle_offset, bool attach_x,
168 bool attach_y, bool attach_angle, bool x_angle_lock,
169 bool y_angle_lock) {
170 this->components() = components;
171 set_x(x);
172 set_y(y);
173 set_angle(angle);
174 set_x_offset(x_offset);
175 set_y_offset(y_offset);
176 set_angle_offset(angle_offset);
177 set_attach_x(attach_x);
178 set_attach_y(attach_y);
179 set_attach_angle(attach_angle);
180 set_x_angle_lock(x_angle_lock);
181 set_y_angle_lock(y_angle_lock);
182}
183
184void wze::entity::recompose() {
185 update<&entity::update_x, &entity::update_y, &entity::update_angle>();
186}
static constexpr float transform_y(float x, float y, std::array< float, 4 > const &transformation_matrix)
Transforms the y component of a vector.
Definition math.hpp:162
static std::array< float, 4 > transformation_matrix(float angle, float scale)
Creates a transformation matrix.
Definition math.cpp:57
static constexpr float transform_x(float x, float y, std::array< float, 4 > const &transformation_matrix)
Transforms the x component of a vector.
Definition math.hpp:148
Composes composable objects.
Math modul.