From fb5128df7b90ad841454ccda5f123e128f910ffb Mon Sep 17 00:00:00 2001 From: Robear Selwans Date: Mon, 31 May 2021 23:00:38 +0200 Subject: [PATCH] Added evmod_assets and started using it for scripts Signed-off-by: Robear Selwans --- meson.build | 6 +- res/scripts/Scene0/camera.lua | 18 ++++ res/scripts/Scene0/child.lua | 15 +++ res/scripts/Scene0/player.lua | 22 ++++ res/scripts/Scene1/camera.lua | 0 res/scripts/Scene1/child.lua | 23 +++++ res/scripts/Scene1/player.lua | 22 ++++ src/main.c | 159 +++++++---------------------- subprojects/evmod_assets.wrap | 3 + subprojects/evmod_assetsystem.wrap | 6 -- 10 files changed, 141 insertions(+), 133 deletions(-) create mode 100644 res/scripts/Scene0/camera.lua create mode 100644 res/scripts/Scene0/child.lua create mode 100644 res/scripts/Scene0/player.lua create mode 100644 res/scripts/Scene1/camera.lua create mode 100644 res/scripts/Scene1/child.lua create mode 100644 res/scripts/Scene1/player.lua create mode 100644 subprojects/evmod_assets.wrap delete mode 100644 subprojects/evmod_assetsystem.wrap diff --git a/meson.build b/meson.build index 8b558f3..d488825 100644 --- a/meson.build +++ b/meson.build @@ -23,7 +23,7 @@ subproject('evmod_glfw') subproject('evmod_ecs') subproject('evmod_physics') subproject('evmod_script') -subproject('evmod_assetsystem') +subproject('evmod_assets') subproject('evmod_game') evmodglfw_dep = dependency('evmod_glfw') @@ -31,7 +31,7 @@ evmodecs_dep = dependency('evmod_ecs') evmodphysics_dep = dependency('evmod_physics') evmodscript_dep = dependency('evmod_script') evmodgame_dep = dependency('evmod_game') -evmod_assetsystem_dep = dependency('evmod_assetsystem') +evmod_assets_dep = dependency('evmod_assets') # Setup build directory subdir('buildscripts') @@ -47,7 +47,7 @@ sandbox_exe = executable( evmodphysics_dep, evmodscript_dep, evmodgame_dep, - evmod_assetsystem_dep + evmod_assets_dep, ], install : true, c_args: sandbox_args, diff --git a/res/scripts/Scene0/camera.lua b/res/scripts/Scene0/camera.lua new file mode 100644 index 0000000..876e20e --- /dev/null +++ b/res/scripts/Scene0/camera.lua @@ -0,0 +1,18 @@ +this.on_init = function() + this.speed = 0.1 +end + +this.on_fixedupdate = function() + if Input.getKeyDown(Input.KeyCode.Up) then + this.position = this.position + Vec3:new(0, 1, 0) * this.speed + end + if Input.getKeyDown(Input.KeyCode.Down) then + this.position = this.position - Vec3:new(0, 1, 0) * this.speed + end + if Input.getKeyDown(Input.KeyCode.Right) then + this.position = this.position + Vec3:new(1, 0, 0) * this.speed + end + if Input.getKeyDown(Input.KeyCode.Left) then + this.position = this.position - Vec3:new(1, 0, 0) * this.speed + end +end diff --git a/res/scripts/Scene0/child.lua b/res/scripts/Scene0/child.lua new file mode 100644 index 0000000..31a5053 --- /dev/null +++ b/res/scripts/Scene0/child.lua @@ -0,0 +1,15 @@ +this.on_init = function () + this.custom_eulerangles = Vec3:new() + this.custom_angularvelocity = Vec3:new(0, 0.01, 0) +end + +this.on_fixedupdate = function () + print('Scene0 OnFixedUpdate Entity #' .. this.entityID) + if Input.getKeyDown(Input.KeyCode.Left) then + this.custom_eulerangles:add(Vec3:new(0,0.01,0)) + end + if Input.getKeyDown(Input.KeyCode.Right) then + this.custom_eulerangles:sub(Vec3:new(0,0.01,0)) + end + this.eulerAngles = this.custom_eulerangles +end diff --git a/res/scripts/Scene0/player.lua b/res/scripts/Scene0/player.lua new file mode 100644 index 0000000..fb15af9 --- /dev/null +++ b/res/scripts/Scene0/player.lua @@ -0,0 +1,22 @@ +this.on_collisionenter = function(other) + other.position = other.position + Vec3:new(3.2, 0, 0) +end + +this.on_update = function () + rb = this:getComponent(Rigidbody) + if Input.getKeyDown(Input.KeyCode.Space) then + rb:addForce(Vec3:new(0, 100, 0)) + end + if Input.getKeyDown(Input.KeyCode.D) then + rb:addForce(Vec3:new(10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.A) then + rb:addForce(Vec3:new(-10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.W) then + rb:addForce(Vec3:new(0, 0, -10)) + end + if Input.getKeyDown(Input.KeyCode.S) then + rb:addForce(Vec3:new(0, 0, 10)) + end +end diff --git a/res/scripts/Scene1/camera.lua b/res/scripts/Scene1/camera.lua new file mode 100644 index 0000000..e69de29 diff --git a/res/scripts/Scene1/child.lua b/res/scripts/Scene1/child.lua new file mode 100644 index 0000000..121f894 --- /dev/null +++ b/res/scripts/Scene1/child.lua @@ -0,0 +1,23 @@ +this.on_init = function () + this.custom_eulerangles = Vec3:new() + this.custom_angularvelocity = Vec3:new(0, 0.01, 0) +end + +this.on_update = function () + rb = this:getComponent(Rigidbody) + if Input.getKeyDown(Input.KeyCode.Enter) then + rb:addForce(Vec3:new(0, 100, 0)) + end + if Input.getKeyDown(Input.KeyCode.Right) then + rb:addForce(Vec3:new(10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.Left) then + rb:addForce(Vec3:new(-10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.Up) then + rb:addForce(Vec3:new(0, 0, -10)) + end + if Input.getKeyDown(Input.KeyCode.Down) then + rb:addForce(Vec3:new(0, 0, 10)) + end +end diff --git a/res/scripts/Scene1/player.lua b/res/scripts/Scene1/player.lua new file mode 100644 index 0000000..bbbd7df --- /dev/null +++ b/res/scripts/Scene1/player.lua @@ -0,0 +1,22 @@ +this.on_collisionenter = function(other) + other.position = other.position + Vec3:new(0.2, 0, 0) +end + +this.on_update = function () + rb = this:getComponent(Rigidbody) + if Input.getKeyDown(Input.KeyCode.Space) then + rb:addForce(Vec3:new(0, 100, 0)) + end + if Input.getKeyDown(Input.KeyCode.D) then + rb:addForce(Vec3:new(10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.A) then + rb:addForce(Vec3:new(-10, 0, 0)) + end + if Input.getKeyDown(Input.KeyCode.W) then + rb:addForce(Vec3:new(0, 0, -10)) + end + if Input.getKeyDown(Input.KeyCode.S) then + rb:addForce(Vec3:new(0, 0, 10)) + end +end diff --git a/src/main.c b/src/main.c index 6f7614d..9c0d83e 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ #include IMPORT_MODULE_H #define IMPORT_MODULE evmod_script #include IMPORT_MODULE_H -#define IMPORT_MODULE evmod_assetsystem +#define IMPORT_MODULE evmod_assets #include IMPORT_MODULE_H #define IMPORT_MODULE evmod_game #include IMPORT_MODULE_H @@ -53,69 +53,20 @@ init_scenes() Camera->setAspectRatio(scenes[0], camera, (F32)width / (F32)height); Object->setPosition(scenes[0], camera, Vec3new(0, 0, -5)); - ScriptHandle childBoxScript = Script->new("Entity1Script1", - "this.on_init = function () " - " this.custom_eulerangles = Vec3:new() " - " this.custom_angularvelocity = Vec3:new(0, 0.01, 0) " - "end " - " " - "this.on_fixedupdate = function () " - " print('Scene0 OnFixedUpdate Entity #' .. this.entityID)" - " if Input.getKeyDown(Input.KeyCode.Left) then " - " this.custom_eulerangles:add(Vec3:new(0,0.01,0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.Right) then " - " this.custom_eulerangles:sub(Vec3:new(0,0.01,0)) " - " end " - " this.eulerAngles = this.custom_eulerangles " - "end " - ); + AssetHandle childBoxScriptAsset = Asset->load("scripts://Scene0/child.lua"); + TextAsset childScript = TextLoader->loadAsset(childBoxScriptAsset); + ScriptHandle childBoxScript = Script->new("ChildScriptScene0", childScript.text); + Asset->free(childBoxScriptAsset); - ScriptHandle playerBoxScript = Script->new("Entity2Script1", - "this.on_collisionenter = function(other) " - " other.position = other.position + Vec3:new(0.2, 0, 0) " - "end " - " " - "this.on_update = function () " - " rb = this:getComponent(Rigidbody) " - " if Input.getKeyDown(Input.KeyCode.Space) then " - " rb:addForce(Vec3:new(0, 100, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.D) then " - " rb:addForce(Vec3:new(10, 0, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.A) then " - " rb:addForce(Vec3:new(-10, 0, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.W) then " - " rb:addForce(Vec3:new(0, 0, -10)) " - " end " - " if Input.getKeyDown(Input.KeyCode.S) then " - " rb:addForce(Vec3:new(0, 0, 10)) " - " end " - "end " - ); + AssetHandle playerBoxScriptAsset = Asset->load("scripts://Scene0/player.lua"); + TextAsset playerScript = TextLoader->loadAsset(playerBoxScriptAsset); + ScriptHandle playerBoxScript = Script->new("PlayerScriptScene0", playerScript.text); + Asset->free(playerBoxScriptAsset); - ScriptHandle cameraScript = Script->new("CameraScript1", - "this.on_init = function() " - " this.speed = 3 " - "end " - - "this.on_fixedupdate = function() " - " if Input.getKeyDown(Input.KeyCode.Up) then " - " this.position = this.position + Vec3:new(0, 1, 0) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Down) then " - " this.position = this.position - Vec3:new(0, 1, 0) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Right) then " - " this.position = this.position + Vec3:new(1, 0, 0) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Left) then " - " this.position = this.position - Vec3:new(1, 0, 0) * this.speed " - " end " - "end " - ); + AssetHandle cameraScriptAsset = Asset->load("scripts://Scene0/camera.lua"); + TextAsset cameraScriptText = TextLoader->loadAsset(cameraScriptAsset); + ScriptHandle cameraScript = Script->new("CameraScriptScene0", cameraScriptText.text); + Asset->free(cameraScriptAsset); GenericHandle ecs_handle = Scene->getECSWorld(scenes[0]); PhysicsWorldHandle physics_handle = Scene->getPhysicsWorld(scenes[0]); @@ -165,69 +116,20 @@ init_scenes() Camera->setAspectRatio(scenes[1], camera, (F32)width / (F32)height); Object->setPosition(scenes[1], camera, Vec3new(0, 0, -5)); - ScriptHandle childBoxScript = Script->new("Entity1Script2", - "this.on_init = function () " - " this.custom_eulerangles = Vec3:new() " - " this.custom_angularvelocity = Vec3:new(0, 0.01, 0) " - "end " - " " - "this.on_fixedupdate = function () " - " print('Scene1 OnFixedUpdate Entity #' .. this.entityID)" - " if Input.getKeyDown(Input.KeyCode.Left) then " - " this.custom_eulerangles:add(Vec3:new(0,0.01,0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.Right) then " - " this.custom_eulerangles:sub(Vec3:new(0,0.01,0)) " - " end " - " this.eulerAngles = this.custom_eulerangles " - "end " - ); + AssetHandle childBoxScriptAsset = Asset->load("scripts://Scene1/child.lua"); + TextAsset childScript = TextLoader->loadAsset(childBoxScriptAsset); + ScriptHandle childBoxScript = Script->new("ChildScriptScene1", childScript.text); + Asset->free(childBoxScriptAsset); - ScriptHandle playerBoxScript = Script->new("Entity2Script2", - "this.on_collisionenter = function(other) " - " other.position = other.position + Vec3:new(0.2, 0, 0) " - "end " - " " - "this.on_update = function () " - " rb = this:getComponent(Rigidbody) " - " if Input.getKeyDown(Input.KeyCode.Space) then " - " rb:addForce(Vec3:new(0, 100, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.D) then " - " rb:addForce(Vec3:new(10, 0, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.A) then " - " rb:addForce(Vec3:new(-10, 0, 0)) " - " end " - " if Input.getKeyDown(Input.KeyCode.W) then " - " rb:addForce(Vec3:new(0, 0, -10)) " - " end " - " if Input.getKeyDown(Input.KeyCode.S) then " - " rb:addForce(Vec3:new(0, 0, 10)) " - " end " - "end " - ); + AssetHandle playerBoxScriptAsset = Asset->load("scripts://Scene1/player.lua"); + TextAsset playerScript = TextLoader->loadAsset(playerBoxScriptAsset); + ScriptHandle playerBoxScript = Script->new("PlayerScriptScene1", playerScript.text); + Asset->free(playerBoxScriptAsset); - ScriptHandle cameraScript = Script->new("CameraScript2", - "this.on_init = function() " - " this.speed = 3 " - "end " - - "this.on_fixedupdate = function() " - " if Input.getKeyDown(Input.KeyCode.Up) then " - " this.position = this.position - Vec3:new(0, 0, 1) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Down) then " - " this.position = this.position + Vec3:new(0, 0, 1) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Right) then " - " this.position = this.position + Vec3:new(1, 0, 0) * this.speed " - " end " - " if Input.getKeyDown(Input.KeyCode.Left) then " - " this.position = this.position - Vec3:new(1, 0, 0) * this.speed " - " end " - "end " - ); + AssetHandle cameraScriptAsset = Asset->load("scripts://Scene1/camera.lua"); + TextAsset cameraScriptText = TextLoader->loadAsset(cameraScriptAsset); + ScriptHandle cameraScript = Script->new("CameraScriptScene1", cameraScriptText.text); + Asset->free(cameraScriptAsset); GenericHandle ecs_handle = Scene->getECSWorld(scenes[1]); PhysicsWorldHandle physics_handle = Scene->getPhysicsWorld(scenes[1]); @@ -275,7 +177,7 @@ int main(int argc, char **argv) evolmodule_t window_mod = evol_loadmodule("window"); DEBUG_ASSERT(window_mod); evolmodule_t input_mod = evol_loadmodule("input"); DEBUG_ASSERT(input_mod); evolmodule_t physics_mod = evol_loadmodule("physics"); DEBUG_ASSERT(physics_mod); - evolmodule_t asset_mod = evol_loadmodule("asset-importer"); DEBUG_ASSERT(asset_mod); + evolmodule_t asset_mod = evol_loadmodule("assetmanager"); DEBUG_ASSERT(asset_mod); evolmodule_t game_mod = evol_loadmodule("game"); DEBUG_ASSERT(game_mod); imports(script_mod , (Script)) @@ -283,6 +185,15 @@ int main(int argc, char **argv) imports(window_mod , (Window)) imports(input_mod , (Input)) imports(physics_mod, (PhysicsWorld, Rigidbody, CollisionShape)) + imports(asset_mod , (AssetManager, Asset, TextLoader)) + + AssetManager->mount("../res", "res:/"); + AssetManager->mount("../res/scripts", "scripts:/"); + + AssetHandle handle = Asset->load("scripts://script.lua"); + TextAsset txt = TextLoader->loadAsset(handle); + ev_log_debug("Script: \n%s\n", txt.text); + Asset->free(handle); IMPORT_EVENTS_evmod_glfw(window_mod); diff --git a/subprojects/evmod_assets.wrap b/subprojects/evmod_assets.wrap new file mode 100644 index 0000000..ba44f94 --- /dev/null +++ b/subprojects/evmod_assets.wrap @@ -0,0 +1,3 @@ +[wrap-git] +url = https://github.com/evol3D/evol-mod-assetmanager +revision = master diff --git a/subprojects/evmod_assetsystem.wrap b/subprojects/evmod_assetsystem.wrap deleted file mode 100644 index 960c092..0000000 --- a/subprojects/evmod_assetsystem.wrap +++ /dev/null @@ -1,6 +0,0 @@ -[wrap-git] -url = https://github.com/evol3D/evol-mod-assetsystem -revision = master - -[provide] -dependency_names = evmod_assetsystem