Skip to content

Commit

Permalink
delete entities
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Dec 31, 2023
1 parent d50349c commit d2e3d65
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 36 deletions.
144 changes: 136 additions & 8 deletions editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>

#include <goop/Core.h>
#include <goop/Entity.h>
#include <goop/sys/Renderer.h>
#include <goop/sys/Sfx.h>

Expand Down Expand Up @@ -139,14 +140,13 @@ void EditorApp::gui()
ImGui::Text("Scene View");
if (ImGui::Button("Add Entity"))
{
popupOpen = true;
addEntityPopupOpen = true;
ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);
ImGui::SetNextWindowPos(centerPos, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
}
ImGui::End();

if (popupOpen)
if (addEntityPopupOpen)
{
ImGui::OpenPopup("AddEntity");

Expand All @@ -160,21 +160,149 @@ void EditorApp::gui()
ImGui::SetNextWindowSize(popupSize, ImGuiCond_Always);
}

// Popup
// AddEntity Popup
if (ImGui::BeginPopup("AddEntity"))
{
ImGui::Text("This is a Popup!");
if (ImGui::Button("Close"))
ImGui::Text("Add Entity");
ImGui::InputText("Name", entityName, 256);
if (ImGui::Button("Add"))
{
auto e = scene->createEntity(entityName);
e.addComponent<goop::TransformComponent>();
ImGui::CloseCurrentPopup();
addEntityPopupOpen = false;
}
if (ImGui::Button("Cancel"))
{
ImGui::CloseCurrentPopup();
popupOpen = false;
addEntityPopupOpen = false;
}
ImGui::EndPopup();
}

auto view = scene->view<goop::TagComponent>();
for (auto entity : view)
{
auto e = goop::Entity(entity, scene);
auto& tag = view.get<goop::TagComponent>(entity).tag;
if (ImGui::Selectable(tag.c_str(), selectedEntity.has_value() &&
selectedEntity.value().getUID() == (uint32_t)entity))
{
selectedEntity = e;
}
}
ImGui::End();

// Inspector
ImGui::Begin("Inspector");
ImGui::Text("Inspector");
ImGui::Text("%s", !selectedEntity.has_value()
? "No Entity Selected"
: selectedEntity.value().getComponent<goop::TagComponent>().tag.c_str());
if (selectedEntity.has_value())
{
auto e = selectedEntity.value();
if (ImGui::Button("Delete Entity"))
{
scene->destroyEntity(e);
addEntityPopupOpen = false;
addComponentPopupOpen = false;
selectedEntity = std::nullopt;
ImGui::End();
return;
}
if (e.hasComponent<goop::TransformComponent>())
{
ImGui::Text("Transform");
auto& transform = e.getComponent<goop::TransformComponent>().transform;
ImGui::DragFloat3("Position", &transform[3][0], 0.1f);
}
if (e.hasComponent<goop::MeshComponent>())
{
ImGui::Text("Mesh");
auto& mesh = e.getComponent<goop::MeshComponent>();
ImGui::Text("%s", mesh.path.c_str());
if (ImGui::Button("Change Mesh"))
{
changeMeshPopupOpen = true;
ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);
ImGui::SetNextWindowPos(centerPos, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
}
if (changeMeshPopupOpen)
{
ImGui::OpenPopup("ChangeMesh");

ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);

ImVec2 popupSize = ImVec2(200, 100); // Adjust the size if needed
ImVec2 popupPos =
ImVec2(centerPos.x - popupSize.x * 0.5f, centerPos.y - popupSize.y * 0.5f);
ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always);
ImGui::SetNextWindowSize(popupSize, ImGuiCond_Always);
}
if (ImGui::BeginPopup("ChangeMesh"))
{
ImGui::Text("Change Mesh");
ImGui::InputText("Path", mesh.path.data(), 256);
if (ImGui::Button("Change"))
{
goop::rm->loadMesh(mesh);
ImGui::CloseCurrentPopup();
changeMeshPopupOpen = false;
}
if (ImGui::Button("Cancel"))
{
ImGui::CloseCurrentPopup();
changeMeshPopupOpen = false;
}
ImGui::EndPopup();
}
}
if (!e.hasComponent<goop::TransformComponent>() ||
!e.hasComponent<goop::MeshComponent>() && ImGui::Button("Add Component"))
{
addComponentPopupOpen = true;
}
if (addComponentPopupOpen)
{
ImGui::OpenPopup("AddComponent");

ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);

ImVec2 popupSize = ImVec2(200, 100); // Adjust the size if needed
ImVec2 popupPos =
ImVec2(centerPos.x - popupSize.x * 0.5f, centerPos.y - popupSize.y * 0.5f);
ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always);
ImGui::SetNextWindowSize(popupSize, ImGuiCond_Always);
}

// AddComponent Popup
if (ImGui::BeginPopup("AddComponent"))
{
ImGui::Text("Add Component");
if (!e.hasComponent<goop::TransformComponent>() && ImGui::Button("Transform"))
{
e.addComponent<goop::TransformComponent>();
ImGui::CloseCurrentPopup();
addComponentPopupOpen = false;
}
if (!e.hasComponent<goop::MeshComponent>() && ImGui::Button("Mesh"))
{
e.addComponent<goop::MeshComponent>("res/viking_room.obj");
goop::rm->loadMesh(e.getComponent<goop::MeshComponent>());
ImGui::CloseCurrentPopup();
addComponentPopupOpen = false;
}
if (ImGui::Button("Cancel"))
{
ImGui::CloseCurrentPopup();
addComponentPopupOpen = false;
}
ImGui::EndPopup();
}
}
ImGui::End();

game->gui();
Expand Down
7 changes: 6 additions & 1 deletion editor/EditorApp.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define GOOP_APPTYPE_EDITOR

#include <goop/App.h>
#include <goop/Entity.h>

#ifndef GOOP_APPTYPE_EDITOR
#include <imgui.h>
Expand All @@ -23,5 +24,9 @@ class EditorApp : public goop::App

App* game;
bool shouldPlay = false;
bool popupOpen = false;
bool addEntityPopupOpen = false;
bool addComponentPopupOpen = false;
bool changeMeshPopupOpen = false;
char entityName[256] = "";
std::optional<goop::Entity> selectedEntity = std::nullopt;
};
1 change: 1 addition & 0 deletions goop/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Entity
template <typename T> void removeComponent() { scene->registry.remove<T>(entity); }

uint32_t getUID() { return (uint32_t)entity; }
entt::entity getEntity() { return entity; }

private:
entt::entity entity;
Expand Down
11 changes: 11 additions & 0 deletions goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Scene.h"
#include "goop/Components.h"
#include <goop/Core.h>
#include "goop/Entity.h"
#include <glm/common.hpp>
#include <glm/gtx/matrix_decompose.hpp>
Expand Down Expand Up @@ -119,3 +120,13 @@ nlohmann::json Scene::saveScene()

return sceneJson;
}

void Scene::destroyEntity(Entity entity)
{
if (entity.hasComponent<MeshComponent>())
{
goop::rm->unloadMesh(entity.getComponent<MeshComponent>());
}
registry.destroy((entt::entity)entity.getUID());
}

6 changes: 6 additions & 0 deletions goop/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Scene
~Scene();

Entity createEntity(const std::string& tag = "Entity");
void destroyEntity(Entity entity);
void loadScene(nlohmann::json& scene);
nlohmann::json getScene() const;
nlohmann::json saveScene();
Expand All @@ -26,6 +27,11 @@ class Scene

Entity getEntity(const std::string& tag);

bool hasEntity(entt::entity id) const
{
return registry.valid(id);
}

private:
entt::registry registry;
friend class Entity;
Expand Down
34 changes: 33 additions & 1 deletion goop/sys/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,26 @@ int ResourceManager::initialize()
return 0;
}

bool ResourceManager::loadMesh(MeshComponent& mesh)
bool ResourceManager::loadMesh(MeshComponent& mesh, const char* oldPath)
{
if (oldPath != nullptr)
{
if (numLoadedMeshes[oldPath] == 0)
{
std::cout << "Mesh " << oldPath << " not loaded" << std::endl;
}
else
{
numLoadedMeshes[oldPath]--;
if (numLoadedMeshes[oldPath] == 0)
{
std::cout << "Unloading mesh " << oldPath << std::endl;
meshLoader->unload(oldPath);
loadedMeshes.erase(oldPath);
}
}
}

if (numLoadedMeshes[mesh.path] > 0)
{
std::cout << "Mesh " << mesh.path << " already loaded" << std::endl;
Expand All @@ -45,6 +63,19 @@ bool ResourceManager::loadMesh(MeshComponent& mesh)
std::cout << "Loaded mesh " << mesh.path << " with id " << mesh.id << std::endl;
return true;
}

bool ResourceManager::unloadMesh(MeshComponent& mesh)
{
numLoadedMeshes[mesh.path]--;
if (numLoadedMeshes[mesh.path] == 0)
{
std::cout << "Unloading mesh " << mesh.path << std::endl;
meshLoader->unload(mesh.path);
loadedMeshes.erase(mesh.path);
}
return true;
}

bool ResourceManager::loadSfx(const std::string& path) { return sfx->load(path); }

int ResourceManager::destroy()
Expand All @@ -55,3 +86,4 @@ int ResourceManager::destroy()
}

void ResourceManager::playSfx(uint32_t id) const { sfx->playSfx(id); }

3 changes: 2 additions & 1 deletion goop/sys/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class ResourceManager : public Subsystem
virtual int initialize() final;
virtual int destroy() final;

virtual bool loadMesh(MeshComponent& mesh);
virtual bool loadMesh(MeshComponent& mesh, const char* oldPath = nullptr);
virtual bool unloadMesh(MeshComponent& mesh);
virtual bool loadSfx(const std::string& path);

void playSfx(uint32_t id) const;
Expand Down
1 change: 1 addition & 0 deletions goop/sys/ResourceSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ResourceSubsystem : public Subsystem
{
public:
virtual uint32_t load(const std::string& path) = 0;
virtual uint32_t unload(const std::string& path) = 0;
protected:
bool bIsInitialized = false;
};
Expand Down
8 changes: 8 additions & 0 deletions goop/sys/platform/assimp/MeshLoader_Assimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,17 @@ uint32_t MeshLoader_Assimp::load(const std::string& path)
}
}

loadedMeshes[path] = index;
return index;
}

MeshLoader_Assimp::MeshLoader_Assimp() {}

MeshLoader_Assimp::~MeshLoader_Assimp() {}

uint32_t MeshLoader_Assimp::unload(const std::string& path)
{
data->erase(data->begin() + loadedMeshes[path]);
return 0;
}

3 changes: 3 additions & 0 deletions goop/sys/platform/assimp/MeshLoader_Assimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <assimp/Importer.hpp>
#include <goop/sys/MeshLoader.h>
#include <map>

namespace goop::sys::platform::assimp
{
Expand All @@ -20,8 +21,10 @@ class MeshLoader_Assimp : public MeshLoader

// ResourceSubsystem Interface
uint32_t load(const std::string& path) final;
uint32_t unload(const std::string& path) final;

private:
Assimp::Importer importer;
std::map<std::string, uint32_t> loadedMeshes;
};
} // namespace goop::sys::platform::assimp
6 changes: 6 additions & 0 deletions goop/sys/platform/soloud/Sfx_SoLoud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ uint32_t Sfx_SoLoud::load(const std::string& path)
}

void Sfx_SoLoud::playSfx(uint32_t id) { engine.play(sfx[id]); }

uint32_t Sfx_SoLoud::unload(const std::string& path)
{
return 0;
}

1 change: 1 addition & 0 deletions goop/sys/platform/soloud/Sfx_SoLoud.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Sfx_SoLoud : public Sfx

// Resource Subsystem interface
virtual uint32_t load(const std::string& path) final;
virtual uint32_t unload(const std::string& path) final;

// Sfx interface
virtual void playSfx(uint32_t id) final;
Expand Down
Loading

0 comments on commit d2e3d65

Please sign in to comment.