2D_Game_Engine
Loading...
Searching...
No Matches
entity.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <entity.hpp>
3#include <renderer.hpp>
4#include <application.hpp>
5#include <global.hpp>
6#include <scene.hpp>
7
8uint32_t NEXT_UID=0;
9
10Entity::Entity(): m_X(0.0f),m_Y(0.0f),m_PreviousX(0.0f),m_PreviousY(0.0f),m_UID(NEXT_UID++){}
11Entity::Entity(float x,float y): m_X(x),m_Y(y),m_PreviousX(x),m_PreviousY(y),m_UID(NEXT_UID++){}
12Entity::Entity(uint32_t uid): m_X(0.0f),m_Y(0.0f),m_PreviousX(0.0f),m_PreviousY(0.0f),m_UID(uid){}
13
14Entity::Entity(const Entity &other){
15 m_X=other.m_X;
16 m_Y=other.m_Y;
19 m_UID=other.m_UID;
20}
21
23 m_X=other.m_X;
24 m_Y=other.m_Y;
25 m_PreviousX=other.m_PreviousX;
26 m_PreviousY=other.m_PreviousY;
27 m_UID=other.m_UID;
28}
29
31 m_X=other.m_X;
32 m_Y=other.m_Y;
35 m_UID=other.m_UID;
36 return *this;
37}
38
40 m_X=other.m_X;
41 m_Y=other.m_Y;
42 m_PreviousX=other.m_PreviousX;
43 m_PreviousY=other.m_PreviousY;
44 m_UID=other.m_UID;
45 return *this;
46}
47
52
54 m_Tag=other.m_Tag;
55 m_UID=other.m_UID;
56}
57
58TextureComponent::TextureComponent(const std::string &path,int mag_filter,int min_filter,float width,float height,int layer,uint32_t uid):
59 m_Width(width),m_Height(height),m_Layer(layer),m_UID(uid){
60
61 m_Texture=TEXTURES_MANAGER->GetTexture(path,mag_filter,min_filter).second;
62}
63
64TextureComponent::TextureComponent(std::shared_ptr<Texture>t,float width,float height,int layer,uint32_t uid):
65 m_Texture(t),m_Width(width),m_Height(height),m_Layer(layer),m_UID(uid){}
66
67AnimatedTextureComponent::AnimatedTextureComponent(const std::string &path,unsigned int tile_width,
68 unsigned int tile_height,int mag_filter,int min_filter,float width,float height,int layer,bool play_animation,
69 bool loop_animation,float animation_delay,int animation_index,uint32_t uid):
70
71 m_Width(width),m_Height(height),m_Layer(layer),m_PlayAnimation(play_animation),
72 m_LoopAnimation(loop_animation),m_AnimationDelay(animation_delay),m_AnimationIndex(animation_index),m_LastAnimationTime(0),m_UID(uid){
73
74 m_AnimatedTexture=TEXTURES_MANAGER->GetSpriteSheet(path,tile_width,tile_height,mag_filter,min_filter).second;
75}
76
77AnimatedTextureComponent::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):
78 m_AnimatedTexture(t),m_Width(width),m_Height(height),m_Layer(layer),m_PlayAnimation(play_animation),
79 m_LoopAnimation(loop_animation),m_AnimationDelay(animation_delay),m_AnimationIndex(animation_index),m_LastAnimationTime(0),m_UID(uid){}
80
88
90 m_Texture=other.m_Texture;
91 m_Width=other.m_Width;
92 m_Height=other.m_Height;
93 m_Layer=other.m_Layer;
94 m_UID=other.m_UID;
95}
96
109
111 m_AnimatedTexture=other.m_AnimatedTexture;
112 m_Width=other.m_Width;
113 m_Height=other.m_Height;
114 m_Layer=other.m_Layer;
115 m_PlayAnimation=other.m_PlayAnimation;
116 m_LoopAnimation=other.m_LoopAnimation;
117 m_AnimationDelay=other.m_AnimationDelay;
118 m_AnimationIndex=other.m_AnimationIndex;
119 m_LastAnimationTime=other.m_LastAnimationTime;
120 m_UID=other.m_UID;
121}
122
123void AnimatedTextureComponent::PlayAnimation(bool loop,float delay){
124 m_LoopAnimation=loop;
125 m_AnimationDelay=delay;
126 m_PlayAnimation=true;
128}
129
130LightComponent::LightComponent(): m_XOffset(0.0f),m_YOffset(0.0f),m_Radius(0.0f),m_Blur(0.0f),m_Color(0.0f,0.0f,0.0f),m_Type(LIGHT_AROUND_POS),m_UID(std::numeric_limits<uint32_t>::max()){}
131LightComponent::LightComponent(float x_offset,float y_offset,float radius,float blur,Vec3 color,LightType type,uint32_t uid):
132 m_XOffset(x_offset),m_YOffset(y_offset),m_Radius(radius),m_Blur(blur),m_Color(color),m_Type(type),m_UID(uid){}
133LightComponent::LightComponent(uint32_t uid): m_XOffset(0.0f),m_YOffset(0.0f),m_Radius(0.0f),m_Blur(0.0f),m_Color(0.0f,0.0f,0.0f),m_Type(LIGHT_AROUND_POS),m_UID(uid){}
134
135void LightComponent::SetOffset(float x_offset,float y_offset){
136 m_XOffset=x_offset;
137 m_YOffset=y_offset;
138}
139
140void LightComponent::SetCentered(float width,float height){
141 m_XOffset=width/2;
142 m_YOffset=height/2;
143}
144
147 m_Text=other.m_Text;
148 m_Offset=other.m_Offset;
149 m_Color=other.m_Color;
150 m_Scale=other.m_Scale;
151 m_Layer=other.m_Layer;
153 m_UID=other.m_UID;
154}
155
157 m_TextRenderer=other.m_TextRenderer;
158 m_Text=other.m_Text;
159 m_Offset=other.m_Offset;
160 m_Color=other.m_Color;
161 m_Scale=other.m_Scale;
162 m_Layer=other.m_Layer;
163 m_IgnoreLighting=other.m_IgnoreLighting;
164 m_UID=other.m_UID;
165}
166
169 m_Text=other.m_Text;
170 m_Offset=other.m_Offset;
171 m_Color=other.m_Color;
172 m_Scale=other.m_Scale;
173 m_Layer=other.m_Layer;
175 m_UID=other.m_UID;
176 return *this;
177}
178
181 m_Text=other.m_Text;
182 m_Offset=other.m_Offset;
183 m_Color=other.m_Color;
184 m_Scale=other.m_Scale;
185 m_Layer=other.m_Layer;
186 m_IgnoreLighting=other.m_IgnoreLighting;
187 m_UID=other.m_UID;
188 return *this;
189}
190
191void TextComponent::SetCentered(float width,float height){
192 auto [t_width,t_height]=m_TextRenderer->GetTextSize(m_Text,m_Scale);
193
194 m_Offset.x=(width-t_width)/2;
195 m_Offset.y=(height-t_height)/2;
196}
197
198std::ostream &operator<<(std::ostream &os,const RigidbodyComponent::BodyType &type){
199 switch(type){
201 os<<"Static";
202 break;
204 os<<"Dynamic";
205 break;
207 os<<"Kinematic";
208 break;
209 }
210 return os;
211}
212
213template<>
214void ComponentManager<TextureComponent>::Update(Scene *scene,float frame_time){
215 printf("Texture components don't need to be updated!\n");
216}
217
218template<>
220 printf("Animated texture components don't need to be updated!\n");
221}
222
223template<>
224void ComponentManager<LightComponent>::Update(Scene *scene,float frame_time){
225 printf("Light components don't need to be updated!\n");
226}
227
228template<>
229void ComponentManager<NativeScriptComponent>::Update(Scene *scene,float frame_time){
230 PROFILE_FUNCTION();
231
232 for(int i=0;i<m_Components.size();i++){
233 if(m_Components[i].OnUpdate){
234 m_Components[i].OnUpdate(scene,&m_Components[i],frame_time);
235 }
236 }
237}
238
239template<>
241 PROFILE_FUNCTION();
242
243 Entity *entity=nullptr;
244 for(int i=0;i<m_Components.size();i++){
245 entity=scene->GetEntity(m_Components[i].m_UID);
246 if(m_Components[i].m_Texture.get()->GetTexID()!=std::numeric_limits<uint32_t>::max()){
247 RENDERER->DrawTexture({Interpolate(entity->m_X,entity->m_PreviousX),Interpolate(entity->m_Y,entity->m_PreviousY)},{m_Components[i].m_Width,m_Components[i].m_Height},false,false,m_Components[i].m_Layer,*m_Components[i].m_Texture);
248 }
249 }
250}
251
252template<>
254 PROFILE_FUNCTION();
255
256 Entity *entity=nullptr;
257 for(int i=0;i<m_Components.size();i++){
258 entity=scene->GetEntity(m_Components[i].m_UID);
259 if(m_Components[i].m_AnimatedTexture.get()->GetTexID()!=std::numeric_limits<uint32_t>::max()){
260 RENDERER->DrawAnimatedTexture({Interpolate(entity->m_X,entity->m_PreviousX),Interpolate(entity->m_Y,entity->m_PreviousY)},{m_Components[i].m_Width,m_Components[i].m_Height},m_Components[i].m_Layer,*m_Components[i].m_AnimatedTexture,
261 m_Components[i].m_PlayAnimation,m_Components[i].m_LoopAnimation,m_Components[i].m_AnimationDelay,m_Components[i].m_LastAnimationTime,m_Components[i].m_AnimationIndex);
262 }
263 }
264}
265
266template<>
268 PROFILE_FUNCTION();
269
270 Entity *entity=nullptr;
271 for(int i=0;i<m_Components.size();i++){
272 entity=scene->GetEntity(m_Components[i].m_UID);
273 RENDERER->DrawLight({Interpolate(entity->m_X,entity->m_PreviousX)+m_Components[i].m_XOffset,Interpolate(entity->m_Y,entity->m_PreviousY)+m_Components[i].m_YOffset},Vec4(m_Components[i].m_Color.r,m_Components[i].m_Color.g,m_Components[i].m_Color.b,1.0f),m_Components[i].m_Type,m_Components[i].m_Radius,m_Components[i].m_Blur);
274 }
275}
276
277template<>
279 PROFILE_FUNCTION();
280
281 Entity *entity=nullptr;
282 for(int i=0;i<m_Components.size();i++){
283 entity=scene->GetEntity(m_Components[i].m_UID);
284 if(m_Components[i].m_TextRenderer!=nullptr)
285 m_Components[i].m_TextRenderer->DrawText(m_Components[i].m_Text,Interpolate(entity->m_X,entity->m_PreviousX)+m_Components[i].m_Offset.x,Interpolate(entity->m_Y,entity->m_PreviousY)+m_Components[i].m_Offset.y,m_Components[i].m_Scale,(m_Components[i].m_IgnoreLighting?std::numeric_limits<int>::max()-1:m_Components[i].m_Layer),m_Components[i].m_Color);
286 }
287}
void PlayAnimation(bool loop=false, float delay=0.0f)
Definition entity.cpp:123
std::shared_ptr< SpriteSheet > m_AnimatedTexture
Definition entity.hpp:189
void Render(Scene *scene)
void Update(Scene *scene, float frame_time)
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
float m_YOffset
Definition entity.hpp:271
void DrawTexture(Vec2 pos, Vec2 size, int layer, float texID)
Definition renderer.cpp:254
void DrawLight(Vec2 pos, Vec4 color, LightType light_type, float radius=0.0f, float blurAmount=0.0f)
Definition renderer.cpp:802
void DrawAnimatedTexture(Vec2 pos, Vec2 size, int layer, SpriteSheet &s, bool &play_animation, bool loop_animation, float animation_delay, float &last_animation_time, int &animation_index)
Definition renderer.cpp:321
Definition scene.hpp:6
Entity * GetEntity(uint32_t uid)
Definition scene.cpp:51
std::string m_Tag
Definition entity.hpp:111
uint32_t m_UID
Definition entity.hpp:112
float m_Scale
Definition entity.hpp:316
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
void SetCentered(float width, float height)
Definition entity.cpp:191
std::shared_ptr< Texture > m_Texture
Definition entity.hpp:143
uint32_t m_UID
Definition entity.hpp:146
std::pair< uint32_t, std::shared_ptr< SpriteSheet > > GetSpriteSheet(const std::string &path, unsigned int tile_width, unsigned int tile_height, int mag_filter, int min_filter)
std::pair< uint32_t, std::shared_ptr< Texture > > GetTexture(const std::string &path, int mag_filter, int min_filter)
uint32_t NEXT_UID
Definition entity.cpp:8
std::ostream & operator<<(std::ostream &os, const RigidbodyComponent::BodyType &type)
Definition entity.cpp:198
float Interpolate(float current, float previous)
Definition entity.hpp:83
LightType
Definition global.hpp:7
@ LIGHT_AROUND_POS
Definition global.hpp:8
int i
float y
Definition structs.hpp:9
float x
Definition structs.hpp:5
TexturesManager * TEXTURES_MANAGER
The textures manager.
Definition window.cpp:15
Renderer * RENDERER
The main renderer.
Definition window.cpp:12