Skip to content

Commit

Permalink
scene queue
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 5, 2024
1 parent aca649e commit d187e69
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 82 deletions.
5 changes: 4 additions & 1 deletion cfg.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"game_file":"lvl.json","start_scene":"lvl_1"}
{
"game_file":"lvl.json",
"scene_queue":["lvl_1", "lvl_2"]
}
2 changes: 1 addition & 1 deletion components/PlayerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions components/WindowsTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "WindowsTest.h"
#include <iostream>
#include <goop/Input.h>

// Gets called when the game starts
void WindowsTest::init()
Expand All @@ -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
Expand Down
25 changes: 15 additions & 10 deletions game/GameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <imgui.h>
#include <iostream>

#include "components/CustomComponents.h"
#include <goop/Core.h>
#include <goop/sys/Renderer.h>
#include <goop/sys/Sfx.h>
#include <json.hpp>
#include "components/CustomComponents.h"
using json = nlohmann::json;

goop::App* goop::createGame(int argc, char** argv, Scene* scene) { return new GameApp(scene); }
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions goop/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class CustomComponent
return e;
}
}
void nextScene()
{
auto scene = entity.getScene();
scene->nextScene();
}
std::string name;
goop::Entity entity;

Expand Down
21 changes: 0 additions & 21 deletions goop/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RigidbodyComponent>();
auto tcView = scene.view<TransformComponent>();
for (auto entity : rbView)
{
RigidbodyComponent* rbc = &rbView.get<RigidbodyComponent>(entity);
TransformComponent* tc = &tcView.get<TransformComponent>(entity);
sys::gPhysics->addRigidBody(rbc, tc);
}
#endif

// load meshes from components
auto mcView = scene.view<MeshComponent>();
for (auto entity : mcView)
{
MeshComponent& mesh = mcView.get<MeshComponent>(entity);
rm->loadMesh(&mesh);
rm->loadTexture(&mesh);
}
}

void Core::run()
Expand Down
51 changes: 46 additions & 5 deletions goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ std::optional<Entity> 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;
Expand Down Expand Up @@ -96,12 +96,36 @@ void Scene::loadScene(nlohmann::json& startScene)
}
}
}

// load meshes from components
auto mcView = view<MeshComponent>();
for (auto entity : mcView)
{
MeshComponent& mesh = mcView.get<MeshComponent>(entity);
rm->loadMesh(&mesh);
rm->loadTexture(&mesh);
}

// initialize rigid bodies
if (sys::gPhysics != nullptr && !sys::gPhysics->isInitialized())
{
sys::gPhysics->initialize();
}
auto rbView = view<RigidbodyComponent>();
auto tcView = view<TransformComponent>();
for (auto entity : rbView)
{
RigidbodyComponent* rbc = &rbView.get<RigidbodyComponent>(entity);
TransformComponent* tc = &tcView.get<TransformComponent>(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<TagComponent>();
for (auto& entity : view)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<TagComponent>();
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));
}
21 changes: 16 additions & 5 deletions goop/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <entt.hpp>

#include <json.hpp>
#include <queue>
using json = nlohmann::json;

namespace goop
Expand All @@ -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();

Expand All @@ -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<entt::entity> spawnedEntities;

std::queue<nlohmann::json> sceneQueue;
};
} // namespace goop
Loading

0 comments on commit d187e69

Please sign in to comment.