R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
MapLoader.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** MapLoader.cpp - Utility for loading map configurations from JSON files
6*/
7
8#include "MapLoader.hpp"
10
11namespace map {
12
13 std::optional<ecs::MapData> MapLoader::loadFromFile(const std::string &filePath) {
14 try {
15 std::ifstream file(filePath);
16 if (!file.is_open()) {
17 LOG_ERROR("Failed to open map file: ", filePath);
18 return std::nullopt;
19 }
20
21 nlohmann::json jsonObj;
22 file >> jsonObj;
23 file.close();
24
25 LOG_INFO("Loaded map configuration from: ", filePath);
26 return parseJson(jsonObj);
27
28 } catch (const nlohmann::json::exception &e) {
29 LOG_ERROR("JSON parsing error in ", filePath, ": ", e.what());
30 return std::nullopt;
31 } catch (const std::exception &e) {
32 LOG_ERROR("Error loading map from ", filePath, ": ", e.what());
33 return std::nullopt;
34 }
35 }
36
37 std::optional<ecs::MapData> MapLoader::loadFromString(const std::string &jsonString) {
38 try {
39 nlohmann::json jsonObj = nlohmann::json::parse(jsonString);
40 return parseJson(jsonObj);
41
42 } catch (const nlohmann::json::exception &e) {
43 LOG_ERROR("JSON parsing error: ", e.what());
44 return std::nullopt;
45 } catch (const std::exception &e) {
46 LOG_ERROR("Error loading map from string: ", e.what());
47 return std::nullopt;
48 }
49 }
50
51 std::optional<ecs::MapData> MapLoader::parseJson(const nlohmann::json &jsonObj) {
52 try {
53 // Required fields
54 if (!jsonObj.contains("mapId")) {
55 LOG_ERROR("Map JSON missing required field: mapId");
56 return std::nullopt;
57 }
58
59 std::string mapId = jsonObj["mapId"].get<std::string>();
60
61 // Optional fields with defaults
62 std::string name = jsonObj.value("name", mapId);
63 float scrollSpeed = jsonObj.value("scrollSpeed", 0.0f);
64 std::string background = jsonObj.value("background", "");
65 std::string parallaxBackground = jsonObj.value("parallaxBackground", "");
66 float parallaxSpeedFactor = jsonObj.value("parallaxSpeedFactor", 0.5f);
67 std::string spawnScript = jsonObj.value("spawnScript", "");
68 float duration = jsonObj.value("duration", 0.0f);
69 std::string nextMap = jsonObj.value("nextMap", "");
70
71 // Create and return MapData
72 ecs::MapData mapData(mapId, name, scrollSpeed, background, spawnScript, duration, nextMap,
73 parallaxBackground, parallaxSpeedFactor);
74
75 LOG_INFO("✓ Parsed map: '", name, "' (", mapId, ")");
76 LOG_DEBUG(" - Scroll speed: ", scrollSpeed, " px/s");
77 LOG_DEBUG(" - Duration: ", duration > 0.0f ? std::to_string(duration) + "s" : "infinite");
78 if (!parallaxBackground.empty()) {
79 LOG_DEBUG(" - Parallax background: ", parallaxBackground,
80 " (speed factor: ", parallaxSpeedFactor, ")");
81 }
82 if (!nextMap.empty()) {
83 LOG_DEBUG(" - Next map: ", nextMap);
84 }
85
86 return mapData;
87
88 } catch (const nlohmann::json::exception &e) {
89 LOG_ERROR("Error parsing map JSON: ", e.what());
90 return std::nullopt;
91 } catch (const std::exception &e) {
92 LOG_ERROR("Unexpected error parsing map: ", e.what());
93 return std::nullopt;
94 }
95 }
96
97} // namespace map
#define LOG_INFO(...)
Definition Logger.hpp:181
#define LOG_DEBUG(...)
Definition Logger.hpp:180
#define LOG_ERROR(...)
Definition Logger.hpp:183
Component storing information about the current map/level.
Definition MapData.hpp:24
static std::optional< ecs::MapData > loadFromFile(const std::string &filePath)
Load a map from a JSON file.
Definition MapLoader.cpp:13
static std::optional< ecs::MapData > loadFromString(const std::string &jsonString)
Load a map from a JSON string.
Definition MapLoader.cpp:37
static std::optional< ecs::MapData > parseJson(const nlohmann::json &jsonObj)
Parse JSON object into MapData.
Definition MapLoader.cpp:51