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:
2026-08-03 19:35:54 -05:00
parent 06bc4fcd55
commit b9d2fe60fd
5 changed files with 234 additions and 139 deletions
+6 -8
View File
@@ -3,23 +3,21 @@
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
// Player: dynamic capsule body, moved relative to the camera by PLAYER's class Player extends Entity {
// own update callback (see entityplayer.c).
class Player {
constructor() { constructor() {
this.entity = new Entity(); super();
this.position = this.entity.add(POSITION); this.position = this.add(POSITION);
this.position.setLocalPosition(0.0, 2.0, 0.0); this.position.setLocalPosition(0.0, 2.0, 0.0);
this.physics = this.entity.add(PHYSICS); this.physics = this.add(PHYSICS);
this.physics.setShape(PHYSICS_SHAPE_CAPSULE, 0.5, 0.5); this.physics.setShape(PHYSICS_SHAPE_CAPSULE, 0.5, 0.5);
this.renderable = this.entity.add(RENDERABLE); this.renderable = this.add(RENDERABLE);
this.renderable.setMesh(0, MESH_CAPSULE); this.renderable.setMesh(0, MESH_CAPSULE);
this.renderable.setColor(0, 0, 255, 255); this.renderable.setColor(0, 0, 255, 255);
this.entity.add(PLAYER); this.add(PLAYER);
} }
} }
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
var CAMERA_OFFSET_ANGLE = 0.0;
var CAMERA_OFFSET_RADIUS = 18.0;
var CAMERA_OFFSET_HEIGHT = 10.0;
class PlayerCamera {
constructor(target) {
this.target = target;
this.entity = new Entity();
this.position = this.entity.add(POSITION);
this.entity.add(CAMERA);
this.update();
}
update() {
var targetPosition = this.target.position.getLocalPosition();
var eyeX = targetPosition.x + Math.cos(CAMERA_OFFSET_ANGLE) *
CAMERA_OFFSET_RADIUS;
var eyeY = targetPosition.y + CAMERA_OFFSET_HEIGHT;
var eyeZ = targetPosition.z + Math.sin(CAMERA_OFFSET_ANGLE) *
CAMERA_OFFSET_RADIUS;
this.position.lookAt(
eyeX, eyeY, eyeZ,
targetPosition.x, targetPosition.y, targetPosition.z,
0.0, 1.0, 0.0
);
}
}
module.exports = PlayerCamera;
+24
View File
@@ -0,0 +1,24 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
class TestPlane {
constructor() {
this.entity = new Entity();
this.position = this.entity.add(POSITION);
this.position.setLocalPosition(-10.0, 0.0, -10.0);
this.position.setLocalScale(20.0, 1.0, 20.0);
this.physics = this.entity.add(PHYSICS);
this.physics.setBodyType(PHYSICS_BODY_STATIC);
this.physics.setShape(PHYSICS_SHAPE_PLANE, 0.0, 1.0, 0.0, 0.0);
this.renderable = this.entity.add(RENDERABLE);
this.renderable.setMesh(0, MESH_PLANE);
this.renderable.setColor(128, 128, 128, 255);
}
}
module.exports = TestPlane;
+8 -56
View File
@@ -3,72 +3,24 @@
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
// Radius must stay well outside the floor's footprint (a 20x20 plane has a
// corner-to-center distance of 10*sqrt(2) =~ 14.1) -- orbiting inside that
// puts parts of the floor's own geometry near/behind the camera's view
// direction, which the PSP's legacy GU pipeline can't clip properly and
// drops the whole triangle instead of clipping it.
var CAMERA_ORBIT_RADIUS = 18.0;
var CAMERA_ORBIT_HEIGHT = 10.0;
var CAMERA_ORBIT_SPEED = 0.5;
var Player = require('./Player.js'); var Player = require('./Player.js');
var PlayerCamera = require('./PlayerCamera.js');
var TestPlane = require('./TestPlane.js');
var cameraOrbitAngle = 0.0; var camera = null;
var cameraPosition = null;
// Orbits the camera around the world origin at a fixed radius/height/
// speed, always looking back at the origin. Called once up front (so the
// very first rendered frame is already positioned correctly) and then
// once per frame via update() below.
function updateCameraOrbit() {
cameraOrbitAngle += Time.delta * CAMERA_ORBIT_SPEED;
var eyeX = Math.cos(cameraOrbitAngle) * CAMERA_ORBIT_RADIUS;
var eyeY = CAMERA_ORBIT_HEIGHT;
var eyeZ = Math.sin(cameraOrbitAngle) * CAMERA_ORBIT_RADIUS;
cameraPosition.lookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
module.exports = { module.exports = {
// Called once by Scene.set(), right after it creates and activates the
// scene this module owns.
init: function() { init: function() {
// Camera, orbiting the origin (see updateCameraOrbit above). var player = new Player();
var camera = new Entity(); new TestPlane();
cameraPosition = camera.add(POSITION); camera = new PlayerCamera(player);
camera.add(CAMERA);
updateCameraOrbit();
// Static ground plane. Physics ignores the entity's position
// component -- the shape's own normal/distance fully define the
// plane in world space.
var plane = new Entity();
var planePosition = plane.add(POSITION);
planePosition.setLocalPosition(-10.0, 0.0, -10.0);
planePosition.setLocalScale(20.0, 1.0, 20.0);
var planePhysics = plane.add(PHYSICS);
planePhysics.setBodyType(PHYSICS_BODY_STATIC);
planePhysics.setShape(PHYSICS_SHAPE_PLANE, 0.0, 1.0, 0.0, 0.0);
var planeRenderable = plane.add(RENDERABLE);
planeRenderable.setMesh(0, MESH_PLANE);
planeRenderable.setColor(128, 128, 128, 255);
new Player();
}, },
// Called once per engine frame while this module is the active scene
// (see Scene.set(), engineUpdate() -> moduleSceneUpdateCurrent()).
update: function() { update: function() {
if(cameraPosition) updateCameraOrbit(); if(camera) camera.update();
}, },
// Called once by Scene.set() when this module is replaced by another,
// right before the scene it owns is destroyed.
dispose: function() { dispose: function() {
cameraPosition = null; camera = null;
} }
}; };
+158 -75
View File
@@ -7,7 +7,7 @@
#include "dusktest.h" #include "dusktest.h"
#include "util/memory.h" #include "util/memory.h"
#include "time/time.h" #include "asset/asset.h"
#include "scene/scene.h" #include "scene/scene.h"
#include "entity/entitymanager.h" #include "entity/entitymanager.h"
#include "entity/component.h" #include "entity/component.h"
@@ -18,7 +18,8 @@
#include "display/mesh/capsule.h" #include "display/mesh/capsule.h"
#include "script/scriptmanager.h" #include "script/scriptmanager.h"
#include "script/module/scene/modulescene.h" #include "script/module/scene/modulescene.h"
#include <math.h> #include "script/module/require/modulerequire.h"
#include <zip.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -60,17 +61,93 @@ static errorret_t installOverworldScene(void) {
); );
memoryFree(fileSrc); 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); errorret_t ret = scriptManagerExec(wrapped, NULL);
moduleRequireDirPop();
memoryFree(wrapped); memoryFree(wrapped);
return ret; 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) { static int overworld_setup(void **state) {
sceneInit(); sceneInit();
errorret_t ret = scriptManagerInit(); errorret_t ret = scriptManagerInit();
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; } 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; return 0;
} }
@@ -80,6 +157,9 @@ static int overworld_teardown(void **state) {
sceneDispose(); sceneDispose();
if(g_zip) { zip_close(g_zip); g_zip = NULL; }
ASSET.zip = NULL;
// JerryScript defers freeing native-wrapped handles (Entity/Scene/ // JerryScript defers freeing native-wrapped handles (Entity/Scene/
// Position/Physics/Renderable instances) until GC/jerry_cleanup() runs, // Position/Physics/Renderable instances) until GC/jerry_cleanup() runs,
// so the leak check can only be meaningful after scriptManagerDispose() // 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); assert_true(sceneId != SCENE_ID_INVALID);
entitymanager_t *mgr = sceneGetEntities(sceneId); entitymanager_t *mgr = sceneGetEntities(sceneId);
// Entity 0: camera. Position + Camera components, initial orbit // Entity 0: player (new Player() runs first in init()).
// position placed with angle=0 (Time.delta defaults to 0 with no entityid_t playerEntity = 0;
// timeInit()/timeUpdate() in this test), i.e. eye=(radius, height, 0). componentid_t playerPos = entityGetComponent(
entityid_t camEntity = 0; mgr, playerEntity, COMPONENT_TYPE_POSITION
componentid_t camPos = entityGetComponent(
mgr, camEntity, 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( assert_true(
entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) != entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) !=
COMPONENT_ID_INVALID COMPONENT_ID_INVALID
); );
vec3 camPosition; // Entity 1: static ground plane (new TestPlane() runs second).
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.
entityid_t planeEntity = 1; entityid_t planeEntity = 1;
componentid_t planePos = entityGetComponent( componentid_t planePos = entityGetComponent(
mgr, planeEntity, COMPONENT_TYPE_POSITION 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.b, 128);
assert_int_equal(planeR->data.material.material.unlit.color.a, 255); assert_int_equal(planeR->data.material.material.unlit.color.a, 255);
// Entity 2: player. Dynamic capsule body (default body type), PLAYER // Entity 2: camera (new PlayerCamera(player) runs last). Position +
// component present. // Camera components, positioned at the player's local position plus
entityid_t playerEntity = 2; // PlayerCamera.js's fixed offset (angle=0, radius=18, height=10) --
componentid_t playerPos = entityGetComponent( // player is at (0, 2, 0), so eye=(18, 12, 0).
mgr, playerEntity, COMPONENT_TYPE_POSITION entityid_t camEntity = 2;
componentid_t camPos = entityGetComponent(
mgr, camEntity, COMPONENT_TYPE_POSITION
); );
assert_true(playerPos != COMPONENT_ID_INVALID); assert_true(camPos != 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( assert_true(
entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) != entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) !=
COMPONENT_ID_INVALID 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(); errorret_t ret = installOverworldScene();
assert_true(errorIsOk(ret)); assert_true(errorIsOk(ret));
sceneid_t sceneId = sceneGetActive(); sceneid_t sceneId = sceneGetActive();
entitymanager_t *mgr = sceneGetEntities(sceneId); 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( componentid_t camPos = entityGetComponent(
mgr, camEntity, COMPONENT_TYPE_POSITION mgr, camEntity, COMPONENT_TYPE_POSITION
); );
// Drive one frame with a known delta and confirm the camera orbited by // Move the player and confirm the camera's next update() re-centers on
// exactly angle = delta * speed (0.1 * 0.5 = 0.05 rad). // the player's new position at the same fixed offset (PlayerCamera.js
TIME.delta = 0.1f; // no longer orbits over time -- it just tracks the player).
entityPositionSetLocalPosition(
mgr, playerEntity, playerPos, (vec3){ 5.0f, 2.0f, -3.0f }
);
ret = moduleSceneUpdateCurrent(); ret = moduleSceneUpdateCurrent();
assert_true(errorIsOk(ret)); assert_true(errorIsOk(ret));
vec3 camPosition; vec3 camPosition;
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition); entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
const float_t expectedAngle = 0.1f * 0.5f; assert_float_equal(camPosition[0], 5.0f + 18.0f, 0.0001f);
assert_float_equal( assert_float_equal(camPosition[1], 2.0f + 10.0f, 0.0001f);
camPosition[0], cosf(expectedAngle) * 18.0f, 0.0001f assert_float_equal(camPosition[2], -3.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;
} }
static void test_scene_set_switches_and_disposes(void **state) { static void test_scene_set_switches_and_disposes(void **state) {
@@ -309,7 +392,7 @@ int main(void) {
overworld_setup, overworld_teardown overworld_setup, overworld_teardown
), ),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(
test_overworldscene_update_orbits_camera, test_overworldscene_camera_follows_player,
overworld_setup, overworld_teardown overworld_setup, overworld_teardown
), ),
cmocka_unit_test_setup_teardown( cmocka_unit_test_setup_teardown(