SceneManager nearly working

This commit is contained in:
MegaMech
2025-03-23 18:28:25 -06:00
parent 1dee7001f6
commit 8fe7732039
12 changed files with 238 additions and 135 deletions
+2
View File
@@ -150,6 +150,8 @@ namespace Editor {
eObjectPicker.eGizmo.dimensions.MinZ = minZ + -1000;
eObjectPicker.eGizmo.dimensions.MaxZ = maxZ + 1000;
}
// This is more of a reset
void Editor::NewTrack() {
auto course = gWorldInstance.CurrentCourse = new Course();
course->Props.New();
-85
View File
@@ -1,85 +0,0 @@
#include "SaveLevel.h"
#include "port/Game.h"
#include "CoreMath.h"
#include "World.h"
#include "GameObject.h"
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
namespace Editor {
// SaveLevel::SaveLevel() {}
void SaveLevel() {
auto props = gWorldInstance.CurrentCourse->Props;
nlohmann::json data;
data["Props"] = props.to_json();
nlohmann::json actors;
/* for (const auto& actor : gWorldInstance.StaticMeshActors) {
actors.push_back(actor->to_json());
}
data["Actors"] = actors;*/
std::ofstream file("track.json");
file << data.dump(4); // Pretty print with indent
}
void LoadLevel() {
// Open the JSON file
std::ifstream file("track.json");
if (!file.is_open()) {
std::cerr << "Failed to open track.json for reading!" << std::endl;
return;
}
// Check if level data file is empty
if (file.peek() == std::ifstream::traits_type::eof()) {
return;
}
// Parse the JSON file into a nlohmann::json object
nlohmann::json data;
file >> data;
// Load the Props (deserialize it)
if (data.contains("Props")) {
auto& propsJson = data["Props"];
gWorldInstance.CurrentCourse->Props.from_json(propsJson); // Assuming you have a `from_json` function
} else {
std::cerr << "Props data not found in the JSON file!" << std::endl;
}
// Load the Actors (deserialize them)
if (data.contains("Actors")) {
auto& actorsJson = data["Actors"];
gWorldInstance.StaticMeshActors.clear(); // Clear existing actors, if any
for (const auto& actorJson : actorsJson) {
Load_AddStaticMeshActor(actorJson);
}
} else {
std::cerr << "Actors data not found in the JSON file!" << std::endl;
}
// Close the file after loading
file.close();
}
void Load_AddStaticMeshActor(const nlohmann::json& actorJson) {
gWorldInstance.StaticMeshActors.push_back(new StaticMeshActor("", FVector(0, 0, 0), IRotator(0, 0, 0), FVector(1, 1, 1), "", nullptr));
auto actor = gWorldInstance.StaticMeshActors.back();
actor->from_json(actorJson);
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, (Vec3s*)&actor->Rot, &actor->Scale, (Gfx*) nullptr, 1.0f,
GameObject::CollisionType::BOUNDING_BOX, 20.0f, (int32_t*) &actor->bPendingDestroy, (int32_t) 1);
}
}
-8
View File
@@ -1,8 +0,0 @@
#include <libultraship.h>
#include "Course.h"
namespace Editor {
void SaveLevel();
void LoadLevel();
void Load_AddStaticMeshActor(const nlohmann::json& actorJson);
}
+108
View File
@@ -0,0 +1,108 @@
#include "SceneManager.h"
#include "port/Game.h"
#include "CoreMath.h"
#include "World.h"
#include "GameObject.h"
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include "port/Engine.h"
#include <libultraship/src/resource/type/json.h>
#include <libultraship/src/resource/File.h>
namespace Editor {
std::shared_ptr<Ship::Archive> CurrentArchive;
std::string SceneFile = "";
void SaveLevel() {
auto props = gWorldInstance.CurrentCourse->Props;
if ((CurrentArchive) && (!SceneFile.empty())) {
nlohmann::json data;
data["Props"] = props.to_json();
nlohmann::json actors;
// for (const auto& actor : gWorldInstance.StaticMeshActors) {
// actors.push_back(actor->to_json());
// }
// data["Actors"] = actors;
auto dat = data.dump();
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");
} else {
printf("Failed to write scene file!\n");
}
} else {
printf("Could not save scene file, SceneFile or CurrentArchive not set\n");
}
}
void LoadLevel(std::string sceneFile) {
printf("LOAD TRACK PROPS %s\n", sceneFile.c_str());
nlohmann::json data;
CurrentArchive = GameEngine::Instance->context->GetResourceManager()->GetArchiveManager()->GetArchiveFromFile(sceneFile);
SceneFile = sceneFile;
if (CurrentArchive) {
auto initData = std::make_shared<Ship::ResourceInitData>();
initData->Parent = CurrentArchive;
initData->Format = RESOURCE_FORMAT_BINARY;
initData->ByteOrder = Ship::Endianness::Little;
initData->Type = static_cast<uint32_t>(Ship::ResourceType::Json);
initData->ResourceVersion = 0;
nlohmann::json data = std::static_pointer_cast<Ship::Json>(
GameEngine::Instance->context->GetResourceManager()->LoadResource(sceneFile, true, initData))->Data;
if (data == nullptr) {
return;
}
// Load the Props (deserialize it)
if (data.contains("Props")) {
auto& propsJson = data["Props"];
gWorldInstance.CurrentCourse->Props.from_json(propsJson); // Assuming you have a `from_json` function
} else {
std::cerr << "Props data not found in the JSON file!" << std::endl;
}
// Load the Actors (deserialize them)
if (data.contains("Actors")) {
auto& actorsJson = data["Actors"];
gWorldInstance.StaticMeshActors.clear(); // Clear existing actors, if any
for (const auto& actorJson : actorsJson) {
Load_AddStaticMeshActor(actorJson);
}
} else {
std::cerr << "Actors data not found in the JSON file!" << std::endl;
}
}
}
void Load_AddStaticMeshActor(const nlohmann::json& actorJson) {
gWorldInstance.StaticMeshActors.push_back(new StaticMeshActor("", FVector(0, 0, 0), IRotator(0, 0, 0), FVector(1, 1, 1), "", nullptr));
auto actor = gWorldInstance.StaticMeshActors.back();
actor->from_json(actorJson);
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, (Vec3s*)&actor->Rot, &actor->Scale, (Gfx*) nullptr, 1.0f,
GameObject::CollisionType::BOUNDING_BOX, 20.0f, (int32_t*) &actor->bPendingDestroy, (int32_t) 1);
}
void SetSceneFile(std::string sceneFile) {
SceneFile = sceneFile;
}
}
+12
View File
@@ -0,0 +1,12 @@
#include <libultraship.h>
#include "Course.h"
namespace Editor {
void SaveLevel();
void LoadLevel(std::string sceneFile);
void Load_AddStaticMeshActor(const nlohmann::json& actorJson);
void SetSceneFile(std::string sceneFile);
extern std::shared_ptr<Ship::Archive> CurrentArchive; // This is used to retrieve and write the scene data file
extern std::string SceneFile;
}