Split overworldscene.js's player/plane/camera into their own classes
Extracts Player (extends Entity), TestPlane, and PlayerCamera into their own files; the camera now tracks the player's live position at a fixed offset instead of orbiting the world origin over time. Updates test_overworldscene.c to match the new entity order and to build an in-memory zip fixture, since overworldscene.js now require()s these sibling files and require() always resolves through ASSET.zip.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "util/memory.h"
|
||||
#include "time/time.h"
|
||||
#include "asset/asset.h"
|
||||
#include "scene/scene.h"
|
||||
#include "entity/entitymanager.h"
|
||||
#include "entity/component.h"
|
||||
@@ -18,7 +18,8 @@
|
||||
#include "display/mesh/capsule.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include "script/module/scene/modulescene.h"
|
||||
#include <math.h>
|
||||
#include "script/module/require/modulerequire.h"
|
||||
#include <zip.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -60,17 +61,93 @@ static errorret_t installOverworldScene(void) {
|
||||
);
|
||||
memoryFree(fileSrc);
|
||||
|
||||
// overworldscene.js now require()s sibling files (Player.js etc.) --
|
||||
// push the same base directory scriptManagerExecFile() would push for
|
||||
// a real load, since this test bypasses that path to exec a wrapped
|
||||
// copy of the source directly instead.
|
||||
moduleRequireDirPush("scripts/");
|
||||
errorret_t ret = scriptManagerExec(wrapped, NULL);
|
||||
moduleRequireDirPop();
|
||||
memoryFree(wrapped);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static zip_t *g_zip = NULL;
|
||||
|
||||
// overworldscene.js require()s sibling files (Player.js/TestPlane.js/
|
||||
// PlayerCamera.js), which resolve through the asset system's ASSET.zip --
|
||||
// there's no real-filesystem fallback (see assetFileInit()). Package the
|
||||
// real, shipped copies of those files (read straight off disk, not
|
||||
// hardcoded here) into an in-memory zip so require() finds the same
|
||||
// content a real build would.
|
||||
static int overworld_setup(void **state) {
|
||||
sceneInit();
|
||||
|
||||
errorret_t ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
zip_error_t err;
|
||||
zip_error_init(&err);
|
||||
|
||||
zip_source_t *write_src = zip_source_buffer_create(NULL, 0, 1, &err);
|
||||
if(!write_src) return -1;
|
||||
|
||||
zip_t *za = zip_open_from_source(write_src, ZIP_TRUNCATE, &err);
|
||||
if(!za) { zip_source_free(write_src); return -1; }
|
||||
|
||||
const char_t *requiredScripts[] = {
|
||||
"scripts/Player.js", "scripts/TestPlane.js", "scripts/PlayerCamera.js"
|
||||
};
|
||||
char_t *scriptSrcs[3];
|
||||
|
||||
for(size_t i = 0; i < 3; i++) {
|
||||
scriptSrcs[i] = readFile(requiredScripts[i]);
|
||||
zip_source_t *s = zip_source_buffer(
|
||||
za, scriptSrcs[i], strlen(scriptSrcs[i]), 0
|
||||
);
|
||||
if(zip_file_add(za, requiredScripts[i], s, ZIP_FL_OVERWRITE) < 0) {
|
||||
zip_close(za);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
zip_source_keep(write_src);
|
||||
if(zip_close(za) != 0) { zip_source_free(write_src); return -1; }
|
||||
|
||||
// zip_close() has fully read every source added above by now, so the
|
||||
// backing buffers are safe to free.
|
||||
for(size_t i = 0; i < 3; i++) memoryFree(scriptSrcs[i]);
|
||||
|
||||
zip_stat_t zs;
|
||||
memset(&zs, 0, sizeof(zs));
|
||||
if(zip_source_stat(write_src, &zs) != 0 || !(zs.valid & ZIP_STAT_SIZE)) {
|
||||
zip_source_free(write_src);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *zipbuf = malloc((size_t)zs.size);
|
||||
if(!zipbuf) { zip_source_free(write_src); return -1; }
|
||||
|
||||
if(zip_source_open(write_src) != 0) {
|
||||
free(zipbuf);
|
||||
zip_source_free(write_src);
|
||||
return -1;
|
||||
}
|
||||
zip_source_read(write_src, zipbuf, (zip_uint64_t)zs.size);
|
||||
zip_source_close(write_src);
|
||||
zip_source_free(write_src);
|
||||
|
||||
zip_error_init(&err);
|
||||
zip_source_t *read_src = zip_source_buffer_create(
|
||||
zipbuf, (zip_uint64_t)zs.size, 1, &err
|
||||
);
|
||||
if(!read_src) { free(zipbuf); return -1; }
|
||||
|
||||
g_zip = zip_open_from_source(read_src, 0, &err);
|
||||
if(!g_zip) { zip_source_free(read_src); return -1; }
|
||||
|
||||
ASSET.zip = g_zip;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -80,6 +157,9 @@ static int overworld_teardown(void **state) {
|
||||
|
||||
sceneDispose();
|
||||
|
||||
if(g_zip) { zip_close(g_zip); g_zip = NULL; }
|
||||
ASSET.zip = NULL;
|
||||
|
||||
// JerryScript defers freeing native-wrapped handles (Entity/Scene/
|
||||
// Position/Physics/Renderable instances) until GC/jerry_cleanup() runs,
|
||||
// so the leak check can only be meaningful after scriptManagerDispose()
|
||||
@@ -96,26 +176,53 @@ static void test_overworldscene_builds_expected_entities(void **state) {
|
||||
assert_true(sceneId != SCENE_ID_INVALID);
|
||||
entitymanager_t *mgr = sceneGetEntities(sceneId);
|
||||
|
||||
// Entity 0: camera. Position + Camera components, initial orbit
|
||||
// position placed with angle=0 (Time.delta defaults to 0 with no
|
||||
// timeInit()/timeUpdate() in this test), i.e. eye=(radius, height, 0).
|
||||
entityid_t camEntity = 0;
|
||||
componentid_t camPos = entityGetComponent(
|
||||
mgr, camEntity, COMPONENT_TYPE_POSITION
|
||||
// Entity 0: player (new Player() runs first in init()).
|
||||
entityid_t playerEntity = 0;
|
||||
componentid_t playerPos = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
assert_true(camPos != COMPONENT_ID_INVALID);
|
||||
assert_true(playerPos != COMPONENT_ID_INVALID);
|
||||
|
||||
vec3 playerPosition;
|
||||
entityPositionGetLocalPosition(mgr, playerEntity, playerPos, playerPosition);
|
||||
assert_float_equal(playerPosition[0], 0.0f, 0.0001f);
|
||||
assert_float_equal(playerPosition[1], 2.0f, 0.0001f);
|
||||
assert_float_equal(playerPosition[2], 0.0f, 0.0001f);
|
||||
|
||||
componentid_t playerPhysics = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_PHYSICS
|
||||
);
|
||||
assert_true(playerPhysics != COMPONENT_ID_INVALID);
|
||||
assert_int_equal(
|
||||
entityPhysicsGetBodyType(mgr, playerEntity, playerPhysics),
|
||||
PHYSICS_BODY_DYNAMIC
|
||||
);
|
||||
physicsshape_t playerShape = entityPhysicsGetShape(
|
||||
mgr, playerEntity, playerPhysics
|
||||
);
|
||||
assert_int_equal(playerShape.type, PHYSICS_SHAPE_CAPSULE);
|
||||
assert_float_equal(playerShape.data.capsule.radius, 0.5f, 0.0001f);
|
||||
assert_float_equal(playerShape.data.capsule.halfHeight, 0.5f, 0.0001f);
|
||||
|
||||
componentid_t playerRenderable = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
assert_true(playerRenderable != COMPONENT_ID_INVALID);
|
||||
entityrenderable_t *playerR = componentGetData(
|
||||
mgr, playerEntity, playerRenderable, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
assert_ptr_equal(playerR->data.material.meshes[0], &CAPSULE_MESH_SIMPLE);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.r, 0);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.g, 0);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.b, 255);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.a, 255);
|
||||
|
||||
assert_true(
|
||||
entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) !=
|
||||
entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) !=
|
||||
COMPONENT_ID_INVALID
|
||||
);
|
||||
|
||||
vec3 camPosition;
|
||||
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
|
||||
assert_float_equal(camPosition[0], 18.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[1], 10.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[2], 0.0f, 0.0001f);
|
||||
|
||||
// Entity 1: static ground plane.
|
||||
// Entity 1: static ground plane (new TestPlane() runs second).
|
||||
entityid_t planeEntity = 1;
|
||||
componentid_t planePos = entityGetComponent(
|
||||
mgr, planeEntity, COMPONENT_TYPE_POSITION
|
||||
@@ -162,83 +269,59 @@ static void test_overworldscene_builds_expected_entities(void **state) {
|
||||
assert_int_equal(planeR->data.material.material.unlit.color.b, 128);
|
||||
assert_int_equal(planeR->data.material.material.unlit.color.a, 255);
|
||||
|
||||
// Entity 2: player. Dynamic capsule body (default body type), PLAYER
|
||||
// component present.
|
||||
entityid_t playerEntity = 2;
|
||||
componentid_t playerPos = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_POSITION
|
||||
// Entity 2: camera (new PlayerCamera(player) runs last). Position +
|
||||
// Camera components, positioned at the player's local position plus
|
||||
// PlayerCamera.js's fixed offset (angle=0, radius=18, height=10) --
|
||||
// player is at (0, 2, 0), so eye=(18, 12, 0).
|
||||
entityid_t camEntity = 2;
|
||||
componentid_t camPos = entityGetComponent(
|
||||
mgr, camEntity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
assert_true(playerPos != COMPONENT_ID_INVALID);
|
||||
|
||||
vec3 playerPosition;
|
||||
entityPositionGetLocalPosition(mgr, playerEntity, playerPos, playerPosition);
|
||||
assert_float_equal(playerPosition[0], 0.0f, 0.0001f);
|
||||
assert_float_equal(playerPosition[1], 2.0f, 0.0001f);
|
||||
assert_float_equal(playerPosition[2], 0.0f, 0.0001f);
|
||||
|
||||
componentid_t playerPhysics = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_PHYSICS
|
||||
);
|
||||
assert_true(playerPhysics != COMPONENT_ID_INVALID);
|
||||
assert_int_equal(
|
||||
entityPhysicsGetBodyType(mgr, playerEntity, playerPhysics),
|
||||
PHYSICS_BODY_DYNAMIC
|
||||
);
|
||||
physicsshape_t playerShape = entityPhysicsGetShape(
|
||||
mgr, playerEntity, playerPhysics
|
||||
);
|
||||
assert_int_equal(playerShape.type, PHYSICS_SHAPE_CAPSULE);
|
||||
assert_float_equal(playerShape.data.capsule.radius, 0.5f, 0.0001f);
|
||||
assert_float_equal(playerShape.data.capsule.halfHeight, 0.5f, 0.0001f);
|
||||
|
||||
componentid_t playerRenderable = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
assert_true(playerRenderable != COMPONENT_ID_INVALID);
|
||||
entityrenderable_t *playerR = componentGetData(
|
||||
mgr, playerEntity, playerRenderable, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
assert_ptr_equal(playerR->data.material.meshes[0], &CAPSULE_MESH_SIMPLE);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.r, 0);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.g, 0);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.b, 255);
|
||||
assert_int_equal(playerR->data.material.material.unlit.color.a, 255);
|
||||
|
||||
assert_true(camPos != COMPONENT_ID_INVALID);
|
||||
assert_true(
|
||||
entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) !=
|
||||
entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) !=
|
||||
COMPONENT_ID_INVALID
|
||||
);
|
||||
|
||||
vec3 camPosition;
|
||||
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
|
||||
assert_float_equal(camPosition[0], 18.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[1], 12.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[2], 0.0f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_overworldscene_update_orbits_camera(void **state) {
|
||||
static void test_overworldscene_camera_follows_player(void **state) {
|
||||
errorret_t ret = installOverworldScene();
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
sceneid_t sceneId = sceneGetActive();
|
||||
entitymanager_t *mgr = sceneGetEntities(sceneId);
|
||||
entityid_t camEntity = 0;
|
||||
|
||||
entityid_t playerEntity = 0;
|
||||
componentid_t playerPos = entityGetComponent(
|
||||
mgr, playerEntity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
entityid_t camEntity = 2;
|
||||
componentid_t camPos = entityGetComponent(
|
||||
mgr, camEntity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
// Drive one frame with a known delta and confirm the camera orbited by
|
||||
// exactly angle = delta * speed (0.1 * 0.5 = 0.05 rad).
|
||||
TIME.delta = 0.1f;
|
||||
// Move the player and confirm the camera's next update() re-centers on
|
||||
// the player's new position at the same fixed offset (PlayerCamera.js
|
||||
// no longer orbits over time -- it just tracks the player).
|
||||
entityPositionSetLocalPosition(
|
||||
mgr, playerEntity, playerPos, (vec3){ 5.0f, 2.0f, -3.0f }
|
||||
);
|
||||
|
||||
ret = moduleSceneUpdateCurrent();
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
vec3 camPosition;
|
||||
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
|
||||
const float_t expectedAngle = 0.1f * 0.5f;
|
||||
assert_float_equal(
|
||||
camPosition[0], cosf(expectedAngle) * 18.0f, 0.0001f
|
||||
);
|
||||
assert_float_equal(camPosition[1], 10.0f, 0.0001f);
|
||||
assert_float_equal(
|
||||
camPosition[2], sinf(expectedAngle) * 18.0f, 0.0001f
|
||||
);
|
||||
|
||||
TIME.delta = 0.0f;
|
||||
assert_float_equal(camPosition[0], 5.0f + 18.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[1], 2.0f + 10.0f, 0.0001f);
|
||||
assert_float_equal(camPosition[2], -3.0f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_scene_set_switches_and_disposes(void **state) {
|
||||
@@ -309,7 +392,7 @@ int main(void) {
|
||||
overworld_setup, overworld_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_overworldscene_update_orbits_camera,
|
||||
test_overworldscene_camera_follows_player,
|
||||
overworld_setup, overworld_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
|
||||
Reference in New Issue
Block a user