2D_Game_Engine
Loading...
Searching...
No Matches
sceneserializer.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <sceneserializer.hpp>
3#include <entity.hpp>
4#include <global.hpp>
5#include <texturesmanager.hpp>
6
8 m_Scene=scene;
9}
10
12 switch(bodytype){
13 case RigidbodyComponent::BodyType::Static: return "Static";
14 case RigidbodyComponent::BodyType::Dynamic: return "Dynamic";
15 case RigidbodyComponent::BodyType::Kinematic: return "Kinematic";
16 default: return "";
17 }
18}
19
21 if(bodytype=="Static") return RigidbodyComponent::BodyType::Static;
22 if(bodytype=="Dynamic") return RigidbodyComponent::BodyType::Dynamic;
23 if(bodytype=="Kinematic") return RigidbodyComponent::BodyType::Kinematic;
25}
26
27std::string LightTypeToString(LightType lighttype){
28 switch(lighttype){
29 case LightType::ALL_LIGHT: return "AllLight";
30 case LightType::LIGHT_AROUND_POS: return "LightAroundPos";
31 case LightType::LIGHT_AROUND_POS_COLL: return "LightAroundPosColl";
32 default: return "";
33 }
34}
35
36LightType LightTypeFromString(std::string lighttype){
37 if(lighttype=="AllLight") return LightType::ALL_LIGHT;
38 if(lighttype=="LightAroundPos") return LightType::LIGHT_AROUND_POS;
39 if(lighttype=="LightAroundPosColl") return LightType::LIGHT_AROUND_POS_COLL;
41}
42
43namespace YAML{
44 Emitter& operator<<(Emitter &out,const Vec3 &rhs){
45 out<<Flow;
46 out<<BeginSeq<<rhs.r<<rhs.g<<rhs.b<<EndSeq;
47 return out;
48 }
49
50 Emitter& operator<<(Emitter &out,const Vec2 &rhs){
51 out<<Flow;
52 out<<BeginSeq<<rhs.x<<rhs.y<<EndSeq;
53 return out;
54 }
55
56 template<>
57 struct convert<Vec3>{
58 static Node encode(const Vec3 &rhs){
59 Node node;
60 node.push_back(rhs.r);
61 node.push_back(rhs.g);
62 node.push_back(rhs.b);
63 return node;
64 }
65
66 static bool decode(const Node &node,Vec3 &rhs) {
67 if(!node.IsSequence() || node.size()!=3){
68 return false;
69 }
70
71 rhs.r=node[0].as<float>();
72 rhs.g=node[1].as<float>();
73 rhs.b=node[2].as<float>();
74 return true;
75 }
76 };
77
78 template<>
79 struct convert<Vec2>{
80 static Node encode(const Vec2 &rhs){
81 Node node;
82 node.push_back(rhs.x);
83 node.push_back(rhs.y);
84 return node;
85 }
86
87 static bool decode(const Node &node,Vec2 &rhs) {
88 if(!node.IsSequence() || node.size()!=2){
89 return false;
90 }
91
92 rhs.x=node[0].as<float>();
93 rhs.y=node[1].as<float>();
94 return true;
95 }
96 };
97}
98
99void SceneSerializer::SerializeEntity(YAML::Emitter &out,Entity &entity,Scene *scene,std::vector<std::pair<std::string,uint32_t>>&script_components){
100 out<<YAML::BeginMap; //entity and components
101
102 out<<YAML::Key<<"Entity";
103 out<<YAML::BeginMap; //entity
104
105 out<<YAML::Key<<"UID"<<YAML::Value<<entity.m_UID;
106 out<<YAML::Key<<"X"<<YAML::Value<<entity.m_X;
107 out<<YAML::Key<<"Y"<<YAML::Value<<entity.m_Y;
108 out<<YAML::Key<<"PreviousX"<<YAML::Value<<entity.m_PreviousX;
109 out<<YAML::Key<<"PreviousY"<<YAML::Value<<entity.m_PreviousY;
110
111 out<<YAML::EndMap;
112
113 if(scene->GetComponent<TagComponent>(entity.m_UID)!=nullptr){
114 out<<YAML::Key<<"TagComponent";
115 out<<YAML::BeginMap; //tag component
116
117 TagComponent *tagcomponent=scene->GetComponent<TagComponent>(entity.m_UID);
118
119 std::string tag=tagcomponent->m_Tag;
120 tag.erase(std::remove(tag.begin(),tag.end(),'\0'),tag.end());
121
122 out<<YAML::Key<<"Tag"<<YAML::Value<<tag;
123
124 out<<YAML::EndMap; //tag component
125 }
126
127 if(scene->GetComponent<TextureComponent>(entity.m_UID)!=nullptr){
128
129 out<<YAML::Key<<"TextureComponent";
130 out<<YAML::BeginMap; //texture component
131
132 TextureComponent *texturecomponent=scene->GetComponent<TextureComponent>(entity.m_UID);
133
134 out<<YAML::Key<<"Filepath"<<YAML::Value<<texturecomponent->m_Texture.get()->m_LoadedFilePath;
135 out<<YAML::Key<<"MagFilter"<<YAML::Value<<texturecomponent->m_Texture.get()->m_LoadedMagFilter;
136 out<<YAML::Key<<"MinFilter"<<YAML::Value<<texturecomponent->m_Texture.get()->m_LoadedMinFilter;
137
138 out<<YAML::Key<<"Width"<<YAML::Value<<texturecomponent->m_Width;
139 out<<YAML::Key<<"Height"<<YAML::Value<<texturecomponent->m_Height;
140 out<<YAML::Key<<"Layer"<<YAML::Value<<texturecomponent->m_Layer;
141
142 out<<YAML::EndMap; //texture component
143 }
144
145 if(scene->GetComponent<AnimatedTextureComponent>(entity.m_UID)!=nullptr){
146 out<<YAML::Key<<"AnimatedTextureComponent";
147 out<<YAML::BeginMap; //animated texture component
148
149 AnimatedTextureComponent *animatedtexturecomponent=scene->GetComponent<AnimatedTextureComponent>(entity.m_UID);
150
151 out<<YAML::Key<<"Filepath"<<YAML::Value<<animatedtexturecomponent->m_AnimatedTexture.get()->m_LoadedFilePath;
152 out<<YAML::Key<<"MagFilter"<<YAML::Value<<animatedtexturecomponent->m_AnimatedTexture.get()->m_LoadedMagFilter;
153 out<<YAML::Key<<"MinFilter"<<YAML::Value<<animatedtexturecomponent->m_AnimatedTexture.get()->m_LoadedMinFilter;
154
155 out<<YAML::Key<<"TileWidth"<<YAML::Value<<animatedtexturecomponent->m_AnimatedTexture.get()->m_TileWidth;
156 out<<YAML::Key<<"TileHeight"<<YAML::Value<<animatedtexturecomponent->m_AnimatedTexture.get()->m_TileHeight;
157
158 out<<YAML::Key<<"PlayAnimation"<<YAML::Value<<animatedtexturecomponent->m_PlayAnimation;
159 out<<YAML::Key<<"LoopAnimation"<<YAML::Value<<animatedtexturecomponent->m_LoopAnimation;
160 out<<YAML::Key<<"AnimationDelay"<<YAML::Value<<animatedtexturecomponent->m_AnimationDelay;
161 out<<YAML::Key<<"AnimationIndex"<<YAML::Value<<animatedtexturecomponent->m_AnimationIndex;
162
163 out<<YAML::Key<<"Width"<<YAML::Value<<animatedtexturecomponent->m_Width;
164 out<<YAML::Key<<"Height"<<YAML::Value<<animatedtexturecomponent->m_Height;
165 out<<YAML::Key<<"Layer"<<YAML::Value<<animatedtexturecomponent->m_Layer;
166
167 out<<YAML::EndMap; //animated texture component
168 }
169
170 if(scene->GetComponent<RigidbodyComponent>(entity.m_UID)!=nullptr){
171 out<<YAML::Key<<"RigidbodyComponent";
172 out<<YAML::BeginMap; //rigidbody component
173
174 RigidbodyComponent *rigidbodycomponent=scene->GetComponent<RigidbodyComponent>(entity.m_UID);
175
176 out<<YAML::Key<<"BodyType"<<YAML::Value<<BodyTypeToString(rigidbodycomponent->m_BodyType);
177 out<<YAML::Key<<"FixedRotation"<<YAML::Value<<rigidbodycomponent->m_FixedRotation;
178
179 out<<YAML::EndMap; //rigidbody component
180 }
181
182 if(scene->GetComponent<BoxColliderComponent>(entity.m_UID)!=nullptr){
183 out<<YAML::Key<<"BoxColliderComponent";
184 out<<YAML::BeginMap; //box collider component
185
186 BoxColliderComponent *boxcollidercomponent=scene->GetComponent<BoxColliderComponent>(entity.m_UID);
187
188 out<<YAML::Key<<"XOffset"<<YAML::Value<<boxcollidercomponent->m_XOffset;
189 out<<YAML::Key<<"YOffset"<<YAML::Value<<boxcollidercomponent->m_YOffset;
190 out<<YAML::Key<<"Width"<<YAML::Value<<boxcollidercomponent->m_Width;
191 out<<YAML::Key<<"Height"<<YAML::Value<<boxcollidercomponent->m_Height;
192 out<<YAML::Key<<"Density"<<YAML::Value<<boxcollidercomponent->m_Density;
193 out<<YAML::Key<<"Friction"<<YAML::Value<<boxcollidercomponent->m_Friction;
194 out<<YAML::Key<<"Restitution"<<YAML::Value<<boxcollidercomponent->m_Restitution;
195 out<<YAML::Key<<"RestitutionThreshold"<<YAML::Value<<boxcollidercomponent->m_RestitutionThreshold;
196
197 out<<YAML::EndMap; //box collider component
198 }
199
200 if(scene->GetComponent<CircleColliderComponent>(entity.m_UID)!=nullptr){
201 out<<YAML::Key<<"CircleColliderComponent";
202 out<<YAML::BeginMap; //circle collider component
203
204 CircleColliderComponent *circlecollidercomponent=scene->GetComponent<CircleColliderComponent>(entity.m_UID);
205
206 out<<YAML::Key<<"XOffset"<<YAML::Value<<circlecollidercomponent->m_XOffset;
207 out<<YAML::Key<<"YOffset"<<YAML::Value<<circlecollidercomponent->m_YOffset;
208 out<<YAML::Key<<"Radius"<<YAML::Value<<circlecollidercomponent->m_Radius;
209 out<<YAML::Key<<"Density"<<YAML::Value<<circlecollidercomponent->m_Density;
210 out<<YAML::Key<<"Friction"<<YAML::Value<<circlecollidercomponent->m_Friction;
211 out<<YAML::Key<<"Restitution"<<YAML::Value<<circlecollidercomponent->m_Restitution;
212 out<<YAML::Key<<"RestitutionThreshold"<<YAML::Value<<circlecollidercomponent->m_RestitutionThreshold;
213
214 out<<YAML::EndMap; //circle collider component
215 }
216
217 if(scene->GetComponent<LightComponent>(entity.m_UID)!=nullptr){
218 out<<YAML::Key<<"LightComponent";
219 out<<YAML::BeginMap; //light component
220
221 LightComponent *lightcomponent=scene->GetComponent<LightComponent>(entity.m_UID);
222
223 out<<YAML::Key<<"XOffset"<<YAML::Value<<lightcomponent->m_XOffset;
224 out<<YAML::Key<<"YOffset"<<YAML::Value<<lightcomponent->m_YOffset;
225 out<<YAML::Key<<"Radius"<<YAML::Value<<lightcomponent->m_Radius;
226 out<<YAML::Key<<"Blur"<<YAML::Value<<lightcomponent->m_Blur;
227 out<<YAML::Key<<"Color"<<YAML::Value<<lightcomponent->m_Color;
228 out<<YAML::Key<<"LightType"<<YAML::Value<<LightTypeToString(lightcomponent->m_Type);
229
230 out<<YAML::EndMap; //light component
231 }
232
233 if(scene->GetComponent<TextComponent>(entity.m_UID)!=nullptr){
234 out<<YAML::Key<<"TextComponent";
235 out<<YAML::BeginMap; //text component
236
237 TextComponent *textcomponent=scene->GetComponent<TextComponent>(entity.m_UID);
238
239 out<<YAML::Key<<"FontPath"<<YAML::Value<<textcomponent->m_TextRenderer->m_LoadedFontPath;
240 out<<YAML::Key<<"GlyphSize"<<YAML::Value<<textcomponent->m_TextRenderer->m_LoadedGlyphSize;
241 out<<YAML::Key<<"Fixed"<<YAML::Value<<textcomponent->m_TextRenderer->m_Fixed;
242 out<<YAML::Key<<"Text"<<YAML::Value<<textcomponent->m_Text;
243 out<<YAML::Key<<"Offset"<<YAML::Value<<textcomponent->m_Offset;
244 out<<YAML::Key<<"Color"<<YAML::Value<<textcomponent->m_Color;
245 out<<YAML::Key<<"Scale"<<YAML::Value<<textcomponent->m_Scale;
246 out<<YAML::Key<<"Layer"<<YAML::Value<<textcomponent->m_Layer;
247 out<<YAML::Key<<"IgnoreLighting"<<YAML::Value<<textcomponent->m_IgnoreLighting;
248
249 out<<YAML::EndMap; //text component
250 }
251
252 for(auto &[fn_name,id]:script_components){
253 if(id==entity.m_UID){
254 if(!fn_name.empty()){
255 out<<YAML::Key<<"NativeScriptComponent";
256 out<<YAML::BeginMap; //native script component
257 out<<YAML::Key<<"FunctionName"<<YAML::Value<<fn_name;
258 out<<YAML::EndMap; //native script component
259 }
260 }
261 }
262 out<<YAML::EndMap; //entity and components
263}
264
265void SceneSerializer::Serialize(const std::string &path,std::vector<std::pair<std::string,uint32_t>>&script_components){
266 printf("Starting serialization of scene file %s\n",path.c_str());
267 YAML::Emitter out;
268 out<<YAML::BeginMap;
269 out<<YAML::Key<<"Scene"<<YAML::Value<<m_Scene->m_Name;
270 out<<YAML::Key<<"ScalingFactor"<<YAML::Value<<m_Scene->m_ScalingFactor;
271 out<<YAML::Key<<"Gravity"<<YAML::Value<<m_Scene->m_Gravity;
272 out<<YAML::Key<<"NextUID"<<YAML::Value<<NEXT_UID;
273 out<<YAML::Key<<"AmbientLight"<<YAML::Value<<RENDERER->m_AmbientLight;
274 out<<YAML::Key<<"ClearColor"<<YAML::Value<<RENDERER->m_ClearColor;
275 out<<YAML::Key<<"Entities"<<YAML::Value<<YAML::BeginSeq;
276
277 std::vector<Entity>& entities=m_Scene->GetEntities();
278 for(int i=0;i<entities.size();i++){
279 SerializeEntity(out,entities[i],m_Scene,script_components);
280 }
281
282 out<<YAML::EndSeq;
283 out<<YAML::EndMap;
284
285 FILE *fout=fopen(path.c_str(),"w");
286 if(fout==NULL){
287 printf("Failed to open scene file %s: %s\n",path.c_str(),strerror(errno));
288 return;
289 }
290 fprintf(fout,"%s",out.c_str());
291 fclose(fout);
292 printf("Serialization of scene file %s completed\n",path.c_str());
293}
294
295void SceneSerializer::SerializeEncrypted(const std::string &path,std::vector<std::pair<std::string,uint32_t>>&script_components){
296 printf("Starting binary serialization of scene file %s\n",path.c_str());
297
298 Serialize(path,script_components);
299 FILE *fin=fopen(path.c_str(),"r+");
300 if(fin==NULL){
301 printf("Failed to open scene file %s: %s\n",path.c_str(),strerror(errno));
302 return;
303 }
304
305 char ch;
306 int i=0;
307
308 srand(time(NULL));
309 std::string key;
310 for(int i=0;i<rand()%100+1;i++){
311 key+=rand()%128;
312 }
313
314 FILE *fkey=fopen("key.bin","wb");
315 if(fkey==NULL){
316 printf("Failed to open key file: %s\n",strerror(errno));
317 return;
318 }
319 for(int i=0;i<key.size();i++){
320 for(int j=0;j<8;j++){
321 char bit=(key[i]>>j)&1;
322 fputc(bit,fkey);
323 }
324 }
325
326 fclose(fkey);
327
328 while((ch=fgetc(fin))!=EOF){
329 ch^=key[i];
330 i=(i+1)%key.size();
331 fseek(fin,-1,SEEK_CUR);
332 fputc(ch,fin);
333 fseek(fin,0,SEEK_CUR);
334 }
335
336 fclose(fin);
337}
338
339std::string EncodeTexture(bool animated,const std::string &path,unsigned int *tile_width,unsigned int *tile_height,int mag_filter,int min_filter){
340 std::string encoded;
341 encoded+=std::to_string(animated)+";";
342 encoded+=path+";";
343 encoded+=std::to_string(mag_filter)+";";
344 encoded+=std::to_string(min_filter)+";";
345 if(animated){
346 #ifdef DEBUG
347 assert(tile_width!=nullptr);
348 assert(tile_height!=nullptr);
349 #endif
350 encoded+=std::to_string(*tile_width)+";";
351 encoded+=std::to_string(*tile_height)+";";
352 }
353 return encoded;
354}
355
356bool GetTextureType(const std::string &encoded){
357 std::vector<std::string> tokens;
358 std::string token;
359 std::istringstream tokenstream(encoded);
360 std::getline(tokenstream,token,';');
361 return std::stoi(token);
362}
363
364void DecodeTexture(const std::string &encoded,std::string &path,unsigned int &tile_width,unsigned int &tile_height,int &mag_filter,int &min_filter){
365 std::vector<std::string> tokens;
366 std::string token;
367 std::istringstream tokenstream(encoded);
368 while(std::getline(tokenstream,token,';')){
369 tokens.push_back(token);
370 }
371 if(tokens.size()<6){
372 printf("Invalid encoded animated texture\n");
373 printf("%s\n",encoded.c_str());
374 return;
375 }
376 path=tokens[1];
377 mag_filter=std::stoi(tokens[2]);
378 min_filter=std::stoi(tokens[3]);
379 tile_width=std::stoi(tokens[4]);
380 tile_height=std::stoi(tokens[5]);
381}
382
383void DecodeTexture(const std::string &encoded,std::string &path,int &mag_filter,int &min_filter){
384 std::vector<std::string> tokens;
385 std::string token;
386 std::istringstream tokenstream(encoded);
387 while(std::getline(tokenstream,token,';')){
388 tokens.push_back(token);
389 }
390 if(tokens.size()<4){
391 printf("Invalid encoded texture\n");
392 printf("%s\n",encoded.c_str());
393 return;
394 }
395 path=tokens[1];
396 mag_filter=std::stoi(tokens[2]);
397 min_filter=std::stoi(tokens[3]);
398}
399
400#ifdef EDITOR
401bool SceneSerializer::Deserialize(const std::string &path,std::vector<std::pair<std::string,uint32_t>>&script_components){
402#elif defined(APPLICATION)
403bool SceneSerializer::Deserialize(const std::string &path){
404#endif
405 YAML::Node data=YAML::LoadFile(path);
406
407 //missing error handling
408
409 #ifdef EDITOR
410 return DeserializeNode(data,script_components);
411 #elif defined(APPLICATION)
412 return DeserializeNode(data);
413 #endif
414}
415
416#ifdef EDITOR
417bool SceneSerializer::DeserializeEncrypted(const std::string &path,std::vector<std::pair<std::string,uint32_t>>&script_components){
418#elif defined(APPLICATION)
419bool SceneSerializer::DeserializeEncrypted(const std::string &path){
420#endif
421 std::string file_content;
422 std::string key;
423
424 FILE *fkey=fopen("key.bin","rb");
425 if(fkey==NULL){
426 printf("Failed to open key file: %s\n",strerror(errno));
427 return false;
428 }
429
430 char ch=0;
432 char bit;
433 while((bit=fgetc(fkey))!=EOF){
434 ch|=(bit<<bit_count);
435 bit_count++;
436 if(bit_count==8){
437 bit_count=0;
438 key+=ch;
439 ch=0;
440 }
441 }
442
444
445 FILE *fin=fopen(path.c_str(),"r");
446 if(fin==NULL){
447 printf("Failed to open scene file %s: %s\n",path.c_str(),strerror(errno));
448 return false;
449 }
450
451 int i=0;
452 while((ch=fgetc(fin))!=EOF){
453 ch^=key[i];
454 i=(i+1)%key.size();
456 }
457
459
460 YAML::Node data=YAML::Load(file_content);
461
462 //missing error handling
463
464 #ifdef EDITOR
465 return DeserializeNode(data,script_components);
466 #elif defined(APPLICATION)
467 return DeserializeNode(data);
468 #endif
469}
470
471#ifdef EDITOR
472bool SceneSerializer::DeserializeNode(const YAML::Node &data,std::vector<std::pair<std::string,uint32_t>>&script_components){
473#elif defined(APPLICATION)
474bool SceneSerializer::DeserializeNode(const YAML::Node &data){
475#endif
476
477 if(!data["Scene"]){
478 printf("Scene node is invalid\n");
479 return false;
480 }
481
482 m_Scene->m_Name=data["Scene"].as<std::string>();
483 m_Scene->m_ScalingFactor=data["ScalingFactor"].as<float>();
484 m_Scene->m_Gravity=data["Gravity"].as<Vec2>();
485 NEXT_UID=data["NextUID"].as<uint32_t>();
486 RENDERER->m_AmbientLight=data["AmbientLight"].as<Vec3>();
487 RENDERER->m_ClearColor=data["ClearColor"].as<Vec3>();
488
489 auto entities=data["Entities"];
491 for(auto e:entities){
492 auto ent=e["Entity"];
493 uint32_t uid=ent["UID"].as<uint32_t>();
494 m_Scene->AddEntity(uid);
495 auto entity=m_Scene->GetEntity(uid);
496 entity->m_X=ent["X"].as<float>();
497 entity->m_Y=ent["Y"].as<float>();
498 entity->m_PreviousX=ent["PreviousX"].as<float>();
499 entity->m_PreviousY=ent["PreviousY"].as<float>();
500
501 auto tagcomponent=e["TagComponent"];
502 if(tagcomponent){
503 std::string tag=tagcomponent["Tag"].as<std::string>();
504 m_Scene->AddComponent<TagComponent>(uid,tag);
505 }
506
507 auto texturecomponent=e["TextureComponent"];
508 if(texturecomponent){
509 std::string filepath=texturecomponent["Filepath"].as<std::string>();
510 int magfilter=texturecomponent["MagFilter"].as<int>();
511 int minfilter=texturecomponent["MinFilter"].as<int>();
512 int width=texturecomponent["Width"].as<int>();
513 int height=texturecomponent["Height"].as<int>();
514 int layer=texturecomponent["Layer"].as<int>();
515
516 auto [id,texture]=TEXTURES_MANAGER->GetTexture(filepath,magfilter,minfilter);
517 m_Scene->AddComponent<TextureComponent>(uid,texture,width,height,layer);
518 }
519
520 auto animatedtexturecomponent=e["AnimatedTextureComponent"];
521 if(animatedtexturecomponent){
522 std::string filepath=animatedtexturecomponent["Filepath"].as<std::string>();
523 int magfilter=animatedtexturecomponent["MagFilter"].as<int>();
524 int minfilter=animatedtexturecomponent["MinFilter"].as<int>();
525 unsigned int tilewidth=animatedtexturecomponent["TileWidth"].as<unsigned int>();
526 unsigned int tileheight=animatedtexturecomponent["TileHeight"].as<unsigned int>();
527 bool playanimation=animatedtexturecomponent["PlayAnimation"].as<bool>();
528 bool loopanimation=animatedtexturecomponent["LoopAnimation"].as<bool>();
529 float animationdelay=animatedtexturecomponent["AnimationDelay"].as<float>();
530 int animationindex=animatedtexturecomponent["AnimationIndex"].as<int>();
531 int width=animatedtexturecomponent["Width"].as<float>();
532 int height=animatedtexturecomponent["Height"].as<float>();
533 int layer=animatedtexturecomponent["Layer"].as<int>();
534
535 auto [id,texture]=TEXTURES_MANAGER->GetSpriteSheet(filepath,tilewidth,tileheight,magfilter,minfilter);
536 m_Scene->AddComponent<AnimatedTextureComponent>(uid,texture,width,height,layer,playanimation,loopanimation,animationdelay,animationindex);
537 }
538
539 auto rigidbodycomponent=e["RigidbodyComponent"];
540 if(rigidbodycomponent){
541 RigidbodyComponent::BodyType bodytype=BodyTypeFromString(rigidbodycomponent["BodyType"].as<std::string>());
542 bool fixedrotation=rigidbodycomponent["FixedRotation"].as<bool>();
543
544 m_Scene->AddComponent<RigidbodyComponent>(uid,bodytype,fixedrotation);
545 }
546
547 auto boxcollidercomponent=e["BoxColliderComponent"];
548 if(boxcollidercomponent){
549 float xoffset=boxcollidercomponent["XOffset"].as<float>();
550 float yoffset=boxcollidercomponent["YOffset"].as<float>();
551 float width=boxcollidercomponent["Width"].as<float>();
552 float height=boxcollidercomponent["Height"].as<float>();
553 float density=boxcollidercomponent["Density"].as<float>();
554 float friction=boxcollidercomponent["Friction"].as<float>();
555 float restitution=boxcollidercomponent["Restitution"].as<float>();
556 float restitutionthreshold=boxcollidercomponent["RestitutionThreshold"].as<float>();
557
558 m_Scene->AddComponent<BoxColliderComponent>(uid,xoffset,yoffset,width,height,density,friction,restitution,restitutionthreshold);
559 }
560
561 auto circlecollidercomponent=e["CircleColliderComponent"];
562 if(circlecollidercomponent){
563 float xoffset=circlecollidercomponent["XOffset"].as<float>();
564 float yoffset=circlecollidercomponent["YOffset"].as<float>();
565 float radius=circlecollidercomponent["Radius"].as<float>();
566 float density=circlecollidercomponent["Density"].as<float>();
567 float friction=circlecollidercomponent["Friction"].as<float>();
568 float restitution=circlecollidercomponent["Restitution"].as<float>();
569 float restitutionthreshold=circlecollidercomponent["RestitutionThreshold"].as<float>();
570
571 m_Scene->AddComponent<CircleColliderComponent>(uid,xoffset,yoffset,radius,density,friction,restitution,restitutionthreshold);
572 }
573
574 auto lightcomponent=e["LightComponent"];
575 if(lightcomponent){
576 float xoffset=lightcomponent["XOffset"].as<float>();
577 float yoffset=lightcomponent["YOffset"].as<float>();
578 float radius=lightcomponent["Radius"].as<float>();
579 float blur=lightcomponent["Blur"].as<float>();
580 Vec3 color=lightcomponent["Color"].as<Vec3>();
581 LightType lighttype=LightTypeFromString(lightcomponent["LightType"].as<std::string>());
582
583 m_Scene->AddComponent<LightComponent>(uid,xoffset,yoffset,radius,blur,color,lighttype);
584 }
585
586 auto textcomponent=e["TextComponent"];
587 if(textcomponent){
588 std::string fontpath=textcomponent["FontPath"].as<std::string>();
589 float glyphsize=textcomponent["GlyphSize"].as<float>();
590 bool fixed=textcomponent["Fixed"].as<bool>();
591 std::string text=textcomponent["Text"].as<std::string>();
592 Vec2 offset=textcomponent["Offset"].as<Vec2>();
593 Vec3 color=textcomponent["Color"].as<Vec3>();
594 float scale=textcomponent["Scale"].as<float>();
595 int layer=textcomponent["Layer"].as<int>();
596 bool ignore_lighting=textcomponent["IgnoreLighting"].as<bool>();
597
598 m_Scene->AddComponent<TextComponent>(uid,fontpath,glyphsize,fixed,text,offset,color,scale,layer,ignore_lighting);
599 }
600
601
602 #ifdef EDITOR
603 auto nativescriptcomponent=e["NativeScriptComponent"];
604 if(nativescriptcomponent){
605 std::string functionname=nativescriptcomponent["FunctionName"].as<std::string>();
606 script_components.push_back(std::make_pair(functionname,uid));
607 }
608 #endif
609 }
610 }
611
612 return true;
613}
std::shared_ptr< SpriteSheet > m_AnimatedTexture
Definition entity.hpp:189
float m_RestitutionThreshold
Definition entity.hpp:234
float m_PreviousX
Definition entity.hpp:334
float m_X
Definition entity.hpp:333
uint32_t m_UID
Definition entity.hpp:332
float m_Y
Definition entity.hpp:333
float m_PreviousY
Definition entity.hpp:334
float m_XOffset
Definition entity.hpp:271
LightType m_Type
Definition entity.hpp:274
float m_YOffset
Definition entity.hpp:271
BodyType m_BodyType
Definition entity.hpp:211
void SerializeEncrypted(const std::string &path, std::vector< std::pair< std::string, uint32_t > > &script_components)
void SetScene(Scene *scene)
void SerializeEntity(YAML::Emitter &out, Entity &entity, Scene *scene, std::vector< std::pair< std::string, uint32_t > > &script_components)
void Serialize(const std::string &path, std::vector< std::pair< std::string, uint32_t > > &script_components)
Definition scene.hpp:6
void AddComponent(uint32_t uid, Args...args)
Definition scene.hpp:79
uint32_t AddEntity()
Definition scene.cpp:42
T * GetComponent(uint32_t uid)
Entity * GetEntity(uint32_t uid)
Definition scene.cpp:51
std::vector< Entity > & GetEntities()
Definition scene.cpp:337
std::string m_Tag
Definition entity.hpp:111
float m_Scale
Definition entity.hpp:316
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
std::shared_ptr< Texture > m_Texture
Definition entity.hpp:143
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)
LightType
Definition global.hpp:7
@ LIGHT_AROUND_POS_COLL
Definition global.hpp:8
@ ALL_LIGHT
Definition global.hpp:8
@ LIGHT_AROUND_POS
Definition global.hpp:8
Emitter & operator<<(Emitter &out, const Vec3 &rhs)
std::string file_content
std::string LightTypeToString(LightType lighttype)
bool GetTextureType(const std::string &encoded)
void DecodeTexture(const std::string &encoded, std::string &path, unsigned int &tile_width, unsigned int &tile_height, int &mag_filter, int &min_filter)
fclose(fkey)
char bit
FILE * fkey
std::string BodyTypeToString(RigidbodyComponent::BodyType bodytype)
auto entities
LightType LightTypeFromString(std::string lighttype)
std::string key
FILE * fin
YAML::Node data
int i
std::string EncodeTexture(bool animated, const std::string &path, unsigned int *tile_width, unsigned int *tile_height, int mag_filter, int min_filter)
RigidbodyComponent::BodyType BodyTypeFromString(std::string bodytype)
int bit_count
char ch
float y
Definition structs.hpp:9
float x
Definition structs.hpp:5
float b
Definition structs.hpp:17
float r
Definition structs.hpp:17
float g
Definition structs.hpp:17
static Node encode(const Vec2 &rhs)
static bool decode(const Node &node, Vec2 &rhs)
static Node encode(const Vec3 &rhs)
static bool decode(const Node &node, Vec3 &rhs)
TexturesManager * TEXTURES_MANAGER
The textures manager.
Definition window.cpp:15
Renderer * RENDERER
The main renderer.
Definition window.cpp:12