summaryrefslogtreecommitdiff
path: root/src/engine/editor/SceneManager.h
blob: 0d12da569959db12aa73fe11531bd1a17de5eb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include <libultraship/libultraship.h>
#include "engine/CoreMath.h"
#include "engine/tracks/Track.h"
#include <optional>
#include <nlohmann/json.hpp>

namespace TrackEditor {
    void SaveLevel(Track* track, const TrackInfo* info);
    void LoadTrackDataFromJson(Track* track, const std::string& trackPath);
    void LoadTrackInfo(TrackInfo& info, std::shared_ptr<Ship::Archive> archive, std::string sceneFile);
    void Load_AddStaticMeshActor(const nlohmann::json& actorJson);
    void LoadMinimap(Track* track, std::string filePath);
    void SetDefaultMinimap(Track* track);

    void SaveActors(nlohmann::json& actorList);
    void SaveStaticMeshActors(nlohmann::json& actorList);
    void SaveTour(Track* track, nlohmann::json& tour);
    void SaveFog(nlohmann::json& fog);

    void LoadProps(Track* track, nlohmann::json& data);
    void LoadPaths(Track* track, const std::string& trackPath);
    void LoadTrackInfoData(TrackInfo& info, nlohmann::json& data);
    void LoadActors(Track* track, nlohmann::json& data);
    void LoadStaticMeshActors(Track* track, nlohmann::json& data);
    void LoadTour(Track* track, nlohmann::json& data);
    void LoadFog(nlohmann::json& data);

    void SpawnActors(std::vector<std::pair<std::string, SpawnParams>> spawnList);

    inline nlohmann::json ToJson(const FVector& v) {
        return {
            {"x", v.x},
            {"y", v.y},
            {"z", v.z}
        };
    }

    inline nlohmann::json ToJson(const TourCamera::KeyFrame& kf) {
        return {
            {"Pos", ToJson(kf.Pos)},
            {"LookAt", ToJson(kf.LookAt)},
            {"Duration", kf.Duration}
        };
    }

    inline nlohmann::json ToJson(const TourCamera::CameraShot& shot) {
        nlohmann::json j;
        j["StartPos"]   = ToJson(shot.Pos);
        j["StartLookAt"] = ToJson(shot.LookAt);

        // KeyFrames
        nlohmann::json keyframes = nlohmann::json::array();
        for (const auto& kf : shot.Frames) {
            keyframes.push_back(ToJson(kf));
        }
        j["KeyFrames"] = keyframes;

        return j;
    }

    inline FVector FromJsonVec(const nlohmann::json& j) {
        FVector v{};
        if (j.contains("x")) v.x = j["x"].get<float>();
        if (j.contains("y")) v.y = j["y"].get<float>();
        if (j.contains("z")) v.z = j["z"].get<float>();
        return v;
    }

    inline TourCamera::KeyFrame FromJsonKeyFrame(const nlohmann::json& j) {
        TourCamera::KeyFrame kf{};
        if (j.contains("Pos"))     kf.Pos = FromJsonVec(j["Pos"]);
        if (j.contains("LookAt"))  kf.LookAt = FromJsonVec(j["LookAt"]);
        if (j.contains("Duration")) kf.Duration = j["Duration"].get<float>();
        return kf;
    }

    inline TourCamera::CameraShot FromJsonCameraShot(const nlohmann::json& j) {
        TourCamera::CameraShot shot{};
        if (j.contains("StartPos"))    shot.Pos = FromJsonVec(j["StartPos"]);
        if (j.contains("StartLookAt")) shot.LookAt = FromJsonVec(j["StartLookAt"]);

        if (j.contains("KeyFrames") && j["KeyFrames"].is_array()) {
            for (const auto& kfJson : j["KeyFrames"]) {
                shot.Frames.push_back(FromJsonKeyFrame(kfJson));
            }
        }

        return shot;
    }
}