15#include <unordered_map>
16#include <nlohmann/json.hpp>
23 virtual void Execute() = 0;
24 virtual void Undo() = 0;
30 glm::vec3 m_oldPos, m_oldScale, m_oldRot;
31 glm::vec3 m_newPos, m_newScale, m_newRot;
37 glm::vec3 oldP, glm::vec3 oldR, glm::vec3 oldS,
38 glm::vec3 newP, glm::vec3 newR, glm::vec3 newS)
40 m_oldPos(oldP), m_oldRot(oldR), m_oldScale(oldS),
41 m_newPos(newP), m_newRot(newR), m_newScale(newS)
43 m_valid = (obj !=
nullptr);
51 tc.rotation = m_newRot;
52 tc.setLocalScale(m_newScale);
54 tc.enableLastTransformed();
62 tc.rotation = m_oldRot;
63 tc.setLocalScale(m_oldScale);
65 tc.enableLastTransformed();
75 std::unordered_map<std::string, nlohmann::json> components;
81 std::string m_sceneName;
83 std::vector<SerializedObject> m_serializedData;
85 std::vector<vex::Entity> m_runtimeIDs;
90 outList.push_back(obj);
92 auto& registry = m_engine.getRegistry();
97 auto* childObj = m_engine.getSceneManager()->GetScene(m_sceneName)->GetGameObjectByEntity(entity);
112 std::vector<vex::GameObject*> allObjects;
113 for (
auto* root : rootsToDelete) {
117 for (
auto* obj : allObjects) {
122 data.name =
"Unnamed";
124 data.type = obj->getObjectType();
125 data.originalID = obj->GetEntity();
133 const auto& regNames = vex::ComponentRegistry::getInstance().getRegisteredNames();
134 for (
const auto& compName : regNames) {
135 nlohmann::json json = vex::ComponentRegistry::getInstance().saveComponent(*obj, compName);
136 if (!json.is_null()) {
137 data.components[compName] = json;
140 m_serializedData.push_back(data);
146 if (m_runtimeIDs.empty())
return;
148 auto* scene = m_engine.getSceneManager()->GetScene(m_sceneName);
151 m_engine.WaitForGpu();
155 auto* obj = scene->GetGameObjectByEntity(entity);
157 scene->DestroyGameObject(obj);
159 m_engine.getRegistry().destroy(entity);
164 m_runtimeIDs.clear();
165 m_engine.refreshForObject();
170 auto* scene = m_engine.getSceneManager()->GetScene(m_sceneName);
173 std::unordered_map<vex::Entity, vex::Entity> oldToNewIDMap;
174 m_runtimeIDs.clear();
176 for (
const auto& data : m_serializedData) {
178 if (!newObj)
continue;
180 for (
const auto& [compName, jsonData] : data.components) {
181 vex::ComponentRegistry::getInstance().loadComponent(*newObj, compName, jsonData);
184 scene->AddEditorGameObject(newObj);
187 oldToNewIDMap[data.originalID] = newID;
188 m_runtimeIDs.push_back(newID);
191 auto& registry = m_engine.getRegistry();
193 for (
const auto& data : m_serializedData) {
194 if (!oldToNewIDMap.contains(data.originalID))
continue;
196 vex::Entity newEntityID = oldToNewIDMap[data.originalID];
202 if (oldToNewIDMap.contains(oldParent)) {
203 tc.setParent(oldToNewIDMap[oldParent]);
205 tc.setParent(oldParent);
210 m_engine.refreshForObject();
Contains basic components like transform, camera, name components..
Contains ComponentRegistry class used to have register of posible to create components....
This file defines the GameObject class.
This file defines SceneManager class.
void Execute() override
Executes the delete command.
void GatherHierarchy(vex::GameObject *obj, std::vector< vex::GameObject * > &outList)
Gather scene hierarhy for later rebuild.
void Undo() override
Undos the delete command with saved/serialized data.
DeleteCommand(vex::Engine &engine, std::vector< vex::GameObject * > rootsToDelete)
Serialized object data for deletion.
Class for interaction with engine systems.
SceneManager * getSceneManager()
Returns pointer to SceneManager.
GameObject * create(const std::string &type, Engine &engine, const std::string &name)
Create a GameObject instance of the specified type string.
static GameObjectFactory & getInstance()
Get the singleton instance of the GameObjectFactory.
Its base class for all game objects, eg. Player, Enemy, Weapon. Your Class needs to inherit from it.
vex::Entity GetEntity() const
Function that returns entity object, which is the entity associated with this GameObject....
Provides iteration over entities that have all specified component types.
void each(Func func)
Iterates over all entities with the specified component types.
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
constexpr Entity NULL_ENTITY
Special entity value indicating an invalid or null entity.
Struct that simply contains name of the entity. It is used to identify entity and needs to be unique.
Serialized object data for recreation on undo.