diff --git a/config.lua b/config.lua index f6f1315..9ca1207 100644 --- a/config.lua +++ b/config.lua @@ -1,4 +1,6 @@ name = "sandbox" module_dir = "." -visualize_physics = 1 \ No newline at end of file +visualize_physics = 1 + +project_dir = "/home/sisyphus/dev/evol-sandbox/res/project" diff --git a/res/project/game.proj b/res/project/game.proj new file mode 100644 index 0000000..2363ed2 --- /dev/null +++ b/res/project/game.proj @@ -0,0 +1,25 @@ +{ + "name": "dummy_game", + "engineVersion": "0.2", + "mounts": [ + { + "path": "./res", + "mountpoint": "res" + }, + { + "path": "./res/scripts", + "mountpoint": "scripts" + }, + { + "path": "./res/scenes", + "mountpoint": "scenes" + } + ], + "scenes": [ + { + "id": "MainScene", + "path": "scenes://MainScene.evsc" + } + ], + "activeScene": "MainScene" +} diff --git a/res/project/res/scenes/MainScene.evsc b/res/project/res/scenes/MainScene.evsc new file mode 100644 index 0000000..61d7bd1 --- /dev/null +++ b/res/project/res/scenes/MainScene.evsc @@ -0,0 +1,104 @@ +{ + "id":"MainScene", + "nodes": [ + { + "id": "Camera", + "components": [ + { + "type": "TransformComponent", + "position": [0.0, 0.0, 0.0], + "rotation": [0.0, 0.0, 0.0], + "scale": [1.0, 1.0, 1.0] + }, + { + "type": "CameraComponent", + "view": "Perspective", + "fov": 120, + "near": 0.001, + "far": 1000, + "aspectRatio": 1.3333 + }, + { + "type": "ScriptComponent", + "script_name": "CameraController", + "script_path": "scripts://Scene0/camera.lua" + } + ] + }, + { + "id": "Player", + "components": [ + { + "type": "TransformComponent", + "position": [0.0, 0.0, -15.0], + "rotation": [0.0, 0.0, 0.0], + "scale": [1.0, 1.0, 1.0] + }, + { + "type": "RigidbodyComponent", + "rigidbodyType": "Dynamic", + "mass": 1.0, + "restitution": 1.0, + "collisionShape": { + "type": "Sphere", + "radius": 1.0 + } + }, + { + "type": "ScriptComponent", + "script_name": "PlayerController", + "script_path": "scripts://Scene0/player.lua" + } + ], + "children": [ + { + "id": "Child", + "components": [ + { + "type": "TransformComponent", + "position": [0.0, 5.0, -5.0], + "rotation": [0.0, 0.0, 0.0], + "scale": [1.0, 1.0, 1.0] + }, + { + "type": "RigidbodyComponent", + "rigidbodyType": "Kinematic", + "mass": 1.0, + "restitution": 1.0, + "collisionShape": { + "type": "Sphere", + "radius": 1.0 + } + }, + { + "type": "ScriptComponent", + "script_name": "ChildController", + "script_path": "scripts://Scene0/child.lua" + } + ] + } + ] + }, + { + "id": "Ground", + "components": [ + { + "type": "TransformComponent", + "position": [0.0, -15.0, -15.0], + "rotation": [0.0, 0.0, 0.0], + "scale": [1.0, 1.0, 1.0] + }, + { + "type": "RigidbodyComponent", + "rigidbodyType": "Static", + "mass": 0.0, + "restitution": 0.0, + "collisionShape": { + "type": "Sphere", + "radius": 10.0 + } + } + ] + } + ] +} diff --git a/res/scripts/Scene0/camera.lua b/res/project/res/scripts/Scene0/camera.lua similarity index 100% rename from res/scripts/Scene0/camera.lua rename to res/project/res/scripts/Scene0/camera.lua diff --git a/res/scripts/Scene0/child.lua b/res/project/res/scripts/Scene0/child.lua similarity index 87% rename from res/scripts/Scene0/child.lua rename to res/project/res/scripts/Scene0/child.lua index 31a5053..2c4a2cf 100644 --- a/res/scripts/Scene0/child.lua +++ b/res/project/res/scripts/Scene0/child.lua @@ -4,7 +4,6 @@ this.on_init = function () 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 diff --git a/res/scripts/Scene0/player.lua b/res/project/res/scripts/Scene0/player.lua similarity index 90% rename from res/scripts/Scene0/player.lua rename to res/project/res/scripts/Scene0/player.lua index fb15af9..1dba869 100644 --- a/res/scripts/Scene0/player.lua +++ b/res/project/res/scripts/Scene0/player.lua @@ -1,5 +1,5 @@ this.on_collisionenter = function(other) - other.position = other.position + Vec3:new(3.2, 0, 0) + -- other.position = other.position + Vec3:new(3.2, 0, 0) end this.on_update = function () diff --git a/res/scripts/Scene1/camera.lua b/res/project/res/scripts/Scene1/camera.lua similarity index 100% rename from res/scripts/Scene1/camera.lua rename to res/project/res/scripts/Scene1/camera.lua diff --git a/res/scripts/Scene1/child.lua b/res/project/res/scripts/Scene1/child.lua similarity index 100% rename from res/scripts/Scene1/child.lua rename to res/project/res/scripts/Scene1/child.lua diff --git a/res/scripts/Scene1/player.lua b/res/project/res/scripts/Scene1/player.lua similarity index 100% rename from res/scripts/Scene1/player.lua rename to res/project/res/scripts/Scene1/player.lua diff --git a/src/main.c b/src/main.c index cf9c56e..33ceb34 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #define IMPORT_MODULE evmod_glfw #include IMPORT_MODULE_H @@ -34,145 +36,13 @@ DECLARE_EVENT_LISTENER(keyPressedListener, (KeyPressedEvent *event) { /* else if(event->keyCode == 61) // Numrow = */ }) -void -init_scenes() -{ - { // Scene 0 - scenes[0] = Game->newScene(); - - GameObject playerBox = Scene->createObject(scenes[0]); - GameObject childBox = Scene->createChildObject(scenes[0], playerBox); - GameObject ground = Scene->createObject(scenes[0]); - - GameObject camera = Scene->createCamera(scenes[0], EV_CAMERA_VIEWTYPE_PERSPECTIVE); - - assert(Scene->getActiveCamera(scenes[0]) == camera); - Camera->setHFOV(scenes[0], camera, 120); - Camera->setNearPlane(scenes[0], camera, 0.001); - Camera->setFarPlane(scenes[0], camera, 1000); - Camera->setAspectRatio(scenes[0], camera, (F32)width / (F32)height); - Object->setPosition(scenes[0], camera, Vec3new(0, 0, -5)); - - AssetHandle childBoxScriptAsset = Asset->load("scripts://Scene0/child.lua"); - TextAsset childScript = TextLoader->loadAsset(childBoxScriptAsset); - ScriptHandle childBoxScript = Script->new("ChildScriptScene0", childScript.text); - Asset->free(childBoxScriptAsset); - - AssetHandle playerBoxScriptAsset = Asset->load("scripts://Scene0/player.lua"); - TextAsset playerScript = TextLoader->loadAsset(playerBoxScriptAsset); - ScriptHandle playerBoxScript = Script->new("PlayerScriptScene0", playerScript.text); - Asset->free(playerBoxScriptAsset); - - 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]); - - Script->addToEntity(scenes[0], playerBox, playerBoxScript); - Script->addToEntity(scenes[0], childBox, childBoxScript); - Script->addToEntity(scenes[0], camera, cameraScript); - - CollisionShapeHandle boxCollider = CollisionShape->newBox(physics_handle, Vec3new(1., 1., 1.)); - CollisionShapeHandle groundCollider = CollisionShape->newBox(physics_handle, Vec3new(5., 5., 5.)); - - Object->setPosition(scenes[0], childBox, Vec3new(1, 7, -10 )); - Object->setPosition(scenes[0], playerBox, Vec3new(-1, 5, -10 )); - Object->setPosition(scenes[0], ground, Vec3new(0, -10, -10 )); - - RigidbodyHandle childRigidbody = Rigidbody->addToEntity(scenes[0], childBox, &(RigidbodyInfo) { - .type = EV_RIGIDBODY_KINEMATIC, - .collisionShape = boxCollider, - }); - - RigidbodyHandle playerRigidbody = Rigidbody->addToEntity(scenes[0], playerBox, &(RigidbodyInfo) { - .type = EV_RIGIDBODY_DYNAMIC, - .collisionShape = boxCollider, - .mass = 1.0, - }); - - RigidbodyInfo groundRbInfo = { - .type = EV_RIGIDBODY_STATIC, - .collisionShape = groundCollider, - }; - - RigidbodyHandle groundRigidbody = Rigidbody->addToEntity(scenes[0], ground, &groundRbInfo); - } - { // Scene 1 - scenes[1] = Game->newScene(); - - GameObject playerBox = Scene->createObject(scenes[1]); - GameObject childBox = Scene->createObject(scenes[1]); - GameObject ground = Scene->createObject(scenes[1]); - - GameObject camera = Scene->createCamera(scenes[1], EV_CAMERA_VIEWTYPE_PERSPECTIVE); - - assert(Scene->getActiveCamera(scenes[1]) == camera); - Camera->setHFOV(scenes[1], camera, 120); - Camera->setNearPlane(scenes[1], camera, 0.001); - Camera->setFarPlane(scenes[1], camera, 1000); - Camera->setAspectRatio(scenes[1], camera, (F32)width / (F32)height); - Object->setPosition(scenes[1], camera, Vec3new(0, 0, -5)); - - AssetHandle childBoxScriptAsset = Asset->load("scripts://Scene1/child.lua"); - TextAsset childScript = TextLoader->loadAsset(childBoxScriptAsset); - ScriptHandle childBoxScript = Script->new("ChildScriptScene1", childScript.text); - Asset->free(childBoxScriptAsset); - - AssetHandle playerBoxScriptAsset = Asset->load("scripts://Scene1/player.lua"); - TextAsset playerScript = TextLoader->loadAsset(playerBoxScriptAsset); - ScriptHandle playerBoxScript = Script->new("PlayerScriptScene1", playerScript.text); - Asset->free(playerBoxScriptAsset); - - 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]); - - Script->addToEntity(scenes[1], playerBox, playerBoxScript); - Script->addToEntity(scenes[1], childBox, childBoxScript); - Script->addToEntity(scenes[1], camera, cameraScript); - - CollisionShapeHandle boxCollider = CollisionShape->newBox(physics_handle, Vec3new(1., 1., 1.)); - CollisionShapeHandle groundCollider = CollisionShape->newBox(physics_handle, Vec3new(5., 5., 5.)); - - Object->setPosition(scenes[1], childBox, Vec3new(1, 7, -10 )); - Object->setPosition(scenes[1], playerBox, Vec3new(-1, 5, -10 )); - Object->setPosition(scenes[1], ground, Vec3new(0, -10, -10 )); - - - RigidbodyHandle childRigidbody = Rigidbody->addToEntity(scenes[1], childBox, &(RigidbodyInfo) { - .type = EV_RIGIDBODY_DYNAMIC, - .collisionShape = boxCollider, - .mass = 1.0, - }); - - RigidbodyHandle playerRigidbody = Rigidbody->addToEntity(scenes[1], playerBox, &(RigidbodyInfo) { - .type = EV_RIGIDBODY_DYNAMIC, - .collisionShape = boxCollider, - .mass = 1.0, - }); - - RigidbodyInfo groundRbInfo = { - .type = EV_RIGIDBODY_STATIC, - .collisionShape = groundCollider, - }; - - RigidbodyHandle groundRigidbody = Rigidbody->addToEntity(scenes[1], ground, &groundRbInfo); - } -} - int main(int argc, char **argv) { evolengine_t *engine = evol_create(); evol_parse_args(engine, argc, argv); evol_init(engine); + evolmodule_t script_mod = evol_loadmodule("script"); DEBUG_ASSERT(script_mod); evolmodule_t window_mod = evol_loadmodule("window"); DEBUG_ASSERT(window_mod); evolmodule_t input_mod = evol_loadmodule("input"); DEBUG_ASSERT(input_mod); @@ -185,10 +55,7 @@ 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:/"); + imports(asset_mod , (AssetManager, Asset, TextLoader, JSONLoader)) IMPORT_EVENTS_evmod_glfw(window_mod); @@ -196,8 +63,60 @@ int main(int argc, char **argv) Input->setActiveWindow(windowHandle); ACTIVATE_EVENT_LISTENER(keyPressedListener, KeyPressedEvent); + evstring project_dir = NULL; + EvConfigLoaderResult project_dir_get_res = ev_configloader_get("project_dir", EV_TYPE_NAME(STRING), &project_dir); + if(project_dir_get_res != EV_CONFIGLOADER_SUCCESS) { + ev_log_error("[sandbox] Could not get project_dir from config file. Error: %s", EvConfigLoaderResultStrings[project_dir_get_res]); + } - init_scenes(); + assert(project_dir); + + evstring project_mountpoint = evstring_new("project"); + AssetManager->mount(&project_dir, &project_mountpoint); + evstring_free(project_mountpoint); + + + EV_DEFER( + AssetHandle project_config = Asset->load("project://game.proj"), + Asset->free(project_config)) + { + JSONAsset project_desc = JSONLoader->loadAsset(project_config); + + // Loading filesystem mounts + double mounts_count = evjs_get(project_desc.json_data, "mounts.len")->as_num; + for(int i = 0; i < (int)mounts_count;i++) { + evstring path_id = evstring_newfmt("mounts[%d].path", i); + evstring mountpoint_id = evstring_newfmt("mounts[%d].mountpoint", i); + + evstr_ref path_ref = evjs_get(project_desc.json_data, path_id)->as_str; + evstr_ref mountpoint_ref = evjs_get(project_desc.json_data, mountpoint_id)->as_str; + + evstring path = evstring_newfmt("%s/%.*s", project_dir, path_ref.len, path_ref.data + path_ref.offset); + evstring mountpoint = evstring_refclone(mountpoint_ref); + + AssetManager->mount(&path, &mountpoint); + + evstring_free(mountpoint); + evstring_free(path); + + evstring_free(mountpoint_id); + evstring_free(path_id); + } + + // Loading Scenes + double scene_count = evjs_get(project_desc.json_data, "scenes.len")->as_num; + for(int i = 0; i < (int)scene_count;i++) { + evstring scenepath_id = evstring_newfmt("scenes[%d].path", i); + evstring scenepath = evstring_refclone(evjs_get(project_desc.json_data, scenepath_id)->as_str); + + scenes[0] = Game->loadSceneFromFile(scenepath); + + evstring_free(scenepath); + evstring_free(scenepath_id); + } + } + + evstring_free(project_dir); rmt_SetCurrentThreadName("Main Thread");