diff options
| author | MegaMech <MegaMech@users.noreply.github.com> | 2025-11-09 19:07:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-09 19:07:44 -0700 |
| commit | de9c5d510175bba11ab8704e67be3a3a94f9149e (patch) | |
| tree | 34c42dbfe9f2a6eb5fbce5f6a31b532ff2a3f243 /src/engine/editor/SceneManager.cpp | |
| parent | 760fa3bf5226c2a6052a5d4d53fd0e27f2911e60 (diff) | |
Implement SpawnParams struct (#536)
* Impl SpawnParams
* Added json submodule
* Update json
* Update
* Works
* Remove comment
* Works refactor
* Snowman and thwomp working
* Impl hot air balloon
* More progress
* All OObjects are done
* cleanup
* Refactor 2Dpath to normal path
* Update nlohmann json & fix compile
* Rest of actors
* MORE CHANGES
* Finish actors
* Done PR, some fix to collision viewer
* Impl falling rocks
* Add const
* wip editor refactor
* Property work
* continue
* Overridable editor properties
* Actor saving/loading works now
* Fix light alignment
* Clarification
* Impl penguin
* params impl signs
* properties impl falling rock
* More property impls
* impl air balloon
* Add spawnParams to OObject Translate
* Snowman translate better
* impl hedgehog properly
* properties impl trophy
* thwomp progress
* Finish impl properties
* Fix compile
* Fix cursor collisions
* Move registered actors
* Rename pathPoint XYZ to xyz
* Fix editor pause bug
* Clean up
* Review comments
* Remove SpawnParams struct from actor classes
* Rename
* Player Label First Iteration
* Work now
* Working 3d text
* Fix boo bug
* Finish AText actor
* Fix spawnparams compile
* Register AText
* Finish Text Actor
* Fix thwomp interpolation
* Fix compile
* Fix crab and hedgehog
* Fix loading flagpole
* Fix Hot Air Balloon
* Turn zbuffer on for AText
* Update
---------
Co-authored-by: MegaMech <7255464+MegaMech@users.noreply.github.com>
Diffstat (limited to 'src/engine/editor/SceneManager.cpp')
| -rw-r--r-- | src/engine/editor/SceneManager.cpp | 142 |
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()); + } + } + } } |
