Add prefab support

This commit is contained in:
2026-07-20 14:44:14 -05:00
parent b9f06fef7a
commit 38c2f6b6f9
14 changed files with 294 additions and 69 deletions
+2 -1
View File
@@ -3,6 +3,7 @@
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Header-only: gamecomponentlist.h is an X-macro list, no implementation.
# gameprefablist.h is an X-macro list, no implementation of its own.
add_subdirectory(component)
add_subdirectory(prefab)
+16
View File
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
// Game-specific prefabs, appended after the engine's inbuilt prefabs in
// entity/entityprefablist.h.
#include "entity/prefab/entityprefabplayer.h"
// Name (Uppercase)
// Extends (name of another prefab to apply first, or "" for none)
// Apply function
X(PLAYER, "", entityPrefabPlayerApply)
+9
View File
@@ -0,0 +1,9 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
entityprefabplayer.c
)
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entityprefabplayer.h"
#include "entity/entitymanager.h"
#include "entity/component/display/entityposition.h"
#include "entity/component/display/entityrenderable.h"
#include "entity/component/physics/entityphysics.h"
#include "display/color.h"
errorret_t entityPrefabPlayerApply(
entitymanager_t *mgr,
const entityid_t entityId
) {
componentid_t posComp = entityAddComponent(
mgr, entityId, COMPONENT_TYPE_POSITION
);
vec3 startPos = { 0.0f, 5.0f, 0.0f };
entityPositionSetLocalPosition(mgr, entityId, posComp, startPos);
componentid_t renderComp = entityAddComponent(
mgr, entityId, COMPONENT_TYPE_RENDERABLE
);
entityRenderableSetColor(
mgr, entityId, renderComp, color4b(255, 80, 80, 255)
);
entityAddComponent(mgr, entityId, COMPONENT_TYPE_PLAYER);
componentid_t physComp = entityAddComponent(
mgr, entityId, COMPONENT_TYPE_PHYSICS
);
entityPhysicsSetBodyType(mgr, entityId, physComp, PHYSICS_BODY_DYNAMIC);
entityPhysicsSetCollideMask(mgr, entityId, physComp, 0x3);
errorOk();
}
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "entity/entitybase.h"
#include "error/error.h"
/**
* Applies the PLAYER prefab: a POSITION 5 units up, a red
* SHADER_MATERIAL RENDERABLE, a PLAYER component, and a DYNAMIC PHYSICS
* body tagged with collideMask 0x3 (world + player layers). C-coded
* equivalent of the hand-authored "player" entity in
* assets/scenes/test.json.
*
* @param mgr The entity manager that owns the entity.
* @param entityId The ID of the entity to apply the prefab to.
* @return Error state.
*/
errorret_t entityPrefabPlayerApply(
entitymanager_t *mgr,
const entityid_t entityId
);
-24
View File
@@ -34,19 +34,6 @@ errorret_t gameInit(void) {
assetUnlockEntry(sceneEntry);
errorChain(ret);
// Test-only: print a debug message whenever an entity enters "testArea"
// (see assets/scenes/test.json), exercising the TRIGGER component.
entitymanager_t *mgr = sceneGetEntities(testSceneId);
entityid_t testAreaEntity = entityFindByName(mgr, "testArea");
if(testAreaEntity != ENTITY_ID_INVALID) {
componentid_t testAreaTrig = entityGetComponent(
mgr, testAreaEntity, COMPONENT_TYPE_TRIGGER
);
entityTriggerOnEnterAdd(
mgr, testAreaEntity, testAreaTrig, gameTestAreaOnEnter, NULL
);
}
sceneSetActive(testSceneId);
errorOk();
}
@@ -58,14 +45,3 @@ errorret_t gameUpdate(void) {
errorret_t gameDispose(void) {
errorOk();
}
void gameTestAreaOnEnter(
entitymanager_t *mgr,
const entityid_t triggerEntityId,
const componentid_t triggerComponentId,
const entityid_t otherEntityId,
const componentid_t otherComponentId,
void *user
) {
logDebug("testArea: entity %d entered", otherEntityId);
}