| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* Filename: /home/thomas/Documents/Perso/our_rpg/sources/System/system_load_scene |
| 3 |
|
|
* Path: /home/thomas/Documents/Perso/our_rpg/sources/System |
| 4 |
|
|
* Created Date: Tuesday, March 21st 2023, 9:40:17 am |
| 5 |
|
|
* Author: Thomas |
| 6 |
|
|
* |
| 7 |
|
|
* Copyright (c) 2023 our_rpg |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include "world.h" |
| 11 |
|
|
#include "world_system.h" |
| 12 |
|
|
#include "world_resource.h" |
| 13 |
|
|
#include "world_entity.h" |
| 14 |
|
|
#include "world_loader_constructors.h" |
| 15 |
|
|
#include "json.h" |
| 16 |
|
|
|
| 17 |
|
✗ |
int system_load_scene_constructor(system_t *system) |
| 18 |
|
|
{ |
| 19 |
|
✗ |
system->run = system_load_scene_run; |
| 20 |
|
✗ |
system->type = S_LOAD_SCENE; |
| 21 |
|
✗ |
system->active = true; |
| 22 |
|
✗ |
return 0; |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
|
✗ |
static int update_systems(world_t *world, json_object *systems) |
| 26 |
|
|
{ |
| 27 |
|
✗ |
system_t system = {0}; |
| 28 |
|
|
json_object *tmp = 0; |
| 29 |
|
|
|
| 30 |
|
✗ |
while (world->system_list.size(&world->system_list) > 0) { |
| 31 |
|
✗ |
world->system_list.pop_back(&world->system_list); |
| 32 |
|
|
} |
| 33 |
|
✗ |
for (int i = 0; i < json_object_array_length(systems); i++) { |
| 34 |
|
✗ |
tmp = json_object_array_get_idx(systems, i); |
| 35 |
|
✗ |
for (int j = 0; systems_types[j]; j++) { |
| 36 |
|
✗ |
if (strcmp(json_object_get_string(tmp), systems_types[j]) == 0) { |
| 37 |
|
✗ |
systems_constructors[j](&system); |
| 38 |
|
✗ |
world_add_system(world, &system); |
| 39 |
|
✗ |
break; |
| 40 |
|
|
} |
| 41 |
|
|
} |
| 42 |
|
|
} |
| 43 |
|
✗ |
return 0; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
✗ |
static int update_resources(world_t *world, json_object *resources) |
| 47 |
|
|
{ |
| 48 |
|
✗ |
resource_t resource = {0}; |
| 49 |
|
|
json_object *tmp = 0; |
| 50 |
|
|
void *data = 0; |
| 51 |
|
|
|
| 52 |
|
✗ |
while (world->resource_list.size(&world->resource_list) > 0) { |
| 53 |
|
✗ |
(resource_t *){world->resource_list.back(&world->resource_list)}->destructor(world->resource_list.back(&world->resource_list)); |
| 54 |
|
✗ |
world->resource_list.pop_back(&world->resource_list); |
| 55 |
|
|
} |
| 56 |
|
✗ |
for (int i = 0; i < json_object_array_length(resources); i++) { |
| 57 |
|
✗ |
tmp = json_object_array_get_idx(resources, i); |
| 58 |
|
✗ |
for (int j = 0; resources_types[j]; j++) { |
| 59 |
|
✗ |
if (strcmp(json_object_get_string(json_object_object_get(tmp, "type")), resources_types[j]) == 0) { |
| 60 |
|
✗ |
char *wooow = json_object_to_json_string(json_object_object_get(tmp, "data")); |
| 61 |
|
✗ |
resources_constructors[j](&resource, json_object_to_json_string(json_object_object_get(tmp, "data"))); |
| 62 |
|
✗ |
world_add_resource(world, &resource); |
| 63 |
|
✗ |
break; |
| 64 |
|
|
} |
| 65 |
|
|
} |
| 66 |
|
|
} |
| 67 |
|
✗ |
return 0; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
✗ |
static int update_entities(world_t *world, json_object *entities) |
| 71 |
|
|
{ |
| 72 |
|
✗ |
entity_t entity = {0}; |
| 73 |
|
✗ |
component_t component = {0}; |
| 74 |
|
|
json_object *tmp = 0; |
| 75 |
|
|
void *data = 0; |
| 76 |
|
|
|
| 77 |
|
✗ |
while (world->entity_list.size(&world->entity_list) > 0) { |
| 78 |
|
✗ |
entity_t *ptr = world->entity_list.back(&world->entity_list); |
| 79 |
|
✗ |
while (ptr->components.size(&ptr->components) > 0) { |
| 80 |
|
✗ |
component_destructor(ptr->components.back(&ptr->components)); |
| 81 |
|
✗ |
ptr->components.pop_back(&ptr->components); |
| 82 |
|
|
} |
| 83 |
|
✗ |
world->entity_list.pop_back(&world->entity_list); |
| 84 |
|
|
} |
| 85 |
|
✗ |
for (unsigned int i = 0; i < json_object_array_length(entities); i++) { |
| 86 |
|
✗ |
tmp = json_object_object_get(json_object_array_get_idx(entities, i), "components"); |
| 87 |
|
✗ |
entity_constructor(&entity); |
| 88 |
|
✗ |
for (unsigned int j = 0; j < json_object_array_length(tmp); j++) { |
| 89 |
|
✗ |
for (unsigned int k = 0; components_types[k]; k++) { |
| 90 |
|
✗ |
if (strcmp(json_object_get_string(json_object_object_get(json_object_array_get_idx(tmp, j), "type")), components_types[k]) == 0) { |
| 91 |
|
✗ |
components_constructors[k](&component, json_object_to_json_string(json_object_object_get(json_object_array_get_idx(tmp, j), "data"))); |
| 92 |
|
✗ |
entity_add_component(&entity, &component); |
| 93 |
|
✗ |
break; |
| 94 |
|
|
} |
| 95 |
|
|
} |
| 96 |
|
|
} |
| 97 |
|
✗ |
world_add_entity(world, &entity); |
| 98 |
|
|
} |
| 99 |
|
✗ |
return 0; |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
✗ |
int system_load_scene_run(void *ptr) |
| 103 |
|
|
{ |
| 104 |
|
|
world_t *world = ptr; |
| 105 |
|
✗ |
resource_t *scene_filename = world_get_resource_by_type(world, R_SCENE_FILENAME); |
| 106 |
|
|
json_object *json = 0; |
| 107 |
|
|
|
| 108 |
|
✗ |
if (scene_filename == 0) |
| 109 |
|
|
return -1; |
| 110 |
|
✗ |
json = json_object_from_file(scene_filename->data); |
| 111 |
|
✗ |
if (json == 0) |
| 112 |
|
|
return -2; |
| 113 |
|
✗ |
update_resources(world, json_object_object_get(json_object_object_get(json, "world"), "resources")); |
| 114 |
|
✗ |
update_systems(world, json_object_object_get(json_object_object_get(json, "world"), "systems")); |
| 115 |
|
✗ |
update_entities(world, json_object_object_get(json_object_object_get(json, "world"), "entities")); |
| 116 |
|
✗ |
json_object_put(json); |
| 117 |
|
✗ |
return 0; |
| 118 |
|
|
} |
| 119 |
|
|
|