Add area and flag on physics
This commit is contained in:
@@ -8,3 +8,4 @@ include(dusktest)
|
||||
# Tests
|
||||
dusktest(test_entitymanager.c)
|
||||
dusktest(test_entityposition.c)
|
||||
dusktest(test_entitytrigger.c)
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* 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/trigger/entitytrigger.h"
|
||||
#include "entity/component/trigger/entitytriggerevents.h"
|
||||
|
||||
static uint32_t CALLBACK_ENTER_COUNT;
|
||||
static uint32_t CALLBACK_ACTIVE_COUNT;
|
||||
static uint32_t CALLBACK_MOVE_COUNT;
|
||||
static uint32_t CALLBACK_LEAVE_COUNT;
|
||||
static entityid_t CALLBACK_LAST_OTHER;
|
||||
static void *CALLBACK_LAST_USER;
|
||||
|
||||
static void test_resetCallbackCounts(void) {
|
||||
CALLBACK_ENTER_COUNT = 0;
|
||||
CALLBACK_ACTIVE_COUNT = 0;
|
||||
CALLBACK_MOVE_COUNT = 0;
|
||||
CALLBACK_LEAVE_COUNT = 0;
|
||||
CALLBACK_LAST_OTHER = ENTITY_ID_INVALID;
|
||||
CALLBACK_LAST_USER = NULL;
|
||||
}
|
||||
|
||||
static void test_onEnter(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
CALLBACK_ENTER_COUNT++;
|
||||
CALLBACK_LAST_OTHER = otherEntityId;
|
||||
CALLBACK_LAST_USER = user;
|
||||
}
|
||||
|
||||
static void test_onActive(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
CALLBACK_ACTIVE_COUNT++;
|
||||
}
|
||||
|
||||
static void test_onMove(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
CALLBACK_MOVE_COUNT++;
|
||||
}
|
||||
|
||||
static void test_onLeave(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
CALLBACK_LEAVE_COUNT++;
|
||||
}
|
||||
|
||||
static void test_entityTriggerDefaultsAndAccessors(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
componentid_t trig = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
|
||||
// Defaults: 1x1x1 cube, only bit 0 of the collide mask set, no occupants.
|
||||
physicsshape_t shape = entityTriggerGetShape(&mgr, entity, trig);
|
||||
assert_int_equal(shape.type, PHYSICS_SHAPE_CUBE);
|
||||
assert_float_equal(shape.data.cube.halfExtents[0], 0.5f, 0.0001f);
|
||||
assert_int_equal(entityTriggerGetCollideMask(&mgr, entity, trig), 0x1);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, entity, trig), 0);
|
||||
assert_int_equal(
|
||||
entityTriggerGetOccupantEntityId(&mgr, entity, trig, 0),
|
||||
ENTITY_ID_INVALID
|
||||
);
|
||||
assert_false(entityTriggerIsOccupyingEntity(&mgr, entity, trig, 5));
|
||||
|
||||
physicsshape_t sphere = {
|
||||
.type = PHYSICS_SHAPE_SPHERE,
|
||||
.data.sphere.radius = 2.0f
|
||||
};
|
||||
entityTriggerSetShape(&mgr, entity, trig, sphere);
|
||||
physicsshape_t got = entityTriggerGetShape(&mgr, entity, trig);
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_SPHERE);
|
||||
assert_float_equal(got.data.sphere.radius, 2.0f, 0.0001f);
|
||||
|
||||
entityTriggerSetCollideMask(&mgr, entity, trig, 0x2);
|
||||
assert_int_equal(entityTriggerGetCollideMask(&mgr, entity, trig), 0x2);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityTriggerSerializeRoundTrip(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
componentid_t trig = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
|
||||
physicsshape_t sphere = {
|
||||
.type = PHYSICS_SHAPE_SPHERE,
|
||||
.data.sphere.radius = 3.0f
|
||||
};
|
||||
entityTriggerSetShape(&mgr, entity, trig, sphere);
|
||||
entityTriggerSetCollideMask(&mgr, entity, trig, 0x4);
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
assert_true(
|
||||
errorIsOk(entityTriggerSerialize(&mgr, entity, trig, doc, root))
|
||||
);
|
||||
|
||||
size_t len = 0;
|
||||
char_t *jsonStr = yyjson_mut_write(doc, 0, &len);
|
||||
assert_non_null(jsonStr);
|
||||
yyjson_mut_doc_free(doc);
|
||||
|
||||
yyjson_doc *readDoc = yyjson_read(jsonStr, len, 0);
|
||||
free(jsonStr);
|
||||
assert_non_null(readDoc);
|
||||
|
||||
entityid_t entity2 = entityManagerAdd(&mgr);
|
||||
componentid_t trig2 = entityAddComponent(
|
||||
&mgr, entity2, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
assert_true(errorIsOk(entityTriggerDeserialize(
|
||||
&mgr, entity2, trig2, yyjson_doc_get_root(readDoc)
|
||||
)));
|
||||
yyjson_doc_free(readDoc);
|
||||
|
||||
physicsshape_t got = entityTriggerGetShape(&mgr, entity2, trig2);
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_SPHERE);
|
||||
assert_float_equal(got.data.sphere.radius, 3.0f, 0.0001f);
|
||||
assert_int_equal(entityTriggerGetCollideMask(&mgr, entity2, trig2), 0x4);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityTriggerEventSubscription(void **state) {
|
||||
test_resetCallbackCounts();
|
||||
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
componentid_t trig = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
|
||||
int32_t userValue = 42;
|
||||
entityTriggerOnEnterAdd(&mgr, entity, trig, test_onEnter, &userValue);
|
||||
entityTriggerOnActiveAdd(&mgr, entity, trig, test_onActive, NULL);
|
||||
entityTriggerOnMoveAdd(&mgr, entity, trig, test_onMove, NULL);
|
||||
entityTriggerOnLeaveAdd(&mgr, entity, trig, test_onLeave, NULL);
|
||||
|
||||
entityTriggerFireEnter(&mgr, entity, trig, 7, 0);
|
||||
assert_int_equal(CALLBACK_ENTER_COUNT, 1);
|
||||
assert_int_equal(CALLBACK_LAST_OTHER, 7);
|
||||
assert_ptr_equal(CALLBACK_LAST_USER, &userValue);
|
||||
|
||||
entityTriggerFireActive(&mgr, entity, trig, 7, 0);
|
||||
assert_int_equal(CALLBACK_ACTIVE_COUNT, 1);
|
||||
|
||||
entityTriggerFireMove(&mgr, entity, trig, 7, 0);
|
||||
assert_int_equal(CALLBACK_MOVE_COUNT, 1);
|
||||
|
||||
entityTriggerFireLeave(&mgr, entity, trig, 7, 0);
|
||||
assert_int_equal(CALLBACK_LEAVE_COUNT, 1);
|
||||
|
||||
// After removing, the callback no longer fires.
|
||||
entityTriggerOnEnterRemove(&mgr, entity, trig, test_onEnter);
|
||||
entityTriggerFireEnter(&mgr, entity, trig, 8, 0);
|
||||
assert_int_equal(CALLBACK_ENTER_COUNT, 1);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_entityTriggerDefaultsAndAccessors),
|
||||
cmocka_unit_test(test_entityTriggerSerializeRoundTrip),
|
||||
cmocka_unit_test(test_entityTriggerEventSubscription),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -9,3 +9,5 @@ include(dusktest)
|
||||
dusktest(test_physicstest.c)
|
||||
dusktest(test_physicsworld.c)
|
||||
dusktest(test_physicsshapemesh.c)
|
||||
dusktest(test_physicsshape.c)
|
||||
dusktest(test_triggersystem.c)
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 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 "physics/physicsshape.h"
|
||||
|
||||
static yyjson_val *test_roundTripToImmutable(
|
||||
yyjson_mut_doc *doc,
|
||||
yyjson_mut_val *root,
|
||||
yyjson_doc **outReadDoc
|
||||
) {
|
||||
size_t len = 0;
|
||||
char_t *jsonStr = yyjson_mut_write(doc, 0, &len);
|
||||
assert_non_null(jsonStr);
|
||||
yyjson_mut_doc_free(doc);
|
||||
|
||||
*outReadDoc = yyjson_read(jsonStr, len, 0);
|
||||
free(jsonStr);
|
||||
assert_non_null(*outReadDoc);
|
||||
return yyjson_doc_get_root(*outReadDoc);
|
||||
}
|
||||
|
||||
static void test_physicsShapeCubeRoundTrip(void **state) {
|
||||
physicsshape_t shape = {
|
||||
.type = PHYSICS_SHAPE_CUBE,
|
||||
.data.cube.halfExtents = { 1.0f, 2.0f, 3.0f }
|
||||
};
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
assert_true(errorIsOk(physicsShapeSerialize(doc, root, &shape)));
|
||||
|
||||
yyjson_doc *readDoc;
|
||||
yyjson_val *root2 = test_roundTripToImmutable(doc, root, &readDoc);
|
||||
|
||||
physicsshape_t got;
|
||||
assert_true(errorIsOk(
|
||||
physicsShapeDeserialize(yyjson_obj_get(root2, "shape"), &got)
|
||||
));
|
||||
yyjson_doc_free(readDoc);
|
||||
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_CUBE);
|
||||
assert_float_equal(got.data.cube.halfExtents[0], 1.0f, 0.0001f);
|
||||
assert_float_equal(got.data.cube.halfExtents[1], 2.0f, 0.0001f);
|
||||
assert_float_equal(got.data.cube.halfExtents[2], 3.0f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_physicsShapeSphereRoundTrip(void **state) {
|
||||
physicsshape_t shape = {
|
||||
.type = PHYSICS_SHAPE_SPHERE,
|
||||
.data.sphere.radius = 4.5f
|
||||
};
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
assert_true(errorIsOk(physicsShapeSerialize(doc, root, &shape)));
|
||||
|
||||
yyjson_doc *readDoc;
|
||||
yyjson_val *root2 = test_roundTripToImmutable(doc, root, &readDoc);
|
||||
|
||||
physicsshape_t got;
|
||||
assert_true(errorIsOk(
|
||||
physicsShapeDeserialize(yyjson_obj_get(root2, "shape"), &got)
|
||||
));
|
||||
yyjson_doc_free(readDoc);
|
||||
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_SPHERE);
|
||||
assert_float_equal(got.data.sphere.radius, 4.5f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_physicsShapeCapsuleRoundTrip(void **state) {
|
||||
physicsshape_t shape = {
|
||||
.type = PHYSICS_SHAPE_CAPSULE,
|
||||
.data.capsule = { .radius = 0.5f, .halfHeight = 1.5f }
|
||||
};
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
assert_true(errorIsOk(physicsShapeSerialize(doc, root, &shape)));
|
||||
|
||||
yyjson_doc *readDoc;
|
||||
yyjson_val *root2 = test_roundTripToImmutable(doc, root, &readDoc);
|
||||
|
||||
physicsshape_t got;
|
||||
assert_true(errorIsOk(
|
||||
physicsShapeDeserialize(yyjson_obj_get(root2, "shape"), &got)
|
||||
));
|
||||
yyjson_doc_free(readDoc);
|
||||
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_CAPSULE);
|
||||
assert_float_equal(got.data.capsule.radius, 0.5f, 0.0001f);
|
||||
assert_float_equal(got.data.capsule.halfHeight, 1.5f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_physicsShapePlaneRoundTrip(void **state) {
|
||||
physicsshape_t shape = {
|
||||
.type = PHYSICS_SHAPE_PLANE,
|
||||
.data.plane = { .normal = { 0.0f, 1.0f, 0.0f }, .distance = 2.0f }
|
||||
};
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
assert_true(errorIsOk(physicsShapeSerialize(doc, root, &shape)));
|
||||
|
||||
yyjson_doc *readDoc;
|
||||
yyjson_val *root2 = test_roundTripToImmutable(doc, root, &readDoc);
|
||||
|
||||
physicsshape_t got;
|
||||
assert_true(errorIsOk(
|
||||
physicsShapeDeserialize(yyjson_obj_get(root2, "shape"), &got)
|
||||
));
|
||||
yyjson_doc_free(readDoc);
|
||||
|
||||
assert_int_equal(got.type, PHYSICS_SHAPE_PLANE);
|
||||
assert_float_equal(got.data.plane.normal[1], 1.0f, 0.0001f);
|
||||
assert_float_equal(got.data.plane.distance, 2.0f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_physicsShapeDeserializeUnknownTypeFails(void **state) {
|
||||
const char_t *json = "{\"shape\":{\"type\":\"NOT_A_SHAPE\"}}";
|
||||
yyjson_doc *readDoc = yyjson_read(json, strlen(json), YYJSON_READ_NOFLAG);
|
||||
assert_non_null(readDoc);
|
||||
|
||||
physicsshape_t got;
|
||||
errorret_t ret = physicsShapeDeserialize(
|
||||
yyjson_obj_get(yyjson_doc_get_root(readDoc), "shape"), &got
|
||||
);
|
||||
assert_true(errorIsNotOk(ret));
|
||||
errorCatch(ret);
|
||||
|
||||
yyjson_doc_free(readDoc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_physicsShapeCubeRoundTrip),
|
||||
cmocka_unit_test(test_physicsShapeSphereRoundTrip),
|
||||
cmocka_unit_test(test_physicsShapeCapsuleRoundTrip),
|
||||
cmocka_unit_test(test_physicsShapePlaneRoundTrip),
|
||||
cmocka_unit_test(test_physicsShapeDeserializeUnknownTypeFails),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -191,6 +191,49 @@ static void test_physicsWorldDynamicVsDynamicSeparates(void **state) {
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_physicsWorldCollideMaskPreventsCollision(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
physicsworld_t world;
|
||||
physicsWorldInit(&world);
|
||||
|
||||
entityid_t entityA = entityManagerAdd(&mgr);
|
||||
componentid_t posA = entityAddComponent(
|
||||
&mgr, entityA, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
componentid_t physA = entityAddComponent(
|
||||
&mgr, entityA, COMPONENT_TYPE_PHYSICS
|
||||
);
|
||||
vec3 posAStart = { 0.0f, 0.0f, 0.3f };
|
||||
entityPositionSetLocalPosition(&mgr, entityA, posA, posAStart);
|
||||
entityPhysicsGet(&mgr, entityA, physA)->gravityScale = 0.0f;
|
||||
entityPhysicsSetCollideMask(&mgr, entityA, physA, 0x1);
|
||||
|
||||
entityid_t entityB = entityManagerAdd(&mgr);
|
||||
componentid_t posB = entityAddComponent(
|
||||
&mgr, entityB, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
componentid_t physB = entityAddComponent(
|
||||
&mgr, entityB, COMPONENT_TYPE_PHYSICS
|
||||
);
|
||||
vec3 posBStart = { 0.0f, 0.0f, -0.3f };
|
||||
entityPositionSetLocalPosition(&mgr, entityB, posB, posBStart);
|
||||
entityPhysicsGet(&mgr, entityB, physB)->gravityScale = 0.0f;
|
||||
entityPhysicsSetCollideMask(&mgr, entityB, physB, 0x2);
|
||||
|
||||
// Overlapping by 0.4 on Z, but masks share no bits -- no separation.
|
||||
physicsWorldStep(&world, &mgr, 1.0f / 60.0f);
|
||||
|
||||
vec3 finalA, finalB;
|
||||
entityPositionGetWorldPosition(&mgr, entityA, posA, finalA);
|
||||
entityPositionGetWorldPosition(&mgr, entityB, posB, finalB);
|
||||
assert_float_equal(finalA[2], posAStart[2], 0.0001f);
|
||||
assert_float_equal(finalB[2], posBStart[2], 0.0001f);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_physicsWorldStepNoPhysicsEntitiesIsNoop(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
@@ -210,6 +253,7 @@ int main(int argc, char **argv) {
|
||||
cmocka_unit_test(test_physicsWorldGravityIntegratesDynamicBody),
|
||||
cmocka_unit_test(test_physicsWorldRestsOnStaticFloor),
|
||||
cmocka_unit_test(test_physicsWorldDynamicVsDynamicSeparates),
|
||||
cmocka_unit_test(test_physicsWorldCollideMaskPreventsCollision),
|
||||
cmocka_unit_test(test_physicsWorldStepNoPhysicsEntitiesIsNoop),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* 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"
|
||||
#include "entity/component/physics/entityphysics.h"
|
||||
#include "entity/component/trigger/entitytrigger.h"
|
||||
#include "entity/component/trigger/entitytriggerevents.h"
|
||||
#include "physics/triggersystem.h"
|
||||
|
||||
static uint32_t TS_ENTER_COUNT;
|
||||
static uint32_t TS_ACTIVE_COUNT;
|
||||
static uint32_t TS_MOVE_COUNT;
|
||||
static uint32_t TS_LEAVE_COUNT;
|
||||
|
||||
static void test_resetCounts(void) {
|
||||
TS_ENTER_COUNT = 0;
|
||||
TS_ACTIVE_COUNT = 0;
|
||||
TS_MOVE_COUNT = 0;
|
||||
TS_LEAVE_COUNT = 0;
|
||||
}
|
||||
|
||||
static void ts_onEnter(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
TS_ENTER_COUNT++;
|
||||
}
|
||||
|
||||
static void ts_onActive(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
TS_ACTIVE_COUNT++;
|
||||
}
|
||||
|
||||
static void ts_onMove(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
TS_MOVE_COUNT++;
|
||||
}
|
||||
|
||||
static void ts_onLeave(
|
||||
entitymanager_t *mgr,
|
||||
const entityid_t triggerEntityId,
|
||||
const componentid_t triggerComponentId,
|
||||
const entityid_t otherEntityId,
|
||||
const componentid_t otherComponentId,
|
||||
void *user
|
||||
) {
|
||||
TS_LEAVE_COUNT++;
|
||||
}
|
||||
|
||||
static void test_triggerSystemLifecycle(void **state) {
|
||||
test_resetCounts();
|
||||
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t trigEntity = entityManagerAdd(&mgr);
|
||||
entityAddComponent(&mgr, trigEntity, COMPONENT_TYPE_POSITION);
|
||||
componentid_t trig = entityAddComponent(
|
||||
&mgr, trigEntity, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
entityTriggerOnEnterAdd(&mgr, trigEntity, trig, ts_onEnter, NULL);
|
||||
entityTriggerOnActiveAdd(&mgr, trigEntity, trig, ts_onActive, NULL);
|
||||
entityTriggerOnMoveAdd(&mgr, trigEntity, trig, ts_onMove, NULL);
|
||||
entityTriggerOnLeaveAdd(&mgr, trigEntity, trig, ts_onLeave, NULL);
|
||||
|
||||
entityid_t otherEntity = entityManagerAdd(&mgr);
|
||||
componentid_t otherPos = entityAddComponent(
|
||||
&mgr, otherEntity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
entityAddComponent(&mgr, otherEntity, COMPONENT_TYPE_PHYSICS);
|
||||
|
||||
// Starts far away: no overlap, no events.
|
||||
vec3 farPos = { 10.0f, 0.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, otherEntity, otherPos, farPos);
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ENTER_COUNT, 0);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, trigEntity, trig), 0);
|
||||
|
||||
// Moves fully inside: onEnter fires once, not onActive.
|
||||
vec3 insidePos = { 0.0f, 0.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, otherEntity, otherPos, insidePos);
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ENTER_COUNT, 1);
|
||||
assert_int_equal(TS_ACTIVE_COUNT, 0);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, trigEntity, trig), 1);
|
||||
assert_true(
|
||||
entityTriggerIsOccupyingEntity(&mgr, trigEntity, trig, otherEntity)
|
||||
);
|
||||
|
||||
// Stays put: onActive fires, no onMove (position unchanged).
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ENTER_COUNT, 1);
|
||||
assert_int_equal(TS_ACTIVE_COUNT, 1);
|
||||
assert_int_equal(TS_MOVE_COUNT, 0);
|
||||
|
||||
// Shifts slightly but still overlapping: onActive + onMove both fire.
|
||||
vec3 shiftedPos = { 0.1f, 0.0f, 0.0f };
|
||||
entityPositionSetLocalPosition(&mgr, otherEntity, otherPos, shiftedPos);
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ACTIVE_COUNT, 2);
|
||||
assert_int_equal(TS_MOVE_COUNT, 1);
|
||||
|
||||
// Leaves: onLeave fires, occupant list empties.
|
||||
entityPositionSetLocalPosition(&mgr, otherEntity, otherPos, farPos);
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_LEAVE_COUNT, 1);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, trigEntity, trig), 0);
|
||||
assert_false(
|
||||
entityTriggerIsOccupyingEntity(&mgr, trigEntity, trig, otherEntity)
|
||||
);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_triggerSystemCollideMaskFilter(void **state) {
|
||||
test_resetCounts();
|
||||
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t trigEntity = entityManagerAdd(&mgr);
|
||||
entityAddComponent(&mgr, trigEntity, COMPONENT_TYPE_POSITION);
|
||||
componentid_t trig = entityAddComponent(
|
||||
&mgr, trigEntity, COMPONENT_TYPE_TRIGGER
|
||||
);
|
||||
entityTriggerSetCollideMask(&mgr, trigEntity, trig, 0x1);
|
||||
entityTriggerOnEnterAdd(&mgr, trigEntity, trig, ts_onEnter, NULL);
|
||||
|
||||
entityid_t otherEntity = entityManagerAdd(&mgr);
|
||||
entityAddComponent(&mgr, otherEntity, COMPONENT_TYPE_POSITION);
|
||||
componentid_t otherPhys = entityAddComponent(
|
||||
&mgr, otherEntity, COMPONENT_TYPE_PHYSICS
|
||||
);
|
||||
entityPhysicsSetCollideMask(&mgr, otherEntity, otherPhys, 0x2);
|
||||
|
||||
// Fully overlapping (both default to the origin), but masks share no
|
||||
// bits -- must not be detected.
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ENTER_COUNT, 0);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, trigEntity, trig), 0);
|
||||
|
||||
// Sharing bit 0x1 now -- detected.
|
||||
entityPhysicsSetCollideMask(&mgr, otherEntity, otherPhys, 0x3);
|
||||
triggerSystemStep(&mgr);
|
||||
assert_int_equal(TS_ENTER_COUNT, 1);
|
||||
assert_int_equal(entityTriggerGetOccupantCount(&mgr, trigEntity, trig), 1);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_triggerSystemStepNoTriggersIsNoop(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityManagerAdd(&mgr);
|
||||
triggerSystemStep(&mgr);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_triggerSystemLifecycle),
|
||||
cmocka_unit_test(test_triggerSystemCollideMaskFilter),
|
||||
cmocka_unit_test(test_triggerSystemStepNoTriggersIsNoop),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user