iuno just screwing around tbh
This commit is contained in:
@@ -4,4 +4,5 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
add_asset(SCRIPT test.lua)
|
add_asset(SCRIPT test.lua)
|
||||||
add_asset(SCRIPT test2.lua)
|
|
||||||
|
add_subdirectory(scene)
|
||||||
5
assets/script/init.lua
Normal file
5
assets/script/init.lua
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
print('Init')
|
||||||
|
|
||||||
|
-- Load map scene
|
||||||
|
setScene('map')
|
||||||
|
setMap()
|
||||||
4
assets/script/scene/CMakeLists.txt
Normal file
4
assets/script/scene/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Copyright (c) 2025 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
include('script/test2')
|
|
||||||
|
|
||||||
player = entityAdd(ENTITY_TYPE_PLAYER, 3, 6, 1)
|
|
||||||
|
|
||||||
print("Player entity ID: " .. player)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
print("This is test2.lua")
|
|
||||||
@@ -8,7 +8,7 @@ ENTITY_DIR_WEST = 1
|
|||||||
ENTITY_DIR_EAST = 2
|
ENTITY_DIR_EAST = 2
|
||||||
ENTITY_DIR_NORTH = 3
|
ENTITY_DIR_NORTH = 3
|
||||||
|
|
||||||
ENTITY_COUNT = 256
|
ENTITY_COUNT = 128
|
||||||
|
|
||||||
ENTITY_TYPE_NULL = 0
|
ENTITY_TYPE_NULL = 0
|
||||||
ENTITY_TYPE_PLAYER = 1
|
ENTITY_TYPE_PLAYER = 1
|
||||||
|
|||||||
@@ -18,8 +18,6 @@
|
|||||||
#include "script/scriptmanager.h"
|
#include "script/scriptmanager.h"
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
|
|
||||||
#include "script/scriptcontext.h"
|
|
||||||
|
|
||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||||
@@ -41,10 +39,9 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
errorChain(sceneManagerInit());
|
errorChain(sceneManagerInit());
|
||||||
|
|
||||||
// Run the initial script.
|
// Run the initial script.
|
||||||
scriptcontext_t testCtx;
|
errorChain(scriptContextInit(&ENGINE.mainScriptContext));
|
||||||
errorChain(scriptContextInit(&testCtx));
|
errorChain(scriptContextExecFile(&ENGINE.mainScriptContext, "script/test.dsf"));
|
||||||
errorChain(scriptContextExecFile(&testCtx, "script/test.dsf"));
|
scriptContextDispose(&ENGINE.mainScriptContext);
|
||||||
scriptContextDispose(&testCtx);
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "display/display.h"// Important to be included first.
|
#include "display/display.h"// Important to be included first.
|
||||||
#include "error/error.h"
|
#include "error/error.h"
|
||||||
|
#include "script/scriptcontext.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool_t running;
|
bool_t running;
|
||||||
int32_t argc;
|
int32_t argc;
|
||||||
const char_t **argv;
|
const char_t **argv;
|
||||||
|
scriptcontext_t mainScriptContext;
|
||||||
} engine_t;
|
} engine_t;
|
||||||
|
|
||||||
extern engine_t ENGINE;
|
extern engine_t ENGINE;
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ map_t MAP;
|
|||||||
|
|
||||||
errorret_t mapInit() {
|
errorret_t mapInit() {
|
||||||
memoryZero(&MAP, sizeof(map_t));
|
memoryZero(&MAP, sizeof(map_t));
|
||||||
|
|
||||||
// Init the default chunks. In future I'll probably make this based on where
|
// Init the first chunks.
|
||||||
// the player spawns in to save an initial mapSet.
|
|
||||||
chunkindex_t index = 0;
|
chunkindex_t index = 0;
|
||||||
for(chunkunit_t z = 0; z < MAP_CHUNK_DEPTH; z++) {
|
for(chunkunit_t z = 0; z < MAP_CHUNK_DEPTH; z++) {
|
||||||
for(chunkunit_t y = 0; y < MAP_CHUNK_HEIGHT; y++) {
|
for(chunkunit_t y = 0; y < MAP_CHUNK_HEIGHT; y++) {
|
||||||
|
|||||||
@@ -18,12 +18,6 @@ errorret_t sceneManagerInit(void) {
|
|||||||
|
|
||||||
sceneManagerRegisterScene(&SCENE_TEST);
|
sceneManagerRegisterScene(&SCENE_TEST);
|
||||||
sceneManagerRegisterScene(&SCENE_MAP);
|
sceneManagerRegisterScene(&SCENE_MAP);
|
||||||
|
|
||||||
// Initial scene
|
|
||||||
scene_t *initial = sceneManagerGetSceneByName("map");
|
|
||||||
sceneManagerSetScene(initial);
|
|
||||||
if(initial->init) errorChain(initial->init(&SCENE_MANAGER.sceneData));
|
|
||||||
initial->flags |= SCENE_FLAG_INITIALIZED;
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
#include "scenedata.h"
|
#include "scenedata.h"
|
||||||
|
|
||||||
#define SCENE_MANAGER_SCENE_COUNT_MAX 32
|
#define SCENE_MANAGER_SCENE_COUNT_MAX 16
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
scene_t *current;
|
scene_t *current;
|
||||||
|
|||||||
@@ -9,8 +9,6 @@
|
|||||||
#include "script/scriptcontext.h"
|
#include "script/scriptcontext.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void scriptFuncCamera(scriptcontext_t *context) {
|
void scriptFuncCamera(scriptcontext_t *context) {
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
}
|
}
|
||||||
39
src/script/func/scriptfuncscene.h
Normal file
39
src/script/func/scriptfuncscene.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "script/scriptcontext.h"
|
||||||
|
#include "scene/scenemanager.h"
|
||||||
|
#include "debug/debug.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "error/error.h"
|
||||||
|
|
||||||
|
int32_t scriptFuncSetScene(lua_State *L) {
|
||||||
|
assertNotNull(L, "Lua state cannot be NULL");
|
||||||
|
assertTrue(lua_isstring(L, 1), "First argument must be a string");
|
||||||
|
|
||||||
|
const char_t *sceneName = lua_tostring(L, 1);
|
||||||
|
scene_t *scene = sceneManagerGetSceneByName(sceneName);
|
||||||
|
assertNotNull(scene, "Scene with given name does not exist");
|
||||||
|
|
||||||
|
sceneManagerSetScene(scene);
|
||||||
|
|
||||||
|
if(scene->init) {
|
||||||
|
errorret_t err = scene->init(&SCENE_MANAGER.sceneData);
|
||||||
|
assertTrue(err.code == ERROR_OK, "Scene initialization failed");
|
||||||
|
}
|
||||||
|
scene->flags |= SCENE_FLAG_INITIALIZED;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void scriptFuncScene(scriptcontext_t *context) {
|
||||||
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
|
|
||||||
|
scriptContextRegFunc(context, "setScene", scriptFuncSetScene);
|
||||||
|
}
|
||||||
@@ -34,19 +34,19 @@ int32_t scriptFuncPrint(lua_State *L) {
|
|||||||
|
|
||||||
int32_t scriptFuncInclude(lua_State *L) {
|
int32_t scriptFuncInclude(lua_State *L) {
|
||||||
assertNotNull(L, "Lua state cannot be NULL");
|
assertNotNull(L, "Lua state cannot be NULL");
|
||||||
|
assertTrue(lua_isstring(L, 1), "Expected string filename");
|
||||||
|
|
||||||
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
|
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
|
||||||
if (!ctx) return luaL_error(L, "Lua extraspace ctx not set");
|
assertNotNull(ctx, "Script context cannot be NULL");
|
||||||
|
|
||||||
const char_t *filename = luaL_checkstring(L, 1);
|
const char_t *filename = luaL_checkstring(L, 1);
|
||||||
if(filename == NULL || filename[0] == '\0') {
|
assertNotNull(filename, "Filename cannot be NULL");
|
||||||
luaL_error(L, "Filename cannot be NULL or empty");
|
assertStrLenMin(filename, 1, "Filename cannot be empty");
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
// Copy out filename to mutable buffer
|
||||||
char_t buffer[1024];
|
char_t buffer[1024];
|
||||||
stringCopy(buffer, filename, 1024);
|
stringCopy(buffer, filename, 1024);
|
||||||
|
|
||||||
// Ensure it has .dsf extension
|
// Ensure it has .dsf extension
|
||||||
size_t len = strlen(buffer);
|
size_t len = strlen(buffer);
|
||||||
if(len < 4 || strcmp(&buffer[len - 4], ".dsf") != 0) {
|
if(len < 4 || strcmp(&buffer[len - 4], ".dsf") != 0) {
|
||||||
@@ -58,6 +58,7 @@ int32_t scriptFuncInclude(lua_State *L) {
|
|||||||
stringCopy(&buffer[len], ".dsf", 5);
|
stringCopy(&buffer[len], ".dsf", 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute the script file
|
||||||
errorret_t err = scriptContextExecFile(
|
errorret_t err = scriptContextExecFile(
|
||||||
ctx,
|
ctx,
|
||||||
buffer
|
buffer
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "script/func/scriptfunccamera.h"
|
#include "script/func/scriptfunccamera.h"
|
||||||
#include "script/func/scriptfuncentity.h"
|
#include "script/func/scriptfuncentity.h"
|
||||||
#include "script/func/scriptfuncsystem.h"
|
#include "script/func/scriptfuncsystem.h"
|
||||||
|
#include "script/func/scriptfuncscene.h"
|
||||||
|
|
||||||
errorret_t scriptContextInit(scriptcontext_t *context) {
|
errorret_t scriptContextInit(scriptcontext_t *context) {
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
@@ -30,9 +31,14 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
|
|||||||
// Store context in Lua extraspace
|
// Store context in Lua extraspace
|
||||||
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
|
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
|
||||||
|
|
||||||
|
// Register variables
|
||||||
|
// scriptContextExec(context, "PLATFORM = 'DESKTOP'");
|
||||||
|
|
||||||
// Register functions
|
// Register functions
|
||||||
scriptFuncSystem(context);
|
scriptFuncSystem(context);
|
||||||
scriptFuncEntity(context);
|
scriptFuncEntity(context);
|
||||||
|
scriptFuncCamera(context);
|
||||||
|
scriptFuncScene(context);
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user