86 lines
1.8 KiB
C++
86 lines
1.8 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <vector>
|
|
#include <Layer.hpp>
|
|
#include <JJX/JSON.hpp>
|
|
#include <Colors.hpp>
|
|
#include <Utils.hpp>
|
|
|
|
using namespace JJX;
|
|
|
|
const float REDACTED_EDITOR_LIB_VERSION = 1;
|
|
|
|
class Level {
|
|
public:
|
|
std::string name;
|
|
std::string description;
|
|
std::string author;
|
|
std::vector<std::string> tags;
|
|
int rows;
|
|
int cols;
|
|
std::vector<Layer> layers;
|
|
std::string tileset_path;
|
|
json::value metadata;
|
|
|
|
Level() {}
|
|
|
|
explicit Level(const std::filesystem::path& path)
|
|
{
|
|
auto [json, err] = json::parse(read_file_contents(path));
|
|
Deserialize(json);
|
|
}
|
|
|
|
explicit Level(const json::value& json) {
|
|
Deserialize(json);
|
|
}
|
|
|
|
void Deserialize(const json::value& json)
|
|
{
|
|
name = json["name"].as_string();
|
|
description = json["description"].as_string();
|
|
author = json["author"].as_string();
|
|
tileset_path = json["tileset-path"].as_string();
|
|
|
|
auto layer_json_array = json["layers"].as_array();
|
|
|
|
for (auto& layer_json : layer_json_array)
|
|
{
|
|
layers.push_back(Layer(layer_json));
|
|
}
|
|
|
|
}
|
|
|
|
json::value Serialize() const
|
|
{
|
|
json::object data;
|
|
data["name"] = name;
|
|
data["description"] = description;
|
|
data["author"] = author;
|
|
data["tileset-path"] = tileset_path;
|
|
data["editor-version"] = REDACTED_EDITOR_LIB_VERSION;
|
|
data["map-type"] = "OrthogonalTileMap";
|
|
data["map-rows"] = (double)rows;
|
|
data["map-cols"] = (double)cols;
|
|
data["tile-width"] = 16.f;
|
|
data["tile-height"] = 16.f;
|
|
data["bgcolor"] = JsonConversions::deparse_color_to_hex(Colors::Black);
|
|
data["tags"] = JsonConversions::deparse_string_list(tags);
|
|
|
|
json::array layers;
|
|
for (auto& layer : this->layers)
|
|
{
|
|
layers.push_back(layer.Serialize());
|
|
}
|
|
data["layers"] = layers;
|
|
|
|
return data;
|
|
}
|
|
|
|
void Deserialize(const json::value& json) const {
|
|
|
|
}
|
|
|
|
protected:
|
|
private:
|
|
};
|