diff --git a/cfg.json b/cfg.json index 6201d9b..3311d3f 100644 --- a/cfg.json +++ b/cfg.json @@ -1 +1,4 @@ -{"game_file":"lvl.json","start_scene":"lvl_1"} \ No newline at end of file +{ + "game_file":"lvl.json", + "scene_queue":["lvl_1", "lvl_2"] +} diff --git a/components/PlayerInput.cpp b/components/PlayerInput.cpp index b42e558..416101e 100644 --- a/components/PlayerInput.cpp +++ b/components/PlayerInput.cpp @@ -77,7 +77,7 @@ void PlayerInput::update(float dt) // Collision callbacks void PlayerInput::onCollisionEnter(goop::Entity other) { - //... + std::cout << "Collision enter with entity " << other.getUID() << std::endl; } void PlayerInput::onCollisionExit(goop::Entity other) diff --git a/components/WindowsTest.cpp b/components/WindowsTest.cpp index 6d42065..715253f 100644 --- a/components/WindowsTest.cpp +++ b/components/WindowsTest.cpp @@ -1,5 +1,6 @@ #include "WindowsTest.h" #include +#include // Gets called when the game starts void WindowsTest::init() @@ -14,6 +15,11 @@ void WindowsTest::init() void WindowsTest::update(float dt) { //... + if (goop::isKeyPressed(ImGuiKey_T)) + { + std::cout << "T was pressed" << std::endl; + nextScene(); + } } // Collision callbacks diff --git a/game/GameApp.cpp b/game/GameApp.cpp index 1f23376..033bf0d 100644 --- a/game/GameApp.cpp +++ b/game/GameApp.cpp @@ -4,11 +4,11 @@ #include #include +#include "components/CustomComponents.h" #include #include #include #include -#include "components/CustomComponents.h" using json = nlohmann::json; goop::App* goop::createGame(int argc, char** argv, Scene* scene) { return new GameApp(scene); } @@ -33,19 +33,24 @@ void GameApp::init() } json lvl = lvlOpt.value(); - json startScene; - - // find starting scene - for (json& scene : lvl["scenes"]) + int size = cfg["scene_queue"].size(); + std::cout << "Loading " << size << " scenes" << std::endl; + for (int i = 0; i < cfg["scene_queue"].size(); i++) { - if (scene["name"] == cfg["start_scene"]) + std::string sceneName = cfg["scene_queue"][i]; + std::cout << "Loading scene: " << sceneName << std::endl; + for (auto& sceneJson : lvl["scenes"]) { - startScene = scene; - break; + if (sceneJson["name"] == sceneName) + { + std::cout << "Found scene: " << sceneName << std::endl; + std::cout << sceneJson.dump(4) << std::endl; + scene->queueScene(sceneJson); + break; + } } } - - scene->loadScene(startScene); + scene->loadScene(); /* TODO ------------- - store EVERYTHING contiguously - decide on a data structure - (week 7) diff --git a/goop/Components.h b/goop/Components.h index 5d2e051..0296c4d 100644 --- a/goop/Components.h +++ b/goop/Components.h @@ -113,6 +113,11 @@ class CustomComponent return e; } } + void nextScene() + { + auto scene = entity.getScene(); + scene->nextScene(); + } std::string name; goop::Entity entity; diff --git a/goop/Core.cpp b/goop/Core.cpp index 2d9cd98..c9886a4 100644 --- a/goop/Core.cpp +++ b/goop/Core.cpp @@ -34,27 +34,6 @@ Core::Core(int argc, char** argv) : app(createGame(argc, argv, &scene)) sys::gRenderer->setScene(&scene); rm->initialize(); app->init(); -#ifndef GOOP_APPTYPE_EDITOR - sys::gPhysics->initialize(); - // initialize rigid bodies - auto rbView = scene.view(); - auto tcView = scene.view(); - for (auto entity : rbView) - { - RigidbodyComponent* rbc = &rbView.get(entity); - TransformComponent* tc = &tcView.get(entity); - sys::gPhysics->addRigidBody(rbc, tc); - } -#endif - - // load meshes from components - auto mcView = scene.view(); - for (auto entity : mcView) - { - MeshComponent& mesh = mcView.get(entity); - rm->loadMesh(&mesh); - rm->loadTexture(&mesh); - } } void Core::run() diff --git a/goop/Scene.cpp b/goop/Scene.cpp index 94ebed0..bd9d262 100644 --- a/goop/Scene.cpp +++ b/goop/Scene.cpp @@ -29,9 +29,9 @@ std::optional Scene::getEntity(const std::string& tag) return std::nullopt; } -void Scene::loadScene(nlohmann::json& startScene) +void Scene::loadScene() { - sceneJson = startScene; + json startScene = sceneQueue.front(); if (startScene["entities"].is_null()) { return; @@ -96,12 +96,36 @@ void Scene::loadScene(nlohmann::json& startScene) } } } + + // load meshes from components + auto mcView = view(); + for (auto entity : mcView) + { + MeshComponent& mesh = mcView.get(entity); + rm->loadMesh(&mesh); + rm->loadTexture(&mesh); + } + + // initialize rigid bodies + if (sys::gPhysics != nullptr && !sys::gPhysics->isInitialized()) + { + sys::gPhysics->initialize(); + } + auto rbView = view(); + auto tcView = view(); + for (auto entity : rbView) + { + RigidbodyComponent* rbc = &rbView.get(entity); + TransformComponent* tc = &tcView.get(entity); + sys::gPhysics->addRigidBody(rbc, tc); + } } -nlohmann::json Scene::getScene() const { return sceneJson; } +nlohmann::json Scene::getScene() const { return sceneQueue.front(); } nlohmann::json Scene::saveScene() { + json sceneJson = sceneQueue.front(); sceneJson["entities"] = json::array(); auto view = registry.view(); for (auto& entity : view) @@ -191,9 +215,9 @@ void Scene::destroyEntity(Entity entity) registry.destroy((entt::entity)entity.getUID()); } -#ifdef GOOP_APPTYPE_EDITOR void Scene::resetScene() { + json sceneJson = sceneQueue.front(); if (sceneJson["entities"].is_null()) { return; @@ -237,4 +261,21 @@ void Scene::resetScene() } spawnedEntities.clear(); } -#endif + +void Scene::clearScene() +{ + for (auto e : spawnedEntities) + { + destroyEntity(goop::Entity(e, this)); + } + spawnedEntities.clear(); + + auto view = registry.view(); + for (auto entity : view) + { + destroyEntity(goop::Entity(entity, this)); + } + + currentCamera->setPosition(glm::vec3(0.f, 0.f, 5.f)); + currentCamera->setRotation(glm::vec3(0.f, 0.f, 0.f)); +} diff --git a/goop/Scene.h b/goop/Scene.h index 572315d..6a624ec 100644 --- a/goop/Scene.h +++ b/goop/Scene.h @@ -5,6 +5,7 @@ #include #include +#include using json = nlohmann::json; namespace goop @@ -15,7 +16,7 @@ class Scene public: Entity createEntity(const std::string& tag = "Entity"); void destroyEntity(Entity entity); - void loadScene(nlohmann::json& scene); + void loadScene(); nlohmann::json getScene() const; nlohmann::json saveScene(); @@ -30,18 +31,28 @@ class Scene void addSpawnedEntity(entt::entity entity) { spawnedEntities.push_back(entity); } -#ifdef GOOP_APPTYPE_EDITOR void resetScene(); -#endif + void clearScene(); + void nextScene() + { + if (sceneQueue.empty()) + { + return; + } + clearScene(); + sceneQueue.pop(); + loadScene(); + } + void queueScene(nlohmann::json scene) { sceneQueue.push(scene); } private: entt::registry registry; friend class Entity; - nlohmann::json sceneJson; - Camera* currentCamera = nullptr; std::vector spawnedEntities; + + std::queue sceneQueue; }; } // namespace goop diff --git a/lvl.json b/lvl.json index d2ce086..d524f60 100644 --- a/lvl.json +++ b/lvl.json @@ -6,71 +6,100 @@ "components": [ { "position": { - "x": -5.0, - "y": -2.0, - "z": -11.600000381469727 + "x": 0, + "y": 5, + "z": 0 }, "rotation": { - "x": 0.0, - "y": -2.4000000953674316, - "z": 0.0 + "x": 0, + "y": 0, + "z": 0 }, "scale": { - "x": 100.0, - "y": 1.0, - "z": 100.0 + "x": 1, + "y": 1, + "z": 1 }, "type": "transform" }, - { - "path": "box", - "primitive": "box", - "texturePath": "res/texture.jpg", - "type": "mesh" - }, { "box": { - "x": 100.0, - "y": 1.0, - "z": 100.0 + "x": 1, + "y": 1, + "z": 1 }, - "mass": 0.0, + "mass": 1, "type": "rigidBody" + }, + { + "active": true, + "position": { + "x": 0, + "y": 0, + "z": 0 + }, + "rotation": { + "x": 0, + "y": 0, + "z": 0 + }, + "type": "camera" + }, + { + "type": "PlayerInput" + }, + { + "type": "WindowsTest" } ], - "name": "floor" + "name": "Player" }, { "components": [ { "position": { - "x": 0.0, - "y": 5.0, - "z": 0.0 + "x": -5, + "y": -2, + "z": -11.600000381469727 }, "rotation": { - "x": 0.0, - "y": 0.0, - "z": 0.0 + "x": 0, + "y": -2.4000000953674316, + "z": 0 }, "scale": { - "x": 1.0, - "y": 1.0, - "z": 1.0 + "x": 100, + "y": 1, + "z": 100 }, "type": "transform" }, + { + "path": "box", + "primitive": "box", + "texturePath": "res/texture.jpg", + "type": "mesh" + }, { "box": { - "x": 1.0, - "y": 1.0, - "z": 1.0 + "x": 100, + "y": 1, + "z": 100 }, - "mass": 1.0, + "mass": 0, "type": "rigidBody" - }, + } + ], + "name": "floor" + } + ], + "name": "lvl_1" + }, + { + "entities": [ + { + "components": [ { - "active": true, "position": { "x": 0.0, "y": 0.0, @@ -81,16 +110,23 @@ "y": 0.0, "z": 0.0 }, - "type": "camera" + "scale": { + "x": 1.0, + "y": 1.0, + "z": 1.0 + }, + "type": "transform" }, { - "type": "PlayerInput" + "path": "res/viking_room.obj", + "texturePath": "res/viking_room.png", + "type": "mesh" } ], - "name": "Player" + "name": "FloorLvl2" } ], - "name": "lvl_1" + "name": "lvl_2" } ] } \ No newline at end of file