2D_Game_Engine
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <structs.hpp>
4#include <texture.hpp>
5#include <window.hpp>
6#include <textrenderer.hpp>
7
8extern uint32_t NEXT_UID; //the next uid to be assigned
9
10class Entity;
11class TagComponent;
17class LightComponent;
19class TextComponent;
20class Scene;
21
22template<typename T>
23using ComponentType=typename std::enable_if<
24 std::is_same<T,TagComponent>::value ||
25 std::is_same<T,TextureComponent>::value ||
26 std::is_same<T,AnimatedTextureComponent>::value ||
27 std::is_same<T,RigidbodyComponent>::value ||
28 std::is_same<T,BoxColliderComponent>::value ||
29 std::is_same<T,CircleColliderComponent>::value ||
30 std::is_same<T,LightComponent>::value ||
31 std::is_same<T,NativeScriptComponent>::value ||
32 std::is_same<T,TextComponent>::value,
33 int>::type;
34
39template<typename T,ComponentType<T> = 0>
40inline int BinarySearch(std::vector<T> &v,uint32_t uid){
41 int l=0,r=v.size()-1;
42 while(l<=r){
43 int m=(l+r)/2;
44 if(v[m].m_UID==uid)
45 return m;
46 else if(v[m].m_UID<uid)
47 l=m+1;
48 else
49 r=m-1;
50 }
51 return -1;
52}
53
57template<typename T,ComponentType<T> = 0>
58inline int FirstGreaterThan(std::vector<T> &v,uint32_t uid){
59 int l=0,r=v.size()-1;
60 while(l<=r){
61 int m=(l+r)/2;
62 if(v[m].m_UID>uid)
63 r=m-1;
64 else
65 l=m+1;
66 }
67 return l;
68}
69
74template<typename T,ComponentType<T> = 0>
75inline void RightShift(std::vector<T> &v,size_t idx){
76 for(size_t i=v.size()-1;i>idx;i--)
77 v[i]=v[i-1];
78}
79
83inline float Interpolate(float current,float previous){
84 return current*Window::Alpha+previous*(1.0f-Window::Alpha);
85}
86
88public:
89 TagComponent(const std::string &tag,uint32_t uid): m_Tag(tag),m_UID(uid){
90 m_Tag.resize(64);
91 }
92 TagComponent(): m_UID(std::numeric_limits<uint32_t>::max()){
93 m_Tag.resize(64);
94 }
97
99 m_Tag=b.m_Tag;
100 m_UID=b.m_UID;
101 return *this;
102 }
104 if(this!=&b){
105 m_Tag=std::move(b.m_Tag);
106 m_UID=b.m_UID;
107 }
108 return *this;
109 }
110
111 std::string m_Tag;
112 uint32_t m_UID;
113};
114
116public:
117 TextureComponent(const std::string &path,int mag_filter,int min_filter,float width,float height,int layer,uint32_t uid);
118 TextureComponent(std::shared_ptr<Texture>t,float width,float height,int layer,uint32_t uid);
119 TextureComponent(): m_Texture(std::make_shared<Texture>()),m_Width(0),m_Height(0),m_Layer(0),m_UID(std::numeric_limits<uint32_t>::max()){}
120 TextureComponent(uint32_t uid): m_Texture(std::make_shared<Texture>()),m_Width(0),m_Height(0),m_Layer(0),m_UID(uid){}
123
129 m_UID=b.m_UID;
130 return *this;
131 }
133 if(this!=&b){
134 m_Texture=std::move(b.m_Texture);
135 m_Width=b.m_Width;
136 m_Height=b.m_Height;
137 m_Layer=b.m_Layer;
138 m_UID=b.m_UID;
139 }
140 return *this;
141 }
142
143 std::shared_ptr<Texture> m_Texture;
146 uint32_t m_UID;
147};
148
150public:
151 AnimatedTextureComponent(const std::string &path,unsigned int tile_width,unsigned int tile_height,int mag_filter,int min_filter,float width,float height,int layer,bool play_animation,bool loop_animation,float animation_delay,int animation_index,uint32_t uid);
152 AnimatedTextureComponent(std::shared_ptr<SpriteSheet>t,float width,float height,int layer,bool play_animation,bool loop_animation,float animation_delay,int animation_index,uint32_t uid);
157
172 if(this!=&b){
173 m_AnimatedTexture=std::move(b.m_AnimatedTexture);
174 m_Width=b.m_Width;
175 m_Height=b.m_Height;
176 m_Layer=b.m_Layer;
177 m_PlayAnimation=b.m_PlayAnimation;
178 m_LoopAnimation=b.m_LoopAnimation;
179 m_AnimationDelay=b.m_AnimationDelay;
180 m_AnimationIndex=b.m_AnimationIndex;
181 m_LastAnimationTime=b.m_LastAnimationTime;
182 m_UID=b.m_UID;
183 }
184 return *this;
185 }
186
187 void PlayAnimation(bool loop=false,float delay=0.0f);
188
189 std::shared_ptr<SpriteSheet> m_AnimatedTexture;
196 uint32_t m_UID;
197};
198
200public:
201 enum class BodyType{
202 Static,
203 Dynamic,
205 };
206
207 RigidbodyComponent(){ m_UID=std::numeric_limits<uint32_t>::max(); }
208 RigidbodyComponent(uint32_t uid): m_UID(uid){}
209 RigidbodyComponent(BodyType bodyType,bool fixedRotation,uint32_t uid): m_BodyType(bodyType),m_FixedRotation(fixedRotation),m_UID(uid){}
210
213 b2Body *m_RuntimeBody=nullptr;
214
215 uint32_t m_UID;
216};
217
218std::ostream &operator<<(std::ostream &os,const RigidbodyComponent::BodyType &type);
219
221public:
222 BoxColliderComponent(){ m_UID=std::numeric_limits<uint32_t>::max(); }
223 BoxColliderComponent(uint32_t uid): m_UID(uid){}
224 BoxColliderComponent(float x_offset,float y_offset,float width,float height,float density,float friction,
225 float restitution,float restitution_threshold,uint32_t uid): m_XOffset(x_offset),m_YOffset(y_offset),m_Width(width),m_Height(height),
226 m_Density(density),m_Friction(friction),m_Restitution(restitution),m_RestitutionThreshold(restitution_threshold),m_UID(uid){}
227
228 float m_XOffset=0.0f,m_YOffset=0.0f;
229 float m_Width=0.0f,m_Height=0.0f;
230
231 float m_Density=1.0f;
232 float m_Friction=0.0f;
233 float m_Restitution=0.0f;
235
236 b2Fixture *m_RuntimeFixture=nullptr;
237
238 uint32_t m_UID;
239};
240
242public:
243 CircleColliderComponent(){ m_UID=std::numeric_limits<uint32_t>::max(); }
244 CircleColliderComponent(uint32_t uid): m_UID(uid){}
245 CircleColliderComponent(float x_offset,float y_offset,float radius,float density,float friction,
246 float restitution,float restitution_threshold,uint32_t uid): m_XOffset(x_offset),m_YOffset(y_offset),m_Radius(radius),
247 m_Density(density),m_Friction(friction),m_Restitution(restitution),m_RestitutionThreshold(restitution_threshold),m_UID(uid){}
248
249 float m_XOffset=0.0f,m_YOffset=0.0f;
250 float m_Radius=0.0f;
251
252 float m_Density=1.0f;
253 float m_Friction=0.0f;
254 float m_Restitution=0.0f;
256
257 b2Fixture *m_RuntimeFixture=nullptr;
258
259 uint32_t m_UID;
260};
261
263public:
265 LightComponent(float x_offset,float y_offset,float radius,float blur,Vec3 color,LightType type,uint32_t uid);
266 LightComponent(uint32_t uid);
267
268 void SetOffset(float x_offset,float y_offset);
269 void SetCentered(float width,float height);
270
275
276 uint32_t m_UID;
277};
278
280public:
281 NativeScriptComponent(): m_UID(std::numeric_limits<uint32_t>::max()){}
282 NativeScriptComponent(uint32_t uid): m_UID(uid){}
283
284 std::function<void(Scene*,NativeScriptComponent*)> OnCreate;
285 std::function<void(Scene*,NativeScriptComponent*,float)> OnUpdate;
286 std::function<void(Scene*,NativeScriptComponent*)> OnDestroy;
287
288 uint32_t m_UID;
289};
290
292public:
293 TextComponent(): m_TextRenderer(std::make_shared<TextRenderer>()),m_Text(100,'\0'),m_Offset(0.0f,0.0f),m_Color(1.0f,1.0f,1.0f),m_Scale(1.0f),m_Layer(0),m_IgnoreLighting(false),m_UID(std::numeric_limits<uint32_t>::max()){}
294 TextComponent(uint32_t uid): m_TextRenderer(std::make_shared<TextRenderer>()),m_Text(100,'\0'),m_Offset(0.0f,0.0f),m_Color(1.0f,1.0f,1.0f),m_Scale(1.0f),m_Layer(0),m_IgnoreLighting(false),m_UID(uid){}
295 TextComponent(std::shared_ptr<TextRenderer> textRenderer,const std::string &text,Vec2 offset,Vec3 color,float scale,uint32_t uid):
296 m_TextRenderer(textRenderer),m_Text(text),m_Offset(offset),m_Color(color),m_Scale(scale),m_Layer(0),m_IgnoreLighting(false),m_UID(uid){
297 m_Text.resize(100);
298 }
299 TextComponent(const std::string &path,float glyph_size,bool fixed,uint32_t uid): m_TextRenderer(FONT_MANAGER->GetFont(path,glyph_size,fixed).second),m_Layer(0),m_IgnoreLighting(false),m_UID(uid){}
300 TextComponent(const std::string &path,float glyph_size,bool fixed,const std::string &text,Vec2 offset,Vec3 color,float scale,int layer,bool ignore_lighting,uint32_t uid):
301 m_TextRenderer(FONT_MANAGER->GetFont(path,glyph_size,fixed).second),m_Text(text),m_Offset(offset),m_Color(color),m_Scale(scale),m_Layer(layer),m_IgnoreLighting(ignore_lighting),m_UID(uid){
302 m_Text.resize(100);
303 }
304
305 TextComponent(const TextComponent &other);
309
310 void SetCentered(float width,float height);
311
312 std::shared_ptr<TextRenderer> m_TextRenderer;
313 std::string m_Text;
316 float m_Scale;
319 uint32_t m_UID;
320};
321
322class Entity{
323public:
324 Entity();
325 Entity(float x,float y);
326 Entity(uint32_t uid);
327 Entity(const Entity &other);
328 Entity(Entity &&other);
329 Entity &operator=(const Entity &other);
330 Entity &operator=(Entity &&other);
331
332 uint32_t m_UID;
333 float m_X,m_Y;
335};
336
341inline Entity* BinarySearch(std::vector<Entity> &v,uint32_t uid){
342 int l=0,r=v.size()-1;
343 while(l<=r){
344 int m=(l+r)/2;
345 if(v[m].m_UID==uid)
346 return &v[m];
347 else if(v[m].m_UID<uid)
348 l=m+1;
349 else
350 r=m-1;
351 }
352 return nullptr;
353}
354
355template<typename T,ComponentType<T> = 0>
357public:
358 std::vector<T> m_Components;
359
363 void AddComponent(T &component,uint32_t uid){
364 int idx=FirstGreaterThan(m_Components,uid);
365 m_Components.resize(m_Components.size()+1);
367 m_Components[idx]=component;
368 }
369
373 void RemoveComponent(uint32_t uid){
374 int idx;
375 if((idx=BinarySearch(m_Components,uid))!=-1){
376 if constexpr(std::is_same<T,RigidbodyComponent>::value)
377 m_Components[idx].m_RuntimeBody->GetWorld()->DestroyBody(m_Components[idx].m_RuntimeBody);
378 if constexpr(std::is_same<T,BoxColliderComponent>::value)
379 m_Components[idx].m_RuntimeFixture->GetBody()->DestroyFixture(m_Components[idx].m_RuntimeFixture);
380 if constexpr(std::is_same<T,CircleColliderComponent>::value)
381 m_Components[idx].m_RuntimeFixture->GetBody()->DestroyFixture(m_Components[idx].m_RuntimeFixture);
382 m_Components.erase(m_Components.begin()+idx);
383 }
384 }
385
389 T* GetComponent(uint32_t uid){
390 int idx;
391 if((idx=BinarySearch(m_Components,uid))!=-1)
392 return &m_Components[idx];
393 else
394 return nullptr;
395 }
396
400 std::vector<T> &GetComponents(){
401 return m_Components;
402 }
403
408 Rect *r=new Rect;
409 r->pos.x=m_Components[index].m_XOffset; //entity position is added later
410 r->pos.y=m_Components[index].m_YOffset;
411 r->size.x=m_Components[index].m_Width;
412 r->size.y=m_Components[index].m_Height;
413 r->vel.x=m_Components[index].m_HSpeed;
414 r->vel.y=m_Components[index].m_VSpeed;
415 return r;
416 }
417
421 void Update(Scene *scene,float frame_time);
425 void Render(Scene *scene);
426};
427
432 switch (bodyType){
434 return b2_staticBody;
436 return b2_dynamicBody;
438 return b2_kinematicBody;
439 }
440 return b2_staticBody;
441}
442
447 switch (bodyType){
448 case b2_staticBody:
450 case b2_dynamicBody:
452 case b2_kinematicBody:
454 }
455 printf("Unknown body type\n");
457}
AnimatedTextureComponent & operator=(AnimatedTextureComponent &b)
Definition entity.hpp:158
AnimatedTextureComponent(uint32_t uid)
Definition entity.hpp:154
void PlayAnimation(bool loop=false, float delay=0.0f)
Definition entity.cpp:123
std::shared_ptr< SpriteSheet > m_AnimatedTexture
Definition entity.hpp:189
AnimatedTextureComponent & operator=(AnimatedTextureComponent &&b)
Definition entity.hpp:171
BoxColliderComponent(float x_offset, float y_offset, float width, float height, float density, float friction, float restitution, float restitution_threshold, uint32_t uid)
Definition entity.hpp:224
float m_RestitutionThreshold
Definition entity.hpp:234
b2Fixture * m_RuntimeFixture
Definition entity.hpp:236
BoxColliderComponent(uint32_t uid)
Definition entity.hpp:223
b2Fixture * m_RuntimeFixture
Definition entity.hpp:257
CircleColliderComponent(uint32_t uid)
Definition entity.hpp:244
CircleColliderComponent(float x_offset, float y_offset, float radius, float density, float friction, float restitution, float restitution_threshold, uint32_t uid)
Definition entity.hpp:245
T * GetComponent(uint32_t uid)
Definition entity.hpp:389
void Render(Scene *scene)
void AddComponent(T &component, uint32_t uid)
Definition entity.hpp:363
Rect * GetComponentAsRect(int index)
Definition entity.hpp:407
std::vector< T > m_Components
Definition entity.hpp:358
void RemoveComponent(uint32_t uid)
Definition entity.hpp:373
void Update(Scene *scene, float frame_time)
std::vector< T > & GetComponents()
Definition entity.hpp:400
float m_PreviousX
Definition entity.hpp:334
Entity & operator=(const Entity &other)
Definition entity.cpp:30
float m_X
Definition entity.hpp:333
uint32_t m_UID
Definition entity.hpp:332
Entity()
Definition entity.cpp:10
float m_Y
Definition entity.hpp:333
float m_PreviousY
Definition entity.hpp:334
float m_XOffset
Definition entity.hpp:271
void SetOffset(float x_offset, float y_offset)
Definition entity.cpp:135
void SetCentered(float width, float height)
Definition entity.cpp:140
uint32_t m_UID
Definition entity.hpp:276
LightType m_Type
Definition entity.hpp:274
float m_YOffset
Definition entity.hpp:271
std::function< void(Scene *, NativeScriptComponent *) OnDestroy)
Definition entity.hpp:286
NativeScriptComponent(uint32_t uid)
Definition entity.hpp:282
std::function< void(Scene *, NativeScriptComponent *, float) OnUpdate)
Definition entity.hpp:285
std::function< void(Scene *, NativeScriptComponent *) OnCreate)
Definition entity.hpp:284
RigidbodyComponent(BodyType bodyType, bool fixedRotation, uint32_t uid)
Definition entity.hpp:209
b2Body * m_RuntimeBody
Definition entity.hpp:213
BodyType m_BodyType
Definition entity.hpp:211
RigidbodyComponent(uint32_t uid)
Definition entity.hpp:208
Definition scene.hpp:6
std::string m_Tag
Definition entity.hpp:111
TagComponent & operator=(TagComponent &&b)
Definition entity.hpp:103
uint32_t m_UID
Definition entity.hpp:112
TagComponent & operator=(TagComponent &b)
Definition entity.hpp:98
TagComponent(const std::string &tag, uint32_t uid)
Definition entity.hpp:89
float m_Scale
Definition entity.hpp:316
TextComponent(std::shared_ptr< TextRenderer > textRenderer, const std::string &text, Vec2 offset, Vec3 color, float scale, uint32_t uid)
Definition entity.hpp:295
TextComponent(const std::string &path, float glyph_size, bool fixed, const std::string &text, Vec2 offset, Vec3 color, float scale, int layer, bool ignore_lighting, uint32_t uid)
Definition entity.hpp:300
TextComponent & operator=(const TextComponent &other)
Definition entity.cpp:167
bool m_IgnoreLighting
Definition entity.hpp:318
std::shared_ptr< TextRenderer > m_TextRenderer
Definition entity.hpp:312
std::string m_Text
Definition entity.hpp:313
uint32_t m_UID
Definition entity.hpp:319
TextComponent(uint32_t uid)
Definition entity.hpp:294
TextComponent(const std::string &path, float glyph_size, bool fixed, uint32_t uid)
Definition entity.hpp:299
void SetCentered(float width, float height)
Definition entity.cpp:191
TextureComponent & operator=(TextureComponent &&b)
Definition entity.hpp:132
TextureComponent & operator=(TextureComponent &b)
Definition entity.hpp:124
std::shared_ptr< Texture > m_Texture
Definition entity.hpp:143
TextureComponent(uint32_t uid)
Definition entity.hpp:120
uint32_t m_UID
Definition entity.hpp:146
int FirstGreaterThan(std::vector< T > &v, uint32_t uid)
Definition entity.hpp:58
RigidbodyComponent::BodyType RigidbodyTypeFromBox2DBody(b2BodyType bodyType)
Definition entity.hpp:446
int BinarySearch(std::vector< T > &v, uint32_t uid)
Definition entity.hpp:40
float Interpolate(float current, float previous)
Definition entity.hpp:83
void RightShift(std::vector< T > &v, size_t idx)
Definition entity.hpp:75
uint32_t NEXT_UID
typename std::enable_if< std::is_same< T, TagComponent >::value|| std::is_same< T, TextureComponent >::value|| std::is_same< T, AnimatedTextureComponent >::value|| std::is_same< T, RigidbodyComponent >::value|| std::is_same< T, BoxColliderComponent >::value|| std::is_same< T, CircleColliderComponent >::value|| std::is_same< T, LightComponent >::value|| std::is_same< T, NativeScriptComponent >::value|| std::is_same< T, TextComponent >::value, int >::type ComponentType
Definition entity.hpp:23
b2BodyType RigidbodyTypeToBox2DBody(RigidbodyComponent::BodyType bodyType)
Definition entity.hpp:431
std::ostream & operator<<(std::ostream &os, const RigidbodyComponent::BodyType &type)
Definition entity.cpp:198
LightType
Definition global.hpp:7
float Alpha
Definition window.cpp:235
int i
Vec2 size
Definition structs.hpp:76
Vec2 pos
Definition structs.hpp:75
Vec2 vel
Definition structs.hpp:77
float y
Definition structs.hpp:9
float x
Definition structs.hpp:5
FontManager * FONT_MANAGER
The font manager.
Definition window.cpp:16