Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
LevelManager.h
Go to the documentation of this file.
1/*
2 * Olympe Tilemap Editor - Level Manager
3 *
4 * Manages level data including entities, tiles, and collisions.
5 * Handles loading/saving LevelDefinition JSON format compatible with Olympe Engine.
6 */
7
8#pragma once
9
10#include "../../third_party/nlohmann/json.hpp"
11#include "../../vector.h" // Use engine's Vector class
12#include <string>
13#include <vector>
14#include <map>
15#include <memory>
16
18
19namespace Olympe {
20namespace Editor {
21
22
23 // Represents an entity instance in the level
25 {
26 std::string id; // Unique identifier
27 std::string prefabPath; // Path to the entity prefab blueprint
28 std::string name; // Display name
29 std::string type; // Entity type (e.g., "Player", "Enemy", "Item", "Collision"...)
30 std::string spritePath = ""; // Optional sprite path for visual representation
31 Vector position; // World position (using engine's Vector for direct usage)
32 float rotation; // Rotation in degrees (0-360)
33 json overrides; // Component property overrides
34
36 : id(""), prefabPath(""), name(""), position(), rotation(0.0f), overrides(json::object()) {}
37 };
38
39 // Represents level metadata
41 {
42 std::string author;
43 std::string created;
44 std::string lastModified;
45 std::vector<std::string> tags;
46 nlohmann::json customData; // For storing additional metadata like parallax layers
47
50 };
51
52 // Represents editor-specific state
54 {
55 double zoom;
57
59 : zoom(0.5), scrollOffset(0, 0, 0) {}
60 };
61
62 // Main level definition structure
64 {
66 std::string type;
67 std::string blueprintType;
68 std::string name;
69 std::string description;
70
73
74 // Level data
75 std::string levelName;
77 std::string backgroundMusic;
78 std::string ambientColor;
79
80 std::vector<std::unique_ptr<EntityInstance>> entities;
81 std::vector<std::vector<int>> tileMap; // 2D grid of tile IDs
82 std::vector<std::vector<uint8_t>> collisionMap; // 2D grid of collision masks
83
84 // Visual layers (Pass 1)
85 struct VisualLayer {
86 std::string name;
87 int zOrder;
89 std::string imagePath;
92 float offsetX;
93 float offsetY;
94 bool repeatX;
95 bool repeatY;
96 float opacity;
98 bool visible;
99
102 offsetX(0.0f), offsetY(0.0f), repeatX(false), repeatY(false),
103 opacity(1.0f), tintColor(0xFFFFFFFF), visible(true) {}
104 };
105 std::vector<VisualLayer> visualLayers;
106
107 // Tile layer definitions (Pass 1)
109 std::string name;
111 std::vector<std::vector<int>> tiles; // [y][x] = tileGID
112 std::vector<std::vector<uint8_t>> tileFlipFlags; // [y][x] = flip flags (H=0x1, V=0x2, D=0x4)
113 float opacity;
115
116 // Chunk support for infinite maps
117 struct Chunk {
118 int x, y; // Chunk coordinates (in tiles)
119 int width, height; // Chunk dimensions (in tiles)
120 std::vector<std::vector<int>> tiles; // [y][x] = tileGID
121 std::vector<std::vector<uint8_t>> tileFlipFlags; // [y][x] = flip flags
122
123 Chunk() : x(0), y(0), width(0), height(0) {}
124 };
125 std::vector<Chunk> chunks; // For infinite maps
126 bool isInfinite; // Whether this layer uses chunks
127
130 };
131 std::vector<TileLayerDef> tileLayers;
132
133 // Spatial structures (Pass 2)
134 struct SectorDef {
135 std::string name;
136 std::string type;
137 std::vector<Vector> polygon;
140
142 : position(0, 0, 0), properties(json::object()) {}
143 };
144 std::vector<SectorDef> sectors;
145
147 std::string name;
150 Vector size; // for rectangles
151 std::vector<Vector> points; // for polygons/polylines
152 float rotation; // rotation in degrees
153
155 : type(Rectangle), position(0, 0, 0), size(0, 0, 0), rotation(0.0f) {}
156 };
157 std::vector<CollisionShape> collisionShapes;
158
159 // Object categorization (Pass 3, 4)
161 std::vector<std::unique_ptr<EntityInstance>> staticObjects; // items, waypoints, triggers
162 std::vector<std::unique_ptr<EntityInstance>> dynamicObjects; // player, NPCs, enemies
163 std::vector<std::unique_ptr<EntityInstance>> patrolPaths; // AI patrol paths
164 std::vector<std::unique_ptr<EntityInstance>> soundObjects; // ambient sounds, music
165 };
167
168 // Relationships (Pass 5)
169 struct ObjectLink {
170 std::string sourceObjectName;
172 std::string targetObjectName;
174 std::string linkType; // "patrol_path", "trigger_target", "ai_target"
176
179 };
180 std::vector<ObjectLink> objectLinks;
181
182 // Resource catalog
184 std::vector<std::string> tilesetPaths;
185 std::vector<std::string> imagePaths;
186 std::vector<std::string> audioPaths;
187 };
189
190 // Map configuration
191 struct MapConfig {
192 std::string orientation; // "orthogonal", "isometric", "staggered", "hexagonal"
197 std::string renderOrder; // "right-down", "right-up", "left-down", "left-up"
199
201 : tileWidth(0), tileHeight(0), mapWidth(0), mapHeight(0),
202 orientation("orthogonal"), renderOrder("right-down"), infinite(false) {}
203 };
205
207 : schema_version(2), type("LevelDefinition"), blueprintType("LevelDefinition"),
208 name(""), description(""), levelName(""), worldSize(1024, 768, 0),
209 backgroundMusic(""), ambientColor("#000000") {}
210 };
211
212 // LevelManager: Core class for level editing operations
214 {
215 public:
216 LevelManager();
218
219 // Level loading/saving
220 bool LoadLevel(const std::string& path);
221 bool SaveLevel(const std::string& path);
222 void NewLevel(const std::string& name);
223
224 // Entity management
225 EntityInstance* CreateEntity(const std::string& prefabPath);
226 void DeleteEntity(const std::string& id);
227 EntityInstance* GetEntity(const std::string& id) const;
228 std::vector<EntityInstance*> GetAllEntities();
229 bool UpdateEntityPosition(const std::string& id, const Vector& position);
230
231 // Tile management
232 void SetTile(int x, int y, int tileId);
233 int GetTile(int x, int y) const;
234 void ResizeTileMap(int width, int height);
235
236 // Collision management
237 void SetCollision(int x, int y, uint8_t mask);
238 uint8_t GetCollision(int x, int y) const;
239 void ResizeCollisionMap(int width, int height);
240
241 // Level properties
244
245 const std::string& GetCurrentLevelPath() const { return m_currentPath; }
246 bool HasUnsavedChanges() const { return m_hasUnsavedChanges; }
248 void ClearDirty() { m_hasUnsavedChanges = false; }
249
250 private:
251 // JSON serialization helpers
252 void SerializeToJson(json& j) const;
253 bool DeserializeFromJson(const json& j);
254
255 std::string GenerateUniqueEntityId();
256 std::string GetCurrentTimestamp();
257
259 std::string m_currentPath;
262 };
263
264 // JSON conversion helpers
265 void to_json(json& j, const Vector& v);
266 void from_json(const json& j, Vector& v);
267 void to_json(json& j, const EntityInstance& e);
268 void from_json(const json& j, EntityInstance& e);
269
270} // namespace Editor
271} // namespace Olympe
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
bool SaveLevel(const std::string &path)
void NewLevel(const std::string &name)
EntityInstance * CreateEntity(const std::string &prefabPath)
void ResizeTileMap(int width, int height)
std::vector< EntityInstance * > GetAllEntities()
void SetTile(int x, int y, int tileId)
int GetTile(int x, int y) const
void SetCollision(int x, int y, uint8_t mask)
LevelDefinition & GetLevelDefinition()
uint8_t GetCollision(int x, int y) const
void DeleteEntity(const std::string &id)
bool LoadLevel(const std::string &path)
bool DeserializeFromJson(const json &j)
const std::string & GetCurrentLevelPath() const
bool UpdateEntityPosition(const std::string &id, const Vector &position)
void SerializeToJson(json &j) const
const LevelDefinition & GetLevelDefinition() const
void ResizeCollisionMap(int width, int height)
EntityInstance * GetEntity(const std::string &id) const
void to_json(json &j, const Vector &v)
void from_json(const json &j, Vector &v)
nlohmann::json json
nlohmann::json json
enum Olympe::Editor::LevelDefinition::CollisionShape::Type type
std::vector< std::unique_ptr< EntityInstance > > patrolPaths
std::vector< std::unique_ptr< EntityInstance > > dynamicObjects
std::vector< std::unique_ptr< EntityInstance > > soundObjects
std::vector< std::unique_ptr< EntityInstance > > staticObjects
std::vector< std::vector< uint8_t > > tileFlipFlags
std::vector< std::vector< int > > tiles
std::vector< std::vector< uint8_t > > tileFlipFlags
std::vector< std::vector< int > > tileMap
std::vector< CollisionShape > collisionShapes
std::vector< std::vector< uint8_t > > collisionMap
std::vector< ObjectLink > objectLinks
std::vector< std::unique_ptr< EntityInstance > > entities
std::vector< VisualLayer > visualLayers
std::vector< SectorDef > sectors
std::vector< TileLayerDef > tileLayers
std::vector< std::string > tags