Refactoring tile loading procedure.
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
#include <Core/Data.hpp>
|
||||
#include <Client/LocalWorld.hpp>
|
||||
|
||||
|
||||
namespace CaveGame::Client
|
||||
{
|
||||
/// The input and gui driver for the temporary tile hotbar.
|
||||
|
@@ -18,24 +18,6 @@ namespace CaveGame::ClientApp
|
||||
using namespace CaveGame::Core;
|
||||
using namespace CaveGame::Client;
|
||||
|
||||
std::string read_file(const std::string& file_path)
|
||||
{
|
||||
std::ifstream file(file_path, std::ios::binary);
|
||||
if (!file)
|
||||
throw std::runtime_error("We couldn't find the file: " + file_path);
|
||||
|
||||
std::streamsize file_size;
|
||||
file.seekg(0, std::ios::end);
|
||||
file_size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::string file_content(file_size, '\0');
|
||||
file.read(&file_content[0], file_size);
|
||||
file.close();
|
||||
|
||||
return file_content;
|
||||
}
|
||||
|
||||
void ReadRecipesAndRegister() {}
|
||||
|
||||
void ReadItemDataAndRegister() {
|
||||
@@ -68,74 +50,11 @@ namespace CaveGame::ClientApp
|
||||
}
|
||||
}
|
||||
|
||||
Color4 parse_color(const JJX::json::value& v) {
|
||||
if (v.type == JJX::json::value_type::string)
|
||||
return Color4::FromHex(v.string.value());
|
||||
else if (v.type == JJX::json::value_type::array)
|
||||
{
|
||||
auto color_array = JJX::json::array_val(v);
|
||||
int r = color_array[0].number.value();
|
||||
int g = color_array[1].number.value();
|
||||
int b = color_array[2].number.value();
|
||||
int a = 255;
|
||||
if (color_array.array.value().size() == 4)
|
||||
a = color_array[3].number.value();
|
||||
return Color4(r, g, b, a);
|
||||
|
||||
}
|
||||
return Colors::Transparent;
|
||||
}
|
||||
|
||||
void ReadTileDataAndRegister()
|
||||
{
|
||||
|
||||
|
||||
using namespace JJX;
|
||||
std::string content = read_file("assets/data/tiles.json");
|
||||
|
||||
auto [data, parse_error] = json::parse(content);
|
||||
|
||||
if (data.type == json::value_type::array) {
|
||||
auto arr_data = json::array_val(data);
|
||||
for (auto& t : arr_data.array.value()) {
|
||||
Core::Tile tile;
|
||||
Core::Item tile_item;
|
||||
|
||||
auto tile_meta_json = json::object_val(t);
|
||||
|
||||
if (tile_meta_json.object->contains("hardcoded-id"))
|
||||
tile.assigned_numeric_id = tile_meta_json["hardcoded-id"].number;
|
||||
|
||||
tile.mnemonic_id = tile_meta_json["mnemonic-id"];
|
||||
tile.display_name = tile_meta_json["display-name"];
|
||||
|
||||
tile_item.mnemonic = std::format("{}-tile", tile.mnemonic_id);
|
||||
tile_item.display_name = tile.display_name;
|
||||
|
||||
tile.color = parse_color(tile_meta_json["color"]);
|
||||
|
||||
if (tile_meta_json.object->contains("pallet") && tile_meta_json["pallet"].array->size() > 0) {
|
||||
std::vector<Color4> pallet;
|
||||
auto pallet_meta = json::array_val(tile_meta_json["pallet"]);
|
||||
|
||||
for (auto& c : pallet_meta.array.value())
|
||||
pallet.push_back(parse_color(c));
|
||||
|
||||
tile.pallet = pallet;
|
||||
}
|
||||
|
||||
Tiles().Register(tile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
CaveGameWindow::CaveGameWindow(const std::string& title, int width, int height): ReWindow::OpenGLWindow(title, width, height, 2, 1)
|
||||
{
|
||||
Logs::Info("Parsing Tile Data.");
|
||||
ReadTileDataAndRegister();
|
||||
CaveGame::Core::LoadTileMetadata();
|
||||
Logs::Info("Creating game window.");
|
||||
|
||||
CreateContexts();
|
||||
|
@@ -4,11 +4,15 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "Color4.hpp"
|
||||
#include "JJX/JSON.hpp"
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <array>
|
||||
#include <Core/Tile.hpp>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace CaveGame::Core {
|
||||
//class ITileMap;
|
||||
@@ -72,4 +76,12 @@ namespace CaveGame::Core {
|
||||
|
||||
/// Convenient access to the tile registry.
|
||||
static TileRegistry& Tiles() { return TileRegistry::Instance();}
|
||||
|
||||
std::string read_file(const std::string& file_path);
|
||||
|
||||
Color4 parse_color(const JJX::json::value& v);
|
||||
|
||||
bool LoadTileMetadata(const std::filesystem::path& path);
|
||||
|
||||
bool LoadTileMetadata();
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
#include <Core/TileRegistry.hpp>
|
||||
#include <Core/Loggers.hpp>
|
||||
#include <format>
|
||||
#include <Core/Item.hpp>
|
||||
#include <Core/ItemRegistry.hpp>
|
||||
|
||||
namespace CaveGame::Core
|
||||
{
|
||||
@@ -83,4 +85,86 @@ namespace CaveGame::Core
|
||||
bool TileRegistry::Exists(const std::string &mnemonic) { return name_to_id_mapping.contains(mnemonic); }
|
||||
|
||||
bool TileRegistry::Exists(u16 id) { return id_to_name_mapping.contains(id); }
|
||||
|
||||
bool LoadTileMetadata(const std::filesystem::path &path) {
|
||||
using namespace JJX;
|
||||
std::string content = read_file(path);
|
||||
|
||||
auto [data, parse_error] = json::parse(content);
|
||||
|
||||
if (data.type == json::value_type::array) {
|
||||
auto arr_data = json::array_val(data);
|
||||
for (auto& t : arr_data.array.value()) {
|
||||
Core::Tile tile;
|
||||
Core::Item tile_item;
|
||||
|
||||
auto tile_meta_json = json::object_val(t);
|
||||
|
||||
if (tile_meta_json.object->contains("hardcoded-id"))
|
||||
tile.assigned_numeric_id = tile_meta_json["hardcoded-id"].number;
|
||||
|
||||
tile.mnemonic_id = tile_meta_json["mnemonic-id"];
|
||||
tile.display_name = tile_meta_json["display-name"];
|
||||
|
||||
tile_item.mnemonic = std::format("{}-tile", tile.mnemonic_id);
|
||||
tile_item.display_name = tile.display_name;
|
||||
|
||||
tile.color = parse_color(tile_meta_json["color"]);
|
||||
|
||||
if (tile_meta_json.object->contains("pallet") && tile_meta_json["pallet"].array->size() > 0) {
|
||||
std::vector<Color4> pallet;
|
||||
auto pallet_meta = json::array_val(tile_meta_json["pallet"]);
|
||||
|
||||
for (auto& c : pallet_meta.array.value())
|
||||
pallet.push_back(parse_color(c));
|
||||
|
||||
tile.pallet = pallet;
|
||||
}
|
||||
|
||||
Tiles().Register(tile);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadTileMetadata() {
|
||||
LoadTileMetadata("assets/data/tiles.json");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string read_file(const std::string &file_path) {
|
||||
std::ifstream file(file_path, std::ios::binary);
|
||||
if (!file)
|
||||
throw std::runtime_error("We couldn't find the file: " + file_path);
|
||||
|
||||
std::streamsize file_size;
|
||||
file.seekg(0, std::ios::end);
|
||||
file_size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::string file_content(file_size, '\0');
|
||||
file.read(&file_content[0], file_size);
|
||||
file.close();
|
||||
|
||||
return file_content;
|
||||
}
|
||||
|
||||
Color4 parse_color(const JJX::json::value &v) {
|
||||
if (v.type == JJX::json::value_type::string)
|
||||
return Color4::FromHex(v.string.value());
|
||||
else if (v.type == JJX::json::value_type::array)
|
||||
{
|
||||
auto color_array = JJX::json::array_val(v);
|
||||
int r = color_array[0].number.value();
|
||||
int g = color_array[1].number.value();
|
||||
int b = color_array[2].number.value();
|
||||
int a = 255;
|
||||
if (color_array.array.value().size() == 4)
|
||||
a = color_array[3].number.value();
|
||||
return Color4(r, g, b, a);
|
||||
|
||||
}
|
||||
return Colors::Transparent;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user