ecs work
This commit is contained in:
@@ -10,7 +10,11 @@
|
||||
scenetreenode_t SCENE_TREE_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
|
||||
ecscomponent_t SCENE_TREE_COMPONENT = ecsComponentInit(
|
||||
SCENE_TREE_DATA,
|
||||
sceneTreeInit
|
||||
((ecscomponentcallbacks_t){
|
||||
.init = sceneTreeInit,
|
||||
.entityAdd = sceneTreeEntityAdded,
|
||||
.entityRemove = sceneTreeEntityRemoved
|
||||
})
|
||||
);
|
||||
|
||||
void sceneTreeInit(void) {
|
||||
@@ -22,12 +26,12 @@ void sceneTreeInit(void) {
|
||||
|
||||
ecsid_t sceneTreeParentGet(const ecsid_t child) {
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
if(!ecsComponentDataHas(&SCENE_TREE_COMPONENT, child)) return -1;
|
||||
return SCENE_TREE_DATA[child].parent;
|
||||
}
|
||||
|
||||
uint8_t sceneTreeChildGetAll(const ecsid_t id, ecsid_t *children) {
|
||||
assertTrue(id >= 0 && id < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
|
||||
if(!ecsComponentDataHas(&SCENE_TREE_COMPONENT, id)) return 0;
|
||||
if(children == NULL) return SCENE_TREE_DATA[id].childCount;
|
||||
|
||||
memoryCopy(
|
||||
@@ -40,17 +44,12 @@ uint8_t sceneTreeChildGetAll(const ecsid_t id, ecsid_t *children) {
|
||||
}
|
||||
|
||||
void sceneTreeChildAdd(const ecsid_t parent, const ecsid_t child) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
assertFalse(
|
||||
sceneTreeChildInTree(parent, child),
|
||||
"Child is already in the parent's tree"
|
||||
);
|
||||
assertFalse(
|
||||
sceneTreeChildInTree(child, parent),
|
||||
"Child cannot be a deep child of itself."
|
||||
);
|
||||
if(!ecsComponentDataHas(&SCENE_TREE_COMPONENT, parent)) {
|
||||
ecsComponentDataAdd(&SCENE_TREE_COMPONENT, parent);
|
||||
}
|
||||
if(!ecsComponentDataHas(&SCENE_TREE_COMPONENT, child)) {
|
||||
ecsComponentDataAdd(&SCENE_TREE_COMPONENT, child);
|
||||
}
|
||||
|
||||
scenetreenode_t *parentNode = &SCENE_TREE_DATA[parent];
|
||||
assertTrue(parentNode->childCount < SCENE_ITEM_CHILD_MAX, "Parent full.");
|
||||
@@ -67,10 +66,15 @@ void sceneTreeChildRemove(
|
||||
const ecsid_t parent,
|
||||
const ecsid_t child
|
||||
) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
|
||||
assertTrue(
|
||||
ecsComponentDataHas(&SCENE_TREE_COMPONENT, parent),
|
||||
"SCENE TREE ECS lacks Parent."
|
||||
);
|
||||
assertTrue(
|
||||
ecsComponentDataHas(&SCENE_TREE_COMPONENT, child),
|
||||
"SCENE TREE ECS lacks Child."
|
||||
);
|
||||
|
||||
scenetreenode_t *childNode = &SCENE_TREE_DATA[child];
|
||||
scenetreenode_t *parentNode = &SCENE_TREE_DATA[parent];
|
||||
assertTrue(childNode->parent == parent, "Child does not belong to parent");
|
||||
@@ -90,25 +94,48 @@ void sceneTreeChildRemove(
|
||||
|
||||
// Clear child's parent
|
||||
childNode->parent = -1;
|
||||
|
||||
if(childNode->childCount == 0) {
|
||||
ecsComponentDataRemove(&SCENE_TREE_COMPONENT, child);
|
||||
}
|
||||
|
||||
if(parentNode->childCount == 0) {
|
||||
ecsComponentDataRemove(&SCENE_TREE_COMPONENT, parent);
|
||||
}
|
||||
}
|
||||
|
||||
void sceneTreeChildRemoveAll(const ecsid_t parent) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
|
||||
assertTrue(
|
||||
ecsComponentDataHas(&SCENE_TREE_COMPONENT, parent),
|
||||
"SCENE TREE ECS lacks Parent."
|
||||
);
|
||||
|
||||
scenetreenode_t *parentNode = &SCENE_TREE_DATA[parent];
|
||||
for(uint8_t i = 0; i < parentNode->childCount; i++) {
|
||||
ecsid_t child = parentNode->children[i];
|
||||
SCENE_TREE_DATA[child].parent = -1;
|
||||
|
||||
if(SCENE_TREE_DATA[child].childCount == 0) {
|
||||
ecsComponentDataRemove(&SCENE_TREE_COMPONENT, child);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset child count
|
||||
parentNode->childCount = 0;
|
||||
ecsComponentDataRemove(&SCENE_TREE_COMPONENT, parent);
|
||||
}
|
||||
|
||||
bool_t sceneTreeChildInTree(
|
||||
const ecsid_t parent,
|
||||
const ecsid_t child
|
||||
) {
|
||||
assertTrue(
|
||||
ecsComponentDataHas(&SCENE_TREE_COMPONENT, parent),
|
||||
"SCENE TREE ECS lacks Parent."
|
||||
);
|
||||
assertTrue(
|
||||
ecsComponentDataHas(&SCENE_TREE_COMPONENT, child),
|
||||
"SCENE TREE ECS lacks Child."
|
||||
);
|
||||
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
@@ -121,4 +148,13 @@ bool_t sceneTreeChildInTree(
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void sceneTreeEntityAdded(const ecsid_t id) {
|
||||
SCENE_TREE_DATA[id].parent = -1; // Default parent to -1 (no parent)
|
||||
}
|
||||
|
||||
void sceneTreeEntityRemoved(const ecsid_t id) {
|
||||
if(SCENE_TREE_DATA[id].parent == -1) return;
|
||||
sceneTreeChildRemove(SCENE_TREE_DATA[id].parent, id);
|
||||
}
|
||||
@@ -82,4 +82,20 @@ void sceneTreeChildRemoveAll(const ecsid_t parent);
|
||||
* @param child The ID of the child scene item to check.
|
||||
* @return True if the child is in the parent's children, false otherwise.
|
||||
*/
|
||||
bool_t sceneTreeChildInTree(const ecsid_t parent, const ecsid_t child);
|
||||
bool_t sceneTreeChildInTree(const ecsid_t parent, const ecsid_t child);
|
||||
|
||||
/**
|
||||
* Callback for when an entity is added to the ECS.
|
||||
* This will initialize the scene tree data for the entity.
|
||||
*
|
||||
* @param id The ID of the entity being added.
|
||||
*/
|
||||
void sceneTreeEntityAdded(const ecsid_t id);
|
||||
|
||||
/**
|
||||
* Callback for when an entity is removed from the ECS.
|
||||
* This will clean up any scene tree data associated with the entity.
|
||||
*
|
||||
* @param id The ID of the entity being removed.
|
||||
*/
|
||||
void sceneTreeEntityRemoved(const ecsid_t id);
|
||||
Reference in New Issue
Block a user