Entity modules
This commit is contained in:
+3
-1
@@ -25,7 +25,6 @@ elseif DOLPHIN then
|
||||
inputBind("right", INPUT_ACTION_RIGHT)
|
||||
inputBind("b", INPUT_ACTION_CANCEL)
|
||||
inputBind("a", INPUT_ACTION_ACCEPT)
|
||||
-- inputBind("z", INPUT_ACTION_RAGEQUIT)
|
||||
inputBind("z", INPUT_ACTION_CONSOLE)
|
||||
inputBind("lstick_up", INPUT_ACTION_UP)
|
||||
inputBind("lstick_down", INPUT_ACTION_DOWN)
|
||||
@@ -77,3 +76,6 @@ elseif LINUX then
|
||||
else
|
||||
print("Unknown platform, no default input bindings set.")
|
||||
end
|
||||
|
||||
-- Hand off to initial scene.
|
||||
sceneSet('test/scene.lua')
|
||||
@@ -55,8 +55,6 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||
errorChain(scriptContextExecFile(&ctx, "init.lua"));
|
||||
scriptContextDispose(&ctx);
|
||||
|
||||
sceneSet("test/scene.lua");
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "entity/entitymanager.h"
|
||||
|
||||
#include "display/mesh/cube.h"
|
||||
|
||||
void entityMeshInit(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId
|
||||
@@ -14,7 +16,7 @@ void entityMeshInit(
|
||||
entitymesh_t *comp = componentGetData(
|
||||
entityId, componentId, COMPONENT_TYPE_MESH
|
||||
);
|
||||
comp->mesh = NULL;
|
||||
comp->mesh = &CUBE_MESH_SIMPLE;
|
||||
}
|
||||
|
||||
mesh_t * entityMeshGetMesh(
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
# Subdirectories
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(entity)
|
||||
add_subdirectory(event)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(locale)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
moduleentity.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(physics)
|
||||
@@ -0,0 +1,12 @@
|
||||
# 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
|
||||
moduleentityposition.c
|
||||
moduleentitycamera.c
|
||||
moduleentitymesh.c
|
||||
moduleentitymaterial.c
|
||||
)
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentitycamera.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entitycamera.h"
|
||||
|
||||
void moduleEntityCamera(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
#define REG(name, func) lua_register(ctx->luaState, name, func)
|
||||
REG("entityCameraAdd", moduleEntityCameraAdd);
|
||||
REG("entityCameraGetZNear", moduleEntityCameraGetZNear);
|
||||
REG("entityCameraSetZNear", moduleEntityCameraSetZNear);
|
||||
REG("entityCameraGetZFar", moduleEntityCameraGetZFar);
|
||||
REG("entityCameraSetZFar", moduleEntityCameraSetZFar);
|
||||
#undef REG
|
||||
}
|
||||
|
||||
int moduleEntityCameraAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_CAMERA);
|
||||
lua_pushnumber(L, (lua_Number)compId);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityCameraGetZNear(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
lua_pushnumber(L, (lua_Number)entityCameraGetZNear(entityId, compId));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityCameraSetZNear(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
float_t zNear = (float_t)luaL_checknumber(L, 3);
|
||||
entityCameraSetZNear(entityId, compId, zNear);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityCameraGetZFar(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
lua_pushnumber(L, (lua_Number)entityCameraGetZFar(entityId, compId));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityCameraSetZFar(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
float_t zFar = (float_t)luaL_checknumber(L, 3);
|
||||
entityCameraSetZFar(entityId, compId, zFar);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
void moduleEntityCamera(scriptcontext_t *ctx);
|
||||
|
||||
int moduleEntityCameraAdd(lua_State *L);
|
||||
int moduleEntityCameraGetZNear(lua_State *L);
|
||||
int moduleEntityCameraSetZNear(lua_State *L);
|
||||
int moduleEntityCameraGetZFar(lua_State *L);
|
||||
int moduleEntityCameraSetZFar(lua_State *L);
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentitymaterial.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entitymaterial.h"
|
||||
|
||||
void moduleEntityMaterial(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
lua_register(ctx->luaState, "entityMaterialAdd", moduleEntityMaterialAdd);
|
||||
}
|
||||
|
||||
int moduleEntityMaterialAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_MATERIAL);
|
||||
lua_pushnumber(L, (lua_Number)compId);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
void moduleEntityMaterial(scriptcontext_t *ctx);
|
||||
|
||||
int moduleEntityMaterialAdd(lua_State *L);
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentitymesh.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entitymesh.h"
|
||||
|
||||
void moduleEntityMesh(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
lua_register(ctx->luaState, "entityMeshAdd", moduleEntityMeshAdd);
|
||||
}
|
||||
|
||||
int moduleEntityMeshAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_MESH);
|
||||
lua_pushnumber(L, (lua_Number)compId);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
void moduleEntityMesh(scriptcontext_t *ctx);
|
||||
|
||||
int moduleEntityMeshAdd(lua_State *L);
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentityposition.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
|
||||
void moduleEntityPosition(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
#define REG(name, func) lua_register(ctx->luaState, name, func)
|
||||
REG("entityPositionAdd", moduleEntityPositionAdd);
|
||||
REG("entityPositionSetPosition", moduleEntityPositionSetPosition);
|
||||
REG("entityPositionGetPosition", moduleEntityPositionGetPosition);
|
||||
REG("entityPositionSetRotation", moduleEntityPositionSetRotation);
|
||||
REG("entityPositionGetRotation", moduleEntityPositionGetRotation);
|
||||
REG("entityPositionSetScale", moduleEntityPositionSetScale);
|
||||
REG("entityPositionGetScale", moduleEntityPositionGetScale);
|
||||
REG("entityPositionLookAt", moduleEntityPositionLookAt);
|
||||
#undef REG
|
||||
}
|
||||
|
||||
int moduleEntityPositionAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_POSITION);
|
||||
lua_pushnumber(L, (lua_Number)compId);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityPositionSetPosition(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 pos = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
entityPositionSetPosition(entityId, compId, pos);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPositionGetPosition(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 pos;
|
||||
entityPositionGetPosition(entityId, compId, pos);
|
||||
lua_pushnumber(L, pos[0]);
|
||||
lua_pushnumber(L, pos[1]);
|
||||
lua_pushnumber(L, pos[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int moduleEntityPositionSetRotation(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 rot = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
entityPositionSetRotation(entityId, compId, rot);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPositionGetRotation(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 rot;
|
||||
entityPositionGetRotation(entityId, compId, rot);
|
||||
lua_pushnumber(L, rot[0]);
|
||||
lua_pushnumber(L, rot[1]);
|
||||
lua_pushnumber(L, rot[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int moduleEntityPositionSetScale(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 scale = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
entityPositionSetScale(entityId, compId, scale);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPositionGetScale(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 scale;
|
||||
entityPositionGetScale(entityId, compId, scale);
|
||||
lua_pushnumber(L, scale[0]);
|
||||
lua_pushnumber(L, scale[1]);
|
||||
lua_pushnumber(L, scale[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int moduleEntityPositionLookAt(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 target = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
vec3 up = {
|
||||
(float_t)luaL_checknumber(L, 6),
|
||||
(float_t)luaL_checknumber(L, 7),
|
||||
(float_t)luaL_checknumber(L, 8)
|
||||
};
|
||||
vec3 eye = {
|
||||
(float_t)luaL_checknumber(L, 9),
|
||||
(float_t)luaL_checknumber(L, 10),
|
||||
(float_t)luaL_checknumber(L, 11)
|
||||
};
|
||||
entityPositionLookAt(entityId, compId, target, up, eye);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
void moduleEntityPosition(scriptcontext_t *ctx);
|
||||
|
||||
int moduleEntityPositionAdd(lua_State *L);
|
||||
int moduleEntityPositionSetPosition(lua_State *L);
|
||||
int moduleEntityPositionGetPosition(lua_State *L);
|
||||
int moduleEntityPositionSetRotation(lua_State *L);
|
||||
int moduleEntityPositionGetRotation(lua_State *L);
|
||||
int moduleEntityPositionSetScale(lua_State *L);
|
||||
int moduleEntityPositionGetScale(lua_State *L);
|
||||
int moduleEntityPositionLookAt(lua_State *L);
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentity.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entitymanager.h"
|
||||
#include "entity/entity.h"
|
||||
|
||||
void moduleEntity(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
lua_register(ctx->luaState, "entityAdd", moduleEntityAdd);
|
||||
lua_register(ctx->luaState, "entityRemove", moduleEntityRemove);
|
||||
}
|
||||
|
||||
int moduleEntityAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t id = entityManagerAdd();
|
||||
lua_pushnumber(L, (lua_Number)id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityRemove(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
if(!lua_isnumber(L, 1)) {
|
||||
luaL_error(L, "entityRemove requires a number entity ID");
|
||||
return 0;
|
||||
}
|
||||
|
||||
entityid_t id = (entityid_t)luaL_checknumber(L, 1);
|
||||
entityDispose(id);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
/**
|
||||
* Register the entity module within the given script context.
|
||||
*
|
||||
* @param ctx The script context to register the module in.
|
||||
*/
|
||||
void moduleEntity(scriptcontext_t *ctx);
|
||||
|
||||
/**
|
||||
* Lua binding for entityManagerAdd - creates a new entity and returns its ID.
|
||||
*
|
||||
* @param L The Lua state.
|
||||
* @return int Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleEntityAdd(lua_State *L);
|
||||
|
||||
/**
|
||||
* Lua binding for entityDispose - disposes of an entity by its ID.
|
||||
*
|
||||
* @param L The Lua state.
|
||||
* @return int Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleEntityRemove(lua_State *L);
|
||||
@@ -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
|
||||
moduleentityphysics.c
|
||||
)
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "moduleentityphysics.h"
|
||||
#include "assert/assert.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/physics/entityphysics.h"
|
||||
|
||||
void moduleEntityPhysics(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
#define REG(name, func) lua_register(ctx->luaState, name, func)
|
||||
REG("entityPhysicsAdd", moduleEntityPhysicsAdd);
|
||||
REG("entityPhysicsSetVelocity", moduleEntityPhysicsSetVelocity);
|
||||
REG("entityPhysicsGetVelocity", moduleEntityPhysicsGetVelocity);
|
||||
REG("entityPhysicsApplyImpulse", moduleEntityPhysicsApplyImpulse);
|
||||
REG("entityPhysicsIsOnGround", moduleEntityPhysicsIsOnGround);
|
||||
REG("entityPhysicsSetShapeCube", moduleEntityPhysicsSetShapeCube);
|
||||
REG("entityPhysicsSetShapeSphere", moduleEntityPhysicsSetShapeSphere);
|
||||
REG("entityPhysicsSetShapeCapsule", moduleEntityPhysicsSetShapeCapsule);
|
||||
REG("entityPhysicsSetShapePlane", moduleEntityPhysicsSetShapePlane);
|
||||
#undef REG
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsAdd(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = entityAddComponent(entityId, COMPONENT_TYPE_PHYSICS);
|
||||
lua_pushnumber(L, (lua_Number)compId);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsSetVelocity(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 vel = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
entityPhysicsSetVelocity(entityId, compId, vel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsGetVelocity(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 vel;
|
||||
entityPhysicsGetVelocity(entityId, compId, vel);
|
||||
lua_pushnumber(L, vel[0]);
|
||||
lua_pushnumber(L, vel[1]);
|
||||
lua_pushnumber(L, vel[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsApplyImpulse(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
vec3 impulse = {
|
||||
(float_t)luaL_checknumber(L, 3),
|
||||
(float_t)luaL_checknumber(L, 4),
|
||||
(float_t)luaL_checknumber(L, 5)
|
||||
};
|
||||
entityPhysicsApplyImpulse(entityId, compId, impulse);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsIsOnGround(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
lua_pushboolean(L, (int)entityPhysicsIsOnGround(entityId, compId));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsSetShapeCube(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
physicsshape_t shape;
|
||||
shape.type = PHYSICS_SHAPE_CUBE;
|
||||
shape.data.cube.halfExtents[0] = (float_t)luaL_checknumber(L, 3);
|
||||
shape.data.cube.halfExtents[1] = (float_t)luaL_checknumber(L, 4);
|
||||
shape.data.cube.halfExtents[2] = (float_t)luaL_checknumber(L, 5);
|
||||
entityPhysicsSetShape(entityId, compId, shape);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsSetShapeSphere(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
physicsshape_t shape;
|
||||
shape.type = PHYSICS_SHAPE_SPHERE;
|
||||
shape.data.sphere.radius = (float_t)luaL_checknumber(L, 3);
|
||||
entityPhysicsSetShape(entityId, compId, shape);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsSetShapeCapsule(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
physicsshape_t shape;
|
||||
shape.type = PHYSICS_SHAPE_CAPSULE;
|
||||
shape.data.capsule.radius = (float_t)luaL_checknumber(L, 3);
|
||||
shape.data.capsule.halfHeight = (float_t)luaL_checknumber(L, 4);
|
||||
entityPhysicsSetShape(entityId, compId, shape);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleEntityPhysicsSetShapePlane(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
entityid_t entityId = (entityid_t)luaL_checknumber(L, 1);
|
||||
componentid_t compId = (componentid_t)luaL_checknumber(L, 2);
|
||||
physicsshape_t shape;
|
||||
shape.type = PHYSICS_SHAPE_PLANE;
|
||||
shape.data.plane.normal[0] = (float_t)luaL_checknumber(L, 3);
|
||||
shape.data.plane.normal[1] = (float_t)luaL_checknumber(L, 4);
|
||||
shape.data.plane.normal[2] = (float_t)luaL_checknumber(L, 5);
|
||||
shape.data.plane.distance = (float_t)luaL_checknumber(L, 6);
|
||||
entityPhysicsSetShape(entityId, compId, shape);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
void moduleEntityPhysics(scriptcontext_t *ctx);
|
||||
|
||||
int moduleEntityPhysicsAdd(lua_State *L);
|
||||
int moduleEntityPhysicsSetVelocity(lua_State *L);
|
||||
int moduleEntityPhysicsGetVelocity(lua_State *L);
|
||||
int moduleEntityPhysicsApplyImpulse(lua_State *L);
|
||||
int moduleEntityPhysicsIsOnGround(lua_State *L);
|
||||
int moduleEntityPhysicsSetShapeCube(lua_State *L);
|
||||
int moduleEntityPhysicsSetShapeSphere(lua_State *L);
|
||||
int moduleEntityPhysicsSetShapeCapsule(lua_State *L);
|
||||
int moduleEntityPhysicsSetShapePlane(lua_State *L);
|
||||
@@ -6,5 +6,5 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
modulesystem.c
|
||||
modulescript.c
|
||||
)
|
||||
+13
-13
@@ -5,21 +5,21 @@
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "modulesystem.h"
|
||||
#include "log/log.h"
|
||||
#include "modulescript.h"
|
||||
#include "assert/assert.h"
|
||||
#include "console/console.h"
|
||||
#include "util/string.h"
|
||||
#include "script/scriptmodule.h"
|
||||
|
||||
void moduleSystem(scriptcontext_t *context) {
|
||||
void moduleScript(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
|
||||
lua_register(context->luaState, "print", moduleSysPrint);
|
||||
lua_register(context->luaState, "include", moduleSysInclude);
|
||||
lua_register(context->luaState, "module", moduleSysModule);
|
||||
lua_register(context->luaState, "print", moduleScriptPrint);
|
||||
lua_register(context->luaState, "include", moduleScriptInclude);
|
||||
lua_register(context->luaState, "module", moduleScriptModule);
|
||||
}
|
||||
|
||||
int moduleSysPrint(lua_State *L) {
|
||||
int moduleScriptPrint(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
int n = lua_gettop(L);
|
||||
@@ -28,19 +28,19 @@ int moduleSysPrint(lua_State *L) {
|
||||
|
||||
for(int i = 1; i <= n; ++i) {
|
||||
size_t len;
|
||||
const char *s = luaL_tolstring(L, i, &len); // converts any value to string
|
||||
const char *s = luaL_tolstring(L, i, &len);
|
||||
luaL_addlstring(&b, s, len);
|
||||
lua_pop(L, 1); // pop result of luaL_tolstring
|
||||
lua_pop(L, 1);
|
||||
if(i < n) luaL_addlstring(&b, "\t", 1);
|
||||
}
|
||||
|
||||
luaL_pushresult(&b);
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
logDebug("%s\n", msg);
|
||||
return 0; // no values returned to Lua
|
||||
consolePrint("%s", msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleSysInclude(lua_State *L) {
|
||||
int moduleScriptInclude(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
if(!lua_isstring(L, 1)) {
|
||||
@@ -89,7 +89,7 @@ int moduleSysInclude(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleSysModule(lua_State *L) {
|
||||
int moduleScriptModule(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
if(!lua_isstring(L, 1)) {
|
||||
+4
-4
@@ -13,7 +13,7 @@
|
||||
*
|
||||
* @param context The script context to register system module functions to.
|
||||
*/
|
||||
void moduleSystem(scriptcontext_t *context);
|
||||
void moduleScript(scriptcontext_t *context);
|
||||
|
||||
/**
|
||||
* Script binding for printing messages to the debug console.
|
||||
@@ -21,7 +21,7 @@ void moduleSystem(scriptcontext_t *context);
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleSysPrint(lua_State *L);
|
||||
int moduleScriptPrint(lua_State *L);
|
||||
|
||||
/**
|
||||
* Script binding for including and executing another script file.
|
||||
@@ -29,7 +29,7 @@ int moduleSysPrint(lua_State *L);
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleSysInclude(lua_State *L);
|
||||
int moduleScriptInclude(lua_State *L);
|
||||
|
||||
/**
|
||||
* Script binding for loading a script module by name.
|
||||
@@ -37,4 +37,4 @@ int moduleSysInclude(lua_State *L);
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleSysModule(lua_State *L);
|
||||
int moduleScriptModule(lua_State *L);
|
||||
@@ -28,12 +28,12 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
|
||||
// Store context in Lua extraspace
|
||||
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
|
||||
|
||||
// All scripts get the system module
|
||||
const scriptmodule_t *sysModule = scriptModuleGetByName("system");
|
||||
if(sysModule == NULL) {
|
||||
// All scripts get the script module
|
||||
const scriptmodule_t *sysScript = scriptModuleGetByName("script");
|
||||
if(sysScript == NULL) {
|
||||
errorThrow("Failed to find system script module");
|
||||
}
|
||||
sysModule->callback(context);
|
||||
sysScript->callback(context);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
*/
|
||||
|
||||
#include "scriptmodule.h"
|
||||
#include "script/module/system/modulesystem.h"
|
||||
#include "script/module/system/modulescript.h"
|
||||
#include "script/module/entity/moduleentity.h"
|
||||
#include "script/module/input/moduleinput.h"
|
||||
#include "script/module/moduleplatform.h"
|
||||
#include "script/module/scene/modulescene.h"
|
||||
@@ -22,26 +23,40 @@
|
||||
#include "script/module/display/modulescreen.h"
|
||||
#include "script/module/display/moduletexture.h"
|
||||
#include "script/module/display/moduletileset.h"
|
||||
#include "script/module/entity/display/moduleentityposition.h"
|
||||
#include "script/module/entity/display/moduleentitycamera.h"
|
||||
#include "script/module/entity/display/moduleentitymesh.h"
|
||||
#include "script/module/entity/display/moduleentitymaterial.h"
|
||||
#include "script/module/entity/physics/moduleentityphysics.h"
|
||||
|
||||
#include "script/scriptgame.h"
|
||||
#include "util/string.h"
|
||||
|
||||
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
||||
{ .name = "system", .callback = moduleSystem },
|
||||
{ .name = "input", .callback = moduleInput },
|
||||
{ .name = "platform", .callback = modulePlatform },
|
||||
{ .name = "color", .callback = moduleColor },
|
||||
{ .name = "scene", .callback = moduleScene },
|
||||
{ .name = "locale", .callback = moduleLocale },
|
||||
{ .name = "time", .callback = moduleTime },
|
||||
{ .name = "event", .callback = moduleEvent },
|
||||
{ .name = "spritebatch", .callback = moduleSpriteBatch },
|
||||
{ .name = "glm", .callback = moduleGLM },
|
||||
{ .name = "ui", .callback = moduleUi },
|
||||
{ .name = "text", .callback = moduleText },
|
||||
{ .name = "screen", .callback = moduleScreen },
|
||||
{ .name = "texture", .callback = moduleTexture },
|
||||
{ .name = "tileset", .callback = moduleTileset },
|
||||
{ .name = "shader", .callback = moduleShader },
|
||||
#define REG(strName, fnCallback) { .name = strName, .callback = fnCallback },
|
||||
REG("script", moduleScript)
|
||||
REG("entityposition", moduleEntityPosition)
|
||||
REG("entitycamera", moduleEntityCamera)
|
||||
REG("entitymesh", moduleEntityMesh)
|
||||
REG("entitymaterial", moduleEntityMaterial)
|
||||
REG("entityphysics", moduleEntityPhysics)
|
||||
REG("entity", moduleEntity)
|
||||
REG("input", moduleInput)
|
||||
REG("platform", modulePlatform)
|
||||
REG("color", moduleColor)
|
||||
REG("scene", moduleScene)
|
||||
REG("locale", moduleLocale)
|
||||
REG("time", moduleTime)
|
||||
REG("event", moduleEvent)
|
||||
REG("spritebatch", moduleSpriteBatch)
|
||||
REG("glm", moduleGLM)
|
||||
REG("ui", moduleUi)
|
||||
REG("text", moduleText)
|
||||
REG("screen", moduleScreen)
|
||||
REG("texture", moduleTexture)
|
||||
REG("tileset", moduleTileset)
|
||||
REG("shader", moduleShader)
|
||||
#undef REG
|
||||
|
||||
#ifdef SCRIPT_GAME_LIST
|
||||
SCRIPT_GAME_LIST
|
||||
|
||||
Reference in New Issue
Block a user