ST_engine  0.3-ALPHA
entity.hpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #ifndef ENTITY_DEF
11 #define ENTITY_DEF
12 
13 
14 #include <cstdint>
15 #include <cstddef>
16 
17 namespace ST {
18 
20 
24  class entity {
25 
26  private:
27 
28  //4 bytes
29  int16_t col_x = 0;
30  int16_t col_y = 0;
31 
32  //4 bytes
33  int16_t offset_x = 0;
34  int16_t offset_y = 0;
35 
36  public:
37 
38  //8 bytes
39  int32_t x = 0;
40  int32_t y = 0;
41 
42  //8 bytes
43  float tex_scale_x = 1;
44  float tex_scale_y = 1;
45 
46  //4 bytes
47  uint16_t tex_w = 0;
48  uint16_t tex_h = 0;
49 
50  //2 bytes
51  uint8_t sprite_num = 1;
52  uint8_t animation = 1;
53 
54  //2 bytes
55  int8_t velocity_x = 0;
56  int8_t velocity_y = 0;
57 
58  //2 bytes
59  /*
60  [0] is_active = true;
61  [1] is_static = false; does not move with camera
62  [2] is_visible = true;
63  [3] is_affected_by_physics;
64  */
65  uint8_t toggles = 0;
66  uint8_t animation_num = 1;
67 
68  //2 bytes
69  uint16_t texture = 0xffff;
70 
71  entity() {
72  toggles |= (1U << 0U);
73  toggles &= ~(1U << 1U);
74  toggles |= (1U << 2U);
75  toggles &= ~(1U << 3U);
76  };
77 
78  [[nodiscard]] int32_t get_col_x() const;
79 
80  [[nodiscard]] int32_t get_col_y() const;
81 
82  [[nodiscard]] int16_t get_col_y_offset() const;
83 
84  [[nodiscard]] int16_t get_col_x_offset() const;
85 
86  [[nodiscard]] bool collides(const entity &) const;
87 
88  void set_collision_box(int16_t, int16_t, int16_t, int16_t);
89 
90  [[nodiscard]] bool is_active() const;
91 
92  [[nodiscard]] bool is_static() const;
93 
94  [[nodiscard]] bool is_visible() const;
95 
96  [[nodiscard]] bool is_affected_by_physics() const;
97 
98  void set_active(bool active);
99 
100  void set_static(bool static_);
101 
102  void set_visible(bool visible);
103 
104  void set_affected_by_physics(bool affected);
105  };
106 }
107 
108 //INLINED METHODS
109 
110 inline bool ST::entity::is_active() const {
111  return static_cast<bool>(toggles & (1U << 0U));
112 }
113 
114 inline bool ST::entity::is_static() const {
115  return static_cast<bool>(toggles & (1U << 1U));
116 }
117 
118 inline bool ST::entity::is_visible() const {
119  return static_cast<bool>(toggles & (1U << 2U));
120 }
121 
122 inline bool ST::entity::is_affected_by_physics() const {
123  return static_cast<bool>(toggles & (1U << 3U));
124 }
125 
126 inline void ST::entity::set_active(bool active) {
127  if (active) {
128  toggles |= (1U << 0U);
129  } else {
130  toggles &= ~(1U << 0U);
131  }
132 }
133 
134 inline void ST::entity::set_static(bool static_) {
135  if (static_) {
136  toggles |= (1U << 1U);
137  } else {
138  toggles &= ~(1U << 1U);
139  }
140 }
141 
142 inline void ST::entity::set_visible(bool visible) {
143  if (visible) {
144  toggles |= (1U << 2U);
145  } else {
146  toggles &= ~(1U << 2U);
147  }
148 }
149 
150 inline void ST::entity::set_affected_by_physics(bool affected) {
151  if (affected) {
152  toggles |= (1U << 3U);
153  } else {
154  toggles &= ~(1U << 3U);
155  }
156 }
157 
162 inline int32_t ST::entity::get_col_x() const {
163  return col_x;
164 }
165 
170 inline int32_t ST::entity::get_col_y() const {
171  return col_y;
172 }
173 
178 inline int16_t ST::entity::get_col_x_offset() const {
179  return offset_x;
180 }
181 
186 inline int16_t ST::entity::get_col_y_offset() const {
187  return offset_y;
188 }
189 
197 inline void ST::entity::set_collision_box(int16_t offsetX, int16_t offsetY, int16_t col_x_, int16_t col_y_) {
198  this->col_x = col_x_;
199  this->col_y = -col_y_;
200  offset_x = offsetX;
201  offset_y = offsetY;
202 }
203 
210 inline bool ST::entity::collides(const entity &other) const {
211  return !((y + offset_y <= other.y + other.col_y + other.offset_y) ||
212  (y + col_y + offset_y >= other.y + other.offset_y)
213  || (x + offset_x >= other.x + other.col_x + other.offset_x) ||
214  (x + col_x + offset_x <= other.x + other.offset_x));
215 }
216 
217 static_assert(sizeof(ST::entity) == 36, "class 'entity' is not sized properly, maybe you have misaligned the fields");
218 
219 #endif
This class represents all static or dynamic objects in the game (excluding text, see ST::text)
Definition: entity.hpp:24
int32_t get_col_y() const
Definition: entity.hpp:170
void set_collision_box(int16_t, int16_t, int16_t, int16_t)
Definition: entity.hpp:197
bool collides(const entity &) const
Definition: entity.hpp:210
int16_t get_col_x_offset() const
Definition: entity.hpp:178
int16_t get_col_y_offset() const
Definition: entity.hpp:186
int32_t get_col_x() const
Definition: entity.hpp:162