From da2f00aea405d2e0a5763ee07e709c82a07ea8ba Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Mon, 9 Mar 2020 12:02:55 +0100
Subject: [PATCH] Adding prefabs id
---
include/engine.h | 1 -
include/entity.h | 1 +
include/scene.h | 4 ++--
src/components/map_managment.c | 2 +-
src/deserializer/prefab.c | 8 ++++----
src/scene/scene.c | 2 ++
src/scene/scene_loader.c | 6 ++++--
7 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/include/engine.h b/include/engine.h
index db8781a..f5ad8ac 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -90,7 +90,6 @@ int engine_use_sfml(gc_engine *engine, const char *title, int framerate);
gc_vector2 engine_get_cursor_pos(gc_engine *engine);
-#define GETSYS(x) ((struct x *)engine->get_system(engine, #x))
#define GETSYS(engine, x) ((struct x *)engine->get_system(engine, #x))
#endif
\ No newline at end of file
diff --git a/include/entity.h b/include/entity.h
index 5f8cfa6..4e7ece3 100644
--- a/include/entity.h
+++ b/include/entity.h
@@ -16,6 +16,7 @@ typedef struct gc_entity gc_entity;
struct gc_entity
{
int id;
+ int prefab_id;
gc_component *components;
gc_entity *(*add_component)(gc_entity *entity, void *component);
void *(*get_component)(const gc_entity *entity, const char *name);
diff --git a/include/scene.h b/include/scene.h
index a774c6e..ccacd14 100644
--- a/include/scene.h
+++ b/include/scene.h
@@ -27,7 +27,7 @@ struct gc_scene
int (*add_entity)(gc_scene *scene, gc_entity *entity);
gc_entity *(*get_entity)(gc_scene *scene, int id);
gc_list *(*get_entity_by_cmp)(gc_scene *scene, const char *cmp_name);
- void (*load_entity)(gc_scene *this, gc_engine *engine, node *xml);
+ void (*load_entity)(gc_scene *this, gc_engine *engine, node *xml, int pref);
gc_list *callbacks;
callback_t (*get_callback)(gc_scene *this, char *name);
@@ -39,4 +39,4 @@ void scene_load_data(gc_engine *engine, gc_scene *scene, node *n);
void *scene_get_data(gc_scene *scene, const char *type, const char *name);
void scene_add_callback(gc_scene *scene, const char *name, void *func);
void scene_destroy(gc_scene *scene);
-void scene_load_entity(gc_scene *this, gc_engine *engine, node *n);
\ No newline at end of file
+void scene_load_entity(gc_scene *this, gc_engine *engine, node *n, int prefab);
\ No newline at end of file
diff --git a/src/components/map_managment.c b/src/components/map_managment.c
index a56b224..cbbbead 100644
--- a/src/components/map_managment.c
+++ b/src/components/map_managment.c
@@ -131,5 +131,5 @@ bool map_manage_click(gc_engine *engine, int id, gc_vector2 pos)
ret->corners[3]->z += 10;
ret->texture = engine->scene->get_data(scene, "sprite", "command_block");
}
- return (true);
+ return (false);
}
\ No newline at end of file
diff --git a/src/deserializer/prefab.c b/src/deserializer/prefab.c
index 423f45e..9dbe80e 100644
--- a/src/deserializer/prefab.c
+++ b/src/deserializer/prefab.c
@@ -9,10 +9,7 @@
#include "engine.h"
#include "entity.h"
#include "prefab.h"
-#include "read_line.h"
#include "utility.h"
-#include
-#include
int prefab_load(gc_engine *engine, const char *path)
{
@@ -30,20 +27,23 @@ int prefab_load(gc_engine *engine, const char *path)
int prefab_loadentities(node *n, gc_engine *engine, gc_scene *scene)
{
gc_entity *entity;
+ static int prefab_id = 0;
n = xml_getnode(n, "gc_entities");
if (!n)
return (-1);
for (node *ent_n = n->child; ent_n; ent_n = ent_n->next) {
if (my_strcmp(ent_n->name, "gc_entity")) {
- scene->load_entity(scene, engine, ent_n);
+ scene->load_entity(scene, engine, ent_n, prefab_id);
continue;
}
entity = deserialize_entity(engine, scene, ent_n);
if (!entity)
return (-1);
+ entity->prefab_id = prefab_id;
scene->add_entity(scene, entity);
}
+ prefab_id++;
if (engine->on_resize && engine->get_screen_size && engine->scene)
engine->on_resize(engine, engine->get_screen_size(engine));
return (0);
diff --git a/src/scene/scene.c b/src/scene/scene.c
index 06645dd..51ab239 100644
--- a/src/scene/scene.c
+++ b/src/scene/scene.c
@@ -77,6 +77,8 @@ int change_scene(gc_engine *engine, gc_scene *scene)
callback_t scene_get_callback(gc_scene *this, char *name)
{
+ if (!name)
+ return (NULL);
for (gc_list *cal = this->callbacks; cal; cal = cal->next) {
if (!my_strcmp(((gc_data *)cal->data)->name, name))
return (((gc_data *)cal->data)->custom);
diff --git a/src/scene/scene_loader.c b/src/scene/scene_loader.c
index 367390c..c19e309 100644
--- a/src/scene/scene_loader.c
+++ b/src/scene/scene_loader.c
@@ -38,7 +38,7 @@ void scene_load_data(gc_engine *engine, gc_scene *scene, node *n)
}
}
-void scene_load_entity(gc_scene *this, gc_engine *engine, node *n)
+void scene_load_entity(gc_scene *this, gc_engine *engine, node *n, int prefab)
{
gc_dataloader *loader = engine->get_dataloader(engine, n->name);
gc_data *data;
@@ -48,6 +48,8 @@ void scene_load_entity(gc_scene *this, gc_engine *engine, node *n)
return;
}
data = loader->load(engine, this, n);
- for (gc_list *li = (gc_list *)data->custom; li; li = li->next)
+ for (gc_list *li = (gc_list *)data->custom; li; li = li->next) {
+ ((gc_entity *)li->data)->prefab_id = prefab;
this->add_entity(this, li->data);
+ }
}
\ No newline at end of file