potentially fixed entity release function

This commit is contained in:
jacob 2024-08-07 16:36:33 -05:00
parent 8afeea3b9a
commit 28af2c9fc9
3 changed files with 54 additions and 23 deletions

View File

@ -104,16 +104,16 @@ struct entity *entity_alloc_child(struct entity *parent)
return e; return e;
} }
void entity_release(struct entity_store *store, struct entity *ent) INTERNAL void entity_release_internal(struct entity_store *store, struct entity *ent)
{ {
/* Release children */ /* Release children */
if (ent->first.gen) { struct entity_handle first_handle = ent->first;
for (struct entity *child = entity_from_handle(store, ent->first); child->valid; child = entity_from_handle(store, child->next)) { if (first_handle.gen) {
entity_release(store, child); for (struct entity *child = entity_from_handle(store, first_handle); child->valid; child = entity_from_handle(store, child->next)) {
entity_release_internal(store, child);
} }
} }
/* Unlink */
entity_unlink(ent);
/* Release */ /* Release */
++ent->handle.gen; ++ent->handle.gen;
ent->valid = false; ent->valid = false;
@ -121,6 +121,12 @@ void entity_release(struct entity_store *store, struct entity *ent)
store->first_free = ent->handle; store->first_free = ent->handle;
} }
void entity_release(struct entity_store *store, struct entity *ent)
{
entity_unlink_parent(ent);
entity_release_internal(store, ent);
}
/* ========================== * /* ========================== *
* Query * Query
* ========================== */ * ========================== */
@ -294,13 +300,12 @@ void entity_link_parent_child(struct entity *parent, struct entity *child)
child->is_top = parent->is_root; child->is_top = parent->is_root;
} }
void entity_unlink(struct entity *ent) /* NOTE: Entity will be dangling after calling this, should re-link to root entity. */
void entity_unlink_parent(struct entity *ent)
{ {
struct entity_store *store = entity_get_store(ent); struct entity_store *store = entity_get_store(ent);
ASSERT(!entity_handle_eq(ent->handle, store->root));
struct entity_handle parent_handle = ent->parent; struct entity_handle parent_handle = ent->parent;
struct entity *parent = entity_from_handle(store, parent_handle); struct entity *parent = entity_from_handle(store, parent_handle);
struct entity *prev = entity_from_handle(store, ent->prev); struct entity *prev = entity_from_handle(store, ent->prev);
struct entity *next = entity_from_handle(store, ent->next); struct entity *next = entity_from_handle(store, ent->next);
@ -316,8 +321,17 @@ void entity_unlink(struct entity *ent)
} else { } else {
parent->last = prev->handle; parent->last = prev->handle;
} }
ent->is_top = false; ent->prev = entity_handle_nil();
ent->is_root = false; ent->next = entity_handle_nil();
}
/* NOTE: Children will be re-linked as children to their grandparent (ent's parent) */
void entity_unlink_children(struct entity *ent)
{
struct entity_store *store = entity_get_store(ent);
struct entity_handle parent_handle = ent->parent;
struct entity *parent = entity_from_handle(store, parent_handle);
/* Link children to grandparent */ /* Link children to grandparent */
struct entity_handle child_first_handle = ent->first; struct entity_handle child_first_handle = ent->first;

View File

@ -130,7 +130,7 @@ struct entity_prop_array {
* Handle helpers * Handle helpers
* ========================== */ * ========================== */
INLINE struct entity_handle entity_nil_handle(void) INLINE struct entity_handle entity_handle_nil(void)
{ {
return (struct entity_handle) { 0 }; return (struct entity_handle) { 0 };
} }
@ -212,6 +212,7 @@ struct entity *entity_find_first_match_all(struct entity_store *store, struct en
/* Tree */ /* Tree */
void entity_link_parent_child(struct entity *parent, struct entity *child); void entity_link_parent_child(struct entity *parent, struct entity *child);
void entity_unlink(struct entity *ent); void entity_unlink_parent(struct entity *ent);
void entity_unlink_children(struct entity *ent);
#endif #endif

View File

@ -114,9 +114,9 @@ INTERNAL void spawn_test_entities(void)
entity_set_xform(e, XFORM_TRS(.t = pos, .r = r, .s = size)); entity_set_xform(e, XFORM_TRS(.t = pos, .r = r, .s = size));
e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase"));
//e->sprite_span_name = STR("idle.unarmed"); e->sprite_span_name = STR("idle.unarmed");
//e->sprite_span_name = STR("idle.one_handed"); //e->sprite_span_name = STR("idle.one_handed");
e->sprite_span_name = STR("idle.two_handed"); //e->sprite_span_name = STR("idle.two_handed");
entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED); entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED);
e->player_max_speed = 4.f; e->player_max_speed = 4.f;
@ -130,28 +130,44 @@ INTERNAL void spawn_test_entities(void)
//entity_enable_prop(e, ENTITY_PROP_TEST); //entity_enable_prop(e, ENTITY_PROP_TEST);
} }
/* Child ent */ /* Child ent 1 */
{ {
struct v2 pos = V2(0, 0); struct v2 pos = V2(-0.15, -0.05);
struct v2 size = V2(1, 1); struct v2 size = V2(0.25, 0.5);
f32 r = 0; f32 r = 0;
struct xform xf = XFORM_TRS(.t = pos, .r = r, .s = size); struct xform xf = XFORM_TRS(.t = pos, .r = r, .s = size);
struct entity *e = entity_alloc_child(player_ent); struct entity *e = entity_alloc_child(player_ent);
entity_set_xform(e, xf); entity_set_local_xform(e, xf);
e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase"));
//e->sprite_span_name = STR("idle.unarmed");
//e->sprite_span_name = STR("idle.one_handed");
e->sprite_span_name = STR("idle.two_handed"); e->sprite_span_name = STR("idle.two_handed");
e->sprite_tint = RGBA_32_F(1, 0, 0, 0.5); e->sprite_tint = RGBA_32_F(0, 0, 0, 1);
entity_enable_prop(e, ENTITY_PROP_ANIMATING); entity_enable_prop(e, ENTITY_PROP_ANIMATING);
entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED); entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED);
}
//entity_enable_prop(e, ENTITY_PROP_TEST); /* Child ent 2 */
{
struct v2 pos = V2(0.15, -0.05);
struct v2 size = V2(-0.25, 0.5);
f32 r = 0;
struct xform xf = XFORM_TRS(.t = pos, .r = r, .s = size);
struct entity *e = entity_alloc_child(player_ent);
entity_set_local_xform(e, xf);
e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase"));
e->sprite_span_name = STR("idle.two_handed");
e->sprite_tint = RGBA_32_F(0, 0, 0, 1);
entity_enable_prop(e, ENTITY_PROP_ANIMATING);
entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED);
} }
/* Camera ent */ /* Camera ent */