176 lines
5.9 KiB
C
176 lines
5.9 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "dusktest.h"
|
|
#include "util/memory.h"
|
|
#include "entity/entitymanager.h"
|
|
#include "entity/component/display/entityposition.h"
|
|
|
|
static void test_entityPositionLocalGetSet(void **state) {
|
|
entitymanager_t mgr;
|
|
entityManagerInit(&mgr);
|
|
|
|
entityid_t entity = entityManagerAdd(&mgr);
|
|
componentid_t comp = entityAddComponent(
|
|
&mgr, entity, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
vec3 setPos = { 1.0f, 2.0f, 3.0f };
|
|
entityPositionSetLocalPosition(&mgr, entity, comp, setPos);
|
|
|
|
vec3 getPos;
|
|
entityPositionGetLocalPosition(&mgr, entity, comp, getPos);
|
|
assert_float_equal(getPos[0], 1.0f, 0.0001f);
|
|
assert_float_equal(getPos[1], 2.0f, 0.0001f);
|
|
assert_float_equal(getPos[2], 3.0f, 0.0001f);
|
|
|
|
// World position of a parentless entity matches local position.
|
|
vec3 worldPos;
|
|
entityPositionGetWorldPosition(&mgr, entity, comp, worldPos);
|
|
assert_float_equal(worldPos[0], 1.0f, 0.0001f);
|
|
assert_float_equal(worldPos[1], 2.0f, 0.0001f);
|
|
assert_float_equal(worldPos[2], 3.0f, 0.0001f);
|
|
|
|
entityManagerDispose(&mgr);
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_entityPositionParentChildWorldTransform(void **state) {
|
|
entitymanager_t mgr;
|
|
entityManagerInit(&mgr);
|
|
|
|
entityid_t parent = entityManagerAdd(&mgr);
|
|
componentid_t parentComp = entityAddComponent(
|
|
&mgr, parent, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
entityid_t child = entityManagerAdd(&mgr);
|
|
componentid_t childComp = entityAddComponent(
|
|
&mgr, child, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
vec3 parentPos = { 10.0f, 0.0f, 0.0f };
|
|
entityPositionSetLocalPosition(&mgr, parent, parentComp, parentPos);
|
|
|
|
vec3 childLocalPos = { 1.0f, 0.0f, 0.0f };
|
|
entityPositionSetLocalPosition(&mgr, child, childComp, childLocalPos);
|
|
|
|
entityPositionSetParent(&mgr, child, childComp, parent, parentComp);
|
|
|
|
vec3 childWorldPos;
|
|
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
|
assert_float_equal(childWorldPos[0], 11.0f, 0.0001f);
|
|
assert_float_equal(childWorldPos[1], 0.0f, 0.0001f);
|
|
assert_float_equal(childWorldPos[2], 0.0f, 0.0001f);
|
|
|
|
// Moving the parent should lazily update the child's world position too.
|
|
vec3 parentPosMoved = { 20.0f, 5.0f, 0.0f };
|
|
entityPositionSetLocalPosition(&mgr, parent, parentComp, parentPosMoved);
|
|
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
|
assert_float_equal(childWorldPos[0], 21.0f, 0.0001f);
|
|
assert_float_equal(childWorldPos[1], 5.0f, 0.0001f);
|
|
assert_float_equal(childWorldPos[2], 0.0f, 0.0001f);
|
|
|
|
// Detaching does not preserve world position -- the child's local
|
|
// position (never touched since entityPositionSetLocalPosition above) is
|
|
// now its world position too, since it no longer has a parent.
|
|
entityPositionSetParent(
|
|
&mgr, child, childComp, ENTITY_ID_INVALID, COMPONENT_ID_INVALID
|
|
);
|
|
vec3 parentPosMovedAgain = { 100.0f, 100.0f, 100.0f };
|
|
entityPositionSetLocalPosition(
|
|
&mgr, parent, parentComp, parentPosMovedAgain
|
|
);
|
|
entityPositionGetWorldPosition(&mgr, child, childComp, childWorldPos);
|
|
assert_float_equal(childWorldPos[0], childLocalPos[0], 0.0001f);
|
|
assert_float_equal(childWorldPos[1], childLocalPos[1], 0.0001f);
|
|
assert_float_equal(childWorldPos[2], childLocalPos[2], 0.0001f);
|
|
|
|
entityManagerDispose(&mgr);
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_entityPositionDeserializeLookAt(void **state) {
|
|
entitymanager_t mgr;
|
|
entityManagerInit(&mgr);
|
|
|
|
entityid_t entity = entityManagerAdd(&mgr);
|
|
componentid_t comp = entityAddComponent(
|
|
&mgr, entity, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
const char_t *json =
|
|
"{ \"x\": 0, \"y\": 5, \"z\": 10, "
|
|
"\"lookAt\": { \"x\": 0, \"y\": 0, \"z\": 0 } }";
|
|
yyjson_doc *doc = yyjson_read(json, strlen(json), YYJSON_READ_NOFLAG);
|
|
assert_non_null(doc);
|
|
|
|
errorret_t ret = entityPositionDeserialize(
|
|
&mgr, entity, comp, yyjson_doc_get_root(doc)
|
|
);
|
|
yyjson_doc_free(doc);
|
|
assert_true(errorIsOk(ret));
|
|
|
|
// The eye point decoded back out of localTransform should match "x"/"y"/"z".
|
|
vec3 eye;
|
|
entityPositionGetLocalPosition(&mgr, entity, comp, eye);
|
|
assert_float_equal(eye[0], 0.0f, 0.0001f);
|
|
assert_float_equal(eye[1], 5.0f, 0.0001f);
|
|
assert_float_equal(eye[2], 10.0f, 0.0001f);
|
|
|
|
// Forward (-local Z, see entityCameraGetForward's convention) should
|
|
// point from eye toward the lookAt target (the origin here).
|
|
mat4 transform;
|
|
entityPositionGetLocalTransform(&mgr, entity, comp, transform);
|
|
vec3 forward = { -transform[2][0], -transform[2][1], -transform[2][2] };
|
|
glm_vec3_normalize(forward);
|
|
|
|
vec3 toTarget = { -eye[0], -eye[1], -eye[2] };
|
|
glm_vec3_normalize(toTarget);
|
|
|
|
assert_float_equal(glm_vec3_dot(forward, toTarget), 1.0f, 0.001f);
|
|
|
|
entityManagerDispose(&mgr);
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
static void test_entityPositionDisposeDeep(void **state) {
|
|
entitymanager_t mgr;
|
|
entityManagerInit(&mgr);
|
|
|
|
entityid_t parent = entityManagerAdd(&mgr);
|
|
componentid_t parentComp = entityAddComponent(
|
|
&mgr, parent, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
entityid_t child = entityManagerAdd(&mgr);
|
|
componentid_t childComp = entityAddComponent(
|
|
&mgr, child, COMPONENT_TYPE_POSITION
|
|
);
|
|
|
|
entityPositionSetParent(&mgr, child, childComp, parent, parentComp);
|
|
|
|
entityPositionDisposeDeep(&mgr, parent, parentComp);
|
|
|
|
assert_true((mgr.entities[parent].state & ENTITY_STATE_ACTIVE) == 0);
|
|
assert_true((mgr.entities[child].state & ENTITY_STATE_ACTIVE) == 0);
|
|
|
|
entityManagerDispose(&mgr);
|
|
assert_int_equal(memoryGetAllocatedCount(), 0);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test(test_entityPositionLocalGetSet),
|
|
cmocka_unit_test(test_entityPositionParentChildWorldTransform),
|
|
cmocka_unit_test(test_entityPositionDeserializeLookAt),
|
|
cmocka_unit_test(test_entityPositionDisposeDeep),
|
|
};
|
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
}
|