diff --git a/src/main.c b/src/main.c index b4ddd6e..de5f2c8 100644 --- a/src/main.c +++ b/src/main.c @@ -4,32 +4,18 @@ #include #include -#define TYPE_MODULE evmod_glfw -#include -#define NAMESPACE_MODULE evmod_glfw -#include -#define EVENT_MODULE evmod_glfw -#include - -#define TYPE_MODULE evmod_ecs -#include -#define NAMESPACE_MODULE evmod_ecs -#include - -#define TYPE_MODULE evmod_physics -#include -#define NAMESPACE_MODULE evmod_physics -#include - -#define TYPE_MODULE evmod_script -#include -#define NAMESPACE_MODULE evmod_script -#include - -#define TYPE_MODULE evmod_assetsystem -#include -#define NAMESPACE_MODULE evmod_assetsystem -#include +#define IMPORT_MODULE evmod_glfw +#include IMPORT_MODULE_H +#define IMPORT_MODULE evmod_ecs +#include IMPORT_MODULE_H +#define IMPORT_MODULE evmod_physics +#include IMPORT_MODULE_H +#define IMPORT_MODULE evmod_script +#include IMPORT_MODULE_H +#define IMPORT_MODULE evmod_assetsystem +#include IMPORT_MODULE_H +#define IMPORT_MODULE evmod_game +#include IMPORT_MODULE_H // Close window when Q is pressed DECLARE_EVENT_LISTENER(keyPressedListener, (KeyPressedEvent *event) { @@ -38,14 +24,16 @@ DECLARE_EVENT_LISTENER(keyPressedListener, (KeyPressedEvent *event) { }) #define IMPORT_NAMESPACES do { \ - IMPORT_NAMESPACE(ECS, ecs_module); \ - IMPORT_NAMESPACE(Window, window_module); \ - IMPORT_NAMESPACE(Input, input_module); \ - IMPORT_NAMESPACE(Physics, physics_module); \ - IMPORT_NAMESPACE(Rigidbody, physics_module); \ + IMPORT_NAMESPACE(ECS , ecs_module); \ + IMPORT_NAMESPACE(Window , window_module); \ + IMPORT_NAMESPACE(Input , input_module); \ + IMPORT_NAMESPACE(Physics , physics_module); \ + IMPORT_NAMESPACE(Rigidbody , physics_module); \ IMPORT_NAMESPACE(CollisionShape, physics_module); \ - IMPORT_NAMESPACE(Script, script_module); \ - IMPORT_NAMESPACE(AssetSystem, asset_module); \ + IMPORT_NAMESPACE(Script , script_module); \ + IMPORT_NAMESPACE(AssetSystem , asset_module); \ + IMPORT_NAMESPACE(Game , game_module); \ + IMPORT_NAMESPACE(Object , game_module); \ } while (0) int main(int argc, char **argv) @@ -54,12 +42,13 @@ int main(int argc, char **argv) evol_parse_args(engine, argc, argv); evol_init(engine); - evolmodule_t script_module = evol_loadmodule("script"); DEBUG_ASSERT(script_module); - evolmodule_t ecs_module = evol_loadmodule("ecs"); DEBUG_ASSERT(ecs_module); - evolmodule_t window_module = evol_loadmodule("window"); DEBUG_ASSERT(window_module); - evolmodule_t input_module = evol_loadmodule("input"); DEBUG_ASSERT(input_module); - evolmodule_t physics_module = evol_loadmodule("physics"); DEBUG_ASSERT(physics_module); + evolmodule_t script_module = evol_loadmodule("script"); DEBUG_ASSERT(script_module); + evolmodule_t ecs_module = evol_loadmodule("ecs"); DEBUG_ASSERT(ecs_module); + evolmodule_t window_module = evol_loadmodule("window"); DEBUG_ASSERT(window_module); + evolmodule_t input_module = evol_loadmodule("input"); DEBUG_ASSERT(input_module); + evolmodule_t physics_module = evol_loadmodule("physics"); DEBUG_ASSERT(physics_module); evolmodule_t asset_module = evol_loadmodule("asset-importer"); DEBUG_ASSERT(asset_module); + evolmodule_t game_module = evol_loadmodule("game"); DEBUG_ASSERT(game_module); IMPORT_NAMESPACES; IMPORT_EVENTS_evmod_glfw(window_module); @@ -73,67 +62,71 @@ int main(int argc, char **argv) Script->initECS(); Physics->initECS(); + Game->initECS(); - ECSEntityID ent1 = ECS->createEntity(); - ECSEntityID ent2 = ECS->createEntity(); + ECSEntityID playerBox = ECS->createEntity(); + ECSEntityID childBox = ECS->createChild(playerBox); - ScriptHandle ent1ScriptHandle = Script->new("Entity1Script", - /* "this.on_update = function ()\n" */ - /* " rb = this:getComponent(Rigidbody)\n" */ - /* " rb:addForce(Vec3:new(2, 0, 0))\n" */ - /* "end" */ - "" + ECSEntityID ground = ECS->createEntity(); + + + ScriptHandle childBoxScript = Script->new("Entity1Script", ""); + ScriptHandle playerBoxScript = Script->new("Entity2Script", + "this.on_update = function () \n" + " rb = this:getComponent(Rigidbody) \n" + " if Input.getKeyDown(Input.KeyCode.Space) then \n" + " this.position = Vec3:new(-1, 5, -10) \n" + " end \n" + " if Input.getKeyDown(Input.KeyCode.D) then \n" + " rb:addForce(Vec3:new(10, 0, 0)) \n" + " end \n" + " if Input.getKeyDown(Input.KeyCode.A) then \n" + " rb:addForce(Vec3:new(-10, 0, 0)) \n" + " end \n" + " if Input.getKeyDown(Input.KeyCode.W) then \n" + " rb:addForce(Vec3:new(0, 0, -10)) \n" + " end \n" + " if Input.getKeyDown(Input.KeyCode.S) then \n" + " rb:addForce(Vec3:new(0, 0, 10)) \n" + " end \n" + "end \n" ); - ScriptHandle ent2ScriptHandle = Script->new("Entity2Script", - "this.on_update = function ()\n" - " rb = this:getComponent(Rigidbody)\n" - " if Input.getKeyDown(Input.KeyCode.Space) then\n" - " rb:addForce(Vec3:new(0, 100, 0))\n" - " end\n" - " if Input.getKeyDown(Input.KeyCode.D) then\n" - " rb:addForce(Vec3:new(10, 0, 0))\n" - " end\n" - " if Input.getKeyDown(Input.KeyCode.A) then\n" - " rb:addForce(Vec3:new(-10, 0, 0))\n" - " end\n" - " if Input.getKeyDown(Input.KeyCode.W) then\n" - " rb:addForce(Vec3:new(0, 0, -10))\n" - " end\n" - " if Input.getKeyDown(Input.KeyCode.S) then\n" - " rb:addForce(Vec3:new(0, 0, 10))\n" - " end\n" - "end" - ); - - Script->addToEntity(ent1, ent1ScriptHandle); - Script->addToEntity(ent2, ent2ScriptHandle); + Script->addToEntity(playerBox, playerBoxScript); + Script->addToEntity(childBox, childBoxScript); CollisionShapeHandle boxCollider = CollisionShape->newBox(Vec3new(1., 1., 1.)); CollisionShapeHandle groundCollider = CollisionShape->newBox(Vec3new(5., 5., 5.)); - RigidbodyInfo rbInfo = { + Object->setPosition(childBox, Vec3new(1, 7, -10 )); + Object->setRotation(childBox, Vec4new(0, 0, 0, 1)); + Object->setScale(childBox, Vec3new(1, 1, 1 )); + + Object->setPosition(playerBox, Vec3new(-1, 5, -10 )); + Object->setRotation(playerBox, Vec4new( 0, 0, 0, 1)); + Object->setScale(playerBox, Vec3new( 1, 1, 1 )); + + Object->setPosition(ground, Vec3new(0, -10, -10 )); + Object->setRotation(ground, Vec4new(0, 0, 0, 1)); + Object->setScale(ground, Vec3new(1, 1, 1 )); + + RigidbodyHandle childRigidbody = Rigidbody->addToEntity(childBox, &(RigidbodyInfo) { + .type = EV_RIGIDBODY_KINEMATIC, + .collisionShape = boxCollider, + }); + + RigidbodyHandle playerRigidbody = Rigidbody->addToEntity(playerBox, &(RigidbodyInfo) { .type = EV_RIGIDBODY_DYNAMIC, .collisionShape = boxCollider, .mass = 1.0, - .restitution = 0.0 - }; - - RigidbodyHandle box = Rigidbody->new(&rbInfo); - Rigidbody->setPosition(box, Vec3new(1, 5, -10)); - Rigidbody->addToEntity(ent1, box); - - RigidbodyHandle box2 = Rigidbody->new(&rbInfo); - Rigidbody->setPosition(box2, Vec3new(-1, 5, -10)); - Rigidbody->addToEntity(ent2, box2); + }); RigidbodyInfo groundRbInfo = { .type = EV_RIGIDBODY_STATIC, .collisionShape = groundCollider, - .restitution = 0.0 }; - RigidbodyHandle ground = Rigidbody->new(&groundRbInfo); - Rigidbody->setPosition(ground, Vec3new(0, -10, -10)); + + RigidbodyHandle groundRigidbody = Rigidbody->addToEntity(ground, &groundRbInfo); rmt_SetCurrentThreadName("Main Thread"); @@ -157,14 +150,10 @@ int main(int argc, char **argv) result |= ECS->update(0); } - // Remove the following section - /* RigidbodyHandle testHandle = Rigidbody->getFromEntity(ent1); */ - /* Vec3 pos = Rigidbody->getPosition(testHandle); */ - /* printf("Current position of Entity1 = (%f, %f, %f)\n", pos.x, pos.y, pos.z); */ - sleep_ms(17); } + evol_unloadmodule(game_module); evol_unloadmodule(physics_module); evol_unloadmodule(input_module); evol_unloadmodule(asset_module);