summaryrefslogtreecommitdiff
path: root/src/engine/editor/SceneManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/editor/SceneManager.cpp')
-rw-r--r--src/engine/editor/SceneManager.cpp142
1 files changed, 116 insertions, 26 deletions
diff --git a/src/engine/editor/SceneManager.cpp b/src/engine/editor/SceneManager.cpp
index ae41f2f1d..bd3414474 100644
--- a/src/engine/editor/SceneManager.cpp
+++ b/src/engine/editor/SceneManager.cpp
@@ -1,11 +1,12 @@
#include "SceneManager.h"
#include "port/Game.h"
-#include "CoreMath.h"
+#include "engine/CoreMath.h"
#include "World.h"
#include "GameObject.h"
#include <iostream>
#include <fstream>
+#include <optional> // Must be before json.hpp
#include <nlohmann/json.hpp>
#include "port/Engine.h"
#include <libultraship/src/resource/type/Json.h>
@@ -13,6 +14,19 @@
#include <libultraship/src/resource/File.h>
#include "port/resource/type/ResourceType.h"
+#include "engine/vehicles/Train.h"
+
+#include "engine/objects/Object.h"
+#include "engine/objects/Thwomp.h"
+#include "engine/objects/Snowman.h"
+#include <iostream>
+
+extern "C" {
+#include "common_structs.h"
+#include "actors.h"
+#include "actor_types.h"
+}
+
namespace Editor {
std::shared_ptr<Ship::Archive> CurrentArchive;
@@ -33,45 +47,42 @@ namespace Editor {
}
data["StaticMeshActors"] = staticMesh;
- // nlohmann::json actors;
-
- // for (const auto& actor : gWorldInstance.Actors) {
- // actors.push_back(actor->to_json());
- // }
- // data["Actors"] = actors;
+ nlohmann::json actors;
- // nlohmann::json objects;
+ SaveActors(actors);
- // for (const auto& object : gWorldInstance.Objects) {
- // objects.push_back(object->to_json());
- // }
- // data["Objects"] = objects;
+ data["Actors"] = actors;
try {
- auto dat = data.dump();
+ auto dat = data.dump(2);
std::vector<uint8_t> stringify;
stringify.assign(dat.begin(), dat.end());
bool wrote = GameEngine::Instance->context->GetResourceManager()->GetArchiveManager()->WriteFile(CurrentArchive, SceneFile, stringify);
if (wrote) {
- printf("Successfully wrote scene file!\n Wrote: %s\n", SceneFile.c_str());
+ // Tell the cache this needs to be reloaded
+ auto resource = GameEngine::Instance->context->GetResourceManager()->GetCachedResource(SceneFile);
+ if (resource) {
+ resource->Dirty();
+ }
} else {
- printf("Failed to write scene file!\n");
+ printf("[SceneManager::SaveLevel] Failed to write scene file!\n");
}
} catch (const nlohmann::json::exception& e) {
- printf("SceneManager::SaveLevel():\n JSON error during dump: %s\n", e.what());
+ printf("[SceneManager::SaveLevel]\n JSON error during dump: %s\n", e.what());
}
} else {
printf("Could not save scene file, SceneFile or CurrentArchive not set\n");
}
}
- void LoadLevel(std::shared_ptr<Ship::Archive> archive, Course* course, std::string sceneFile) {
- SceneFile = sceneFile;
- if (archive && (course != nullptr)) {
+ /** Do not use gWorldInstance.CurrentCourse during loading! The current track is not guaranteed! **/
+ void LoadLevel(Course* course, std::string sceneFile) {
+ SceneFile = sceneFile;
+ if ((nullptr != course) && (nullptr != course->RootArchive)) {
auto initData = std::make_shared<Ship::ResourceInitData>();
- initData->Parent = archive;
+ initData->Parent = course->RootArchive;
initData->Format = RESOURCE_FORMAT_BINARY;
initData->ByteOrder = Ship::Endianness::Little;
initData->Type = static_cast<uint32_t>(Ship::ResourceType::Json);
@@ -97,6 +108,20 @@ namespace Editor {
std::cerr << "Props data not found in the JSON file!" << std::endl;
}
+ /** Populate Track SpawnParams for spawning actors **/
+ if (data.contains("Actors")) {
+ auto & actorsJson = data["Actors"];
+ course->SpawnList.clear();
+ for (const auto& actor : actorsJson) {
+ SpawnParams params;
+ params.from_json(actor); //<SpawnParams>();
+ if (!params.Name.empty()) {
+ course->SpawnList.push_back(params);
+ }
+ }
+ SPDLOG_INFO("[SceneManager] Loaded Scene File!");
+ }
+
// Load the Actors (deserialize them)
if (data.contains("StaticMeshActors")) {
auto& actorsJson = data["StaticMeshActors"];
@@ -106,7 +131,7 @@ namespace Editor {
Load_AddStaticMeshActor(actorJson);
}
} else {
- std::cerr << "Actors data not found in the JSON file!" << std::endl;
+ SPDLOG_INFO("[SceneManager::LoadLevel] [scene.json] This track contains no StaticMeshActors!");
}
}
}
@@ -118,8 +143,6 @@ namespace Editor {
printf("After from_json: Pos(%f, %f, %f), Name: %s, Model: %s\n",
actor->Pos.x, actor->Pos.y, actor->Pos.z, actor->Name.c_str(), actor->Model.c_str());
- gEditor.AddObject(actor->Name.c_str(), &actor->Pos, &actor->Rot, &actor->Scale, (Gfx*) nullptr, 1.0f,
- GameObject::CollisionType::BOUNDING_BOX, 20.0f, (int32_t*) &actor->bPendingDestroy, (int32_t) 1);
}
void SetSceneFile(std::shared_ptr<Ship::Archive> archive, std::string sceneFile) {
@@ -127,11 +150,11 @@ namespace Editor {
SceneFile = sceneFile;
}
- void LoadMinimap(std::shared_ptr<Ship::Archive> archive, Course* course, std::string filePath) {
+ void LoadMinimap(Course* course, std::string filePath) {
printf("LOADING MINIMAP %s\n", filePath.c_str());
- if (archive) {
+ if ((nullptr != course) && (nullptr != course->RootArchive)) {
auto initData = std::make_shared<Ship::ResourceInitData>();
- initData->Parent = archive;
+ initData->Parent = course->RootArchive;
initData->Format = RESOURCE_FORMAT_BINARY;
initData->ByteOrder = Ship::Endianness::Little;
initData->Type = static_cast<uint32_t>(MK64::ResourceType::Minimap);
@@ -153,4 +176,71 @@ namespace Editor {
}
}
}
+
+ void SaveActors(nlohmann::json& actorList) {
+ for (const auto& actor : gWorldInstance.Actors) {
+ SpawnParams params{};
+ bool alreadyProcessed = false;
+
+ // Only some actors are supported for saving.
+ // Bananas and stuff don't make sense to be saved.
+ switch(actor->Type) {
+ case ACTOR_ITEM_BOX:
+ case ACTOR_FAKE_ITEM_BOX:
+ case ACTOR_TREE_MARIO_RACEWAY:
+ case ACTOR_TREE_YOSHI_VALLEY:
+ case ACTOR_TREE_ROYAL_RACEWAY:
+ case ACTOR_TREE_MOO_MOO_FARM:
+ case ACTOR_PALM_TREE:
+ case ACTOR_TREE_LUIGI_RACEWAY: // A plant?
+ case ACTOR_UNKNOWN_0x1B:
+ case ACTOR_TREE_PEACH_CASTLE:
+ case ACTOR_TREE_FRAPPE_SNOWLAND:
+ case ACTOR_CACTUS1_KALAMARI_DESERT:
+ case ACTOR_CACTUS2_KALAMARI_DESERT:
+ case ACTOR_CACTUS3_KALAMARI_DESERT:
+ case ACTOR_BUSH_BOWSERS_CASTLE:
+ params.Name = get_actor_resource_location_name(actor->Type);
+ params.Location = FVector(actor->Pos[0], actor->Pos[1], actor->Pos[2]);
+ if (!params.Name.empty()) {
+ actorList.push_back(params.to_json());
+ }
+ alreadyProcessed = true;
+ break;
+ case ACTOR_PIRANHA_PLANT:
+ params.Name = get_actor_resource_location_name(actor->Type);
+ params.Location = FVector(actor->Pos[0], actor->Pos[1], actor->Pos[2]);
+ // params.Type = // Need this to use royal raceway version
+ actorList.push_back(params.to_json());
+ alreadyProcessed = true;
+ break;
+ case ACTOR_YOSHI_EGG:
+ params.Name = get_actor_resource_location_name(actor->Type);
+ params.Location = FVector(actor->Velocity[0], actor->Pos[1], actor->Velocity[2]); // Velocity is pathCenter
+ if (!params.Name.empty()) {
+ actorList.push_back(params.to_json());
+ }
+ alreadyProcessed = true;
+ break;
+ }
+
+ if (!alreadyProcessed) {
+ actor->SetSpawnParams(params);
+ if (!params.Name.empty()) {
+ actorList.push_back(params.to_json());
+ }
+ }
+ }
+
+ for (const auto& object : gWorldInstance.Objects) {
+ SpawnParams params;
+ object->SetSpawnParams(params);
+
+ // Unimplemented objects should not be added to the SpawnList
+ // The name field is required. If not set, then its not implemented yet.
+ if (!params.Name.empty()) {
+ actorList.push_back(params.to_json());
+ }
+ }
+ }
}