Files
dusk/test/entity/test_entitytrigger.c
T
YourWishes e61914bad4 Finish JerryScript Entity/Scene wiring; remove JSON serialize/deserialize
Register Entity, the generic Component wrapper, and Scene as JerryScript
classes (modulelist.c ties them together, plus their .d.ts stubs), so
scenes/entities can be built from script going forward instead of the
JSON scene format.

With scripting now the intended authoring path, drop the JSON
serialize/deserialize machinery entirely: componentdefinition_t's
serialize/deserialize callbacks, every component's *Serialize/
*Deserialize function, entitySerialize/entityDeserialize,
sceneSerialize/sceneDeserialize, and the "prefabs/<name>.json"/
"scenes/<name>.json" asset fallback in entityPrefabResolveAndApply/
scenePrefabResolveAndApply (C-coded prefabs only now). physicsshape.c
and its test are removed outright since they only existed for this.
game.c's test scene now comes from the existing overworldSceneCreate()
C builder instead of loading the now-deleted assets/scenes/test.json.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-30 11:19:08 -05:00

161 lines
4.7 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/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_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_entityTriggerEventSubscription),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}