Started using a project file to load scenes

Signed-off-by: Robear Selwans <robear.selwans@outlook.com>
This commit is contained in:
2021-06-02 08:04:44 +02:00
parent 4e37cad399
commit e6cc79f1dc
10 changed files with 190 additions and 141 deletions

View File

@@ -2,3 +2,5 @@ name = "sandbox"
module_dir = "." module_dir = "."
visualize_physics = 1 visualize_physics = 1
project_dir = "/home/sisyphus/dev/evol-sandbox/res/project"

25
res/project/game.proj Normal file
View File

@@ -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"
}

View File

@@ -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
}
}
]
}
]
}

View File

@@ -4,7 +4,6 @@ this.on_init = function ()
end end
this.on_fixedupdate = function () this.on_fixedupdate = function ()
print('Scene0 OnFixedUpdate Entity #' .. this.entityID)
if Input.getKeyDown(Input.KeyCode.Left) then if Input.getKeyDown(Input.KeyCode.Left) then
this.custom_eulerangles:add(Vec3:new(0,0.01,0)) this.custom_eulerangles:add(Vec3:new(0,0.01,0))
end end

View File

@@ -1,5 +1,5 @@
this.on_collisionenter = function(other) 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 end
this.on_update = function () this.on_update = function ()

View File

@@ -3,6 +3,8 @@
#include <evol/common/ev_macros.h> #include <evol/common/ev_macros.h>
#include <evol/common/ev_profile.h> #include <evol/common/ev_profile.h>
#include <evol/utils/sleep.h> #include <evol/utils/sleep.h>
#include <evjson.h>
#include <evol/core/configloader.h>
#define IMPORT_MODULE evmod_glfw #define IMPORT_MODULE evmod_glfw
#include IMPORT_MODULE_H #include IMPORT_MODULE_H
@@ -34,145 +36,13 @@ DECLARE_EVENT_LISTENER(keyPressedListener, (KeyPressedEvent *event) {
/* else if(event->keyCode == 61) // Numrow = */ /* 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) int main(int argc, char **argv)
{ {
evolengine_t *engine = evol_create(); evolengine_t *engine = evol_create();
evol_parse_args(engine, argc, argv); evol_parse_args(engine, argc, argv);
evol_init(engine); evol_init(engine);
evolmodule_t script_mod = evol_loadmodule("script"); DEBUG_ASSERT(script_mod); evolmodule_t script_mod = evol_loadmodule("script"); DEBUG_ASSERT(script_mod);
evolmodule_t window_mod = evol_loadmodule("window"); DEBUG_ASSERT(window_mod); evolmodule_t window_mod = evol_loadmodule("window"); DEBUG_ASSERT(window_mod);
evolmodule_t input_mod = evol_loadmodule("input"); DEBUG_ASSERT(input_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(window_mod , (Window))
imports(input_mod , (Input)) imports(input_mod , (Input))
imports(physics_mod, (PhysicsWorld, Rigidbody, CollisionShape)) imports(physics_mod, (PhysicsWorld, Rigidbody, CollisionShape))
imports(asset_mod , (AssetManager, Asset, TextLoader)) imports(asset_mod , (AssetManager, Asset, TextLoader, JSONLoader))
AssetManager->mount("../res", "res:/");
AssetManager->mount("../res/scripts", "scripts:/");
IMPORT_EVENTS_evmod_glfw(window_mod); IMPORT_EVENTS_evmod_glfw(window_mod);
@@ -196,8 +63,60 @@ int main(int argc, char **argv)
Input->setActiveWindow(windowHandle); Input->setActiveWindow(windowHandle);
ACTIVATE_EVENT_LISTENER(keyPressedListener, KeyPressedEvent); 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"); rmt_SetCurrentThreadName("Main Thread");