Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BTEventTypeRegistry.cpp
Go to the documentation of this file.
2#include <fstream>
3#include <iostream>
4#include <algorithm>
5
6// Static member definitions
7std::vector<BTEventTypeRegistry::EventTypeEntry> BTEventTypeRegistry::s_eventTypes;
8std::map<std::string, size_t> BTEventTypeRegistry::s_eventTypeMap;
10
11bool BTEventTypeRegistry::Initialize(const std::string& filepath)
12{
13 if (s_initialized)
14 {
15 return true;
16 }
17
18 // Try to load from JSON
19 if (LoadFromJSON(filepath))
20 {
21 s_initialized = true;
22 std::cout << "[BTEventTypeRegistry] Successfully loaded " << s_eventTypes.size()
23 << " event types from JSON" << std::endl;
24 return true;
25 }
26
27 // Fallback to hardcoded defaults
28 std::cout << "[BTEventTypeRegistry] Failed to load from JSON, using hardcoded defaults" << std::endl;
30 s_initialized = true;
31 return false;
32}
33
34bool BTEventTypeRegistry::LoadFromJSON(const std::string& filepath)
35{
36 std::ifstream file(filepath);
37 if (!file.is_open())
38 {
39 std::cerr << "[BTEventTypeRegistry] Could not open file: " << filepath << std::endl;
40 return false;
41 }
42
43 try
44 {
46 file >> jsonData;
47
48 if (!jsonData.contains("eventTypes") || !jsonData["eventTypes"].is_array())
49 {
50 std::cerr << "[BTEventTypeRegistry] Invalid JSON: missing 'eventTypes' array" << std::endl;
51 return false;
52 }
53
54 s_eventTypes.clear();
55 s_eventTypeMap.clear();
56
57 const auto& eventTypesArray = jsonData["eventTypes"];
58 for (const auto& entry : eventTypesArray)
59 {
60 if (!entry.contains("id") || !entry.contains("displayName"))
61 {
62 std::cerr << "[BTEventTypeRegistry] Skipping invalid entry (missing required fields)" << std::endl;
63 continue;
64 }
65
66 EventTypeEntry eventType;
67 eventType.id = entry["id"].get<std::string>();
68 eventType.displayName = entry["displayName"].get<std::string>();
69 eventType.domain = entry.value("domain", "System");
70 eventType.description = entry.value("description", "");
71 eventType.category = entry.value("category", "Other");
72
73 s_eventTypeMap[eventType.id] = s_eventTypes.size();
74 s_eventTypes.push_back(eventType);
75
76 std::cout << "[BTEventTypeRegistry] Loaded: " << eventType.displayName
77 << " (" << eventType.category << ")" << std::endl;
78 }
79
80 return true;
81 }
82 catch (const std::exception& e)
83 {
84 std::cerr << "[BTEventTypeRegistry] JSON parsing error: " << e.what() << std::endl;
85 return false;
86 }
87}
88
90{
91 s_eventTypes.clear();
92 s_eventTypeMap.clear();
93
94 // Core defaults from system_consts.h
95 std::vector<EventTypeEntry> defaults = {
96 {"Olympe_EventType_Object_Activate", "Object Activate", "Gameplay", "Trigger object activation", "Objects"},
97 {"Olympe_EventType_Object_Deactivate", "Object Deactivate", "Gameplay", "Trigger object deactivation", "Objects"},
98 {"Olympe_EventType_Object_Create", "Object Create", "Gameplay", "Object created", "Objects"},
99 {"Olympe_EventType_Object_Destroy", "Object Destroy", "Gameplay", "Object destroyed", "Objects"},
100 {"Olympe_EventType_Object_CollideEvent", "Object Collide", "Collision", "Object collision event", "Collision"},
101 {"Olympe_EventType_Level_Load", "Level Load", "System", "Level loaded", "Level"},
102 {"Olympe_EventType_Level_Unload", "Level Unload", "System", "Level unloaded", "Level"},
103 {"Olympe_EventType_Game_Pause", "Game Pause", "System", "Game paused", "Game"},
104 {"Olympe_EventType_Game_Resume", "Game Resume", "System", "Game resumed", "Game"},
105 {"Olympe_EventType_AI_Explosion", "AI Explosion", "Gameplay", "Explosion detected", "AI Stimulus"},
106 {"Olympe_EventType_AI_Noise", "AI Noise", "Gameplay", "Noise detected", "AI Stimulus"},
107 {"Olympe_EventType_AI_DamageDealt", "AI Damage", "Gameplay", "Damage dealt", "AI Stimulus"},
108 {"Olympe_EventType_Keyboard_KeyDown", "Key Down", "Input", "Keyboard key pressed", "Input"},
109 {"Olympe_EventType_Mouse_ButtonDown", "Mouse Button Down", "Input", "Mouse button pressed", "Input"},
110 {"Olympe_EventType_Camera_Shake", "Camera Shake", "Camera", "Start camera shake", "Camera"},
111 {"Olympe_EventType_Menu_Enter", "Menu Enter", "UI", "Menu entered", "Menu"},
112 };
113
114 for (const auto& entry : defaults)
115 {
116 s_eventTypeMap[entry.id] = s_eventTypes.size();
117 s_eventTypes.push_back(entry);
118 }
119
120 std::cout << "[BTEventTypeRegistry] Initialized " << s_eventTypes.size() << " default event types" << std::endl;
121}
122
123const std::vector<BTEventTypeRegistry::EventTypeEntry>& BTEventTypeRegistry::GetAllEventTypes()
124{
125 if (!s_initialized)
126 {
127 Initialize("./Gamedata/BehaviorTree/EventTypes.json");
128 }
129 return s_eventTypes;
130}
131
133{
134 if (!s_initialized)
135 {
136 Initialize("./Gamedata/BehaviorTree/EventTypes.json");
137 }
138
139 auto it = s_eventTypeMap.find(id);
140 if (it != s_eventTypeMap.end() && it->second < s_eventTypes.size())
141 {
142 return &s_eventTypes[it->second];
143 }
144 return nullptr;
145}
146
147std::string BTEventTypeRegistry::GetDisplayName(const std::string& id)
148{
149 const auto* entry = GetEventTypeEntry(id);
150 return entry ? entry->displayName : id;
151}
152
153std::string BTEventTypeRegistry::GetDomain(const std::string& id)
154{
155 const auto* entry = GetEventTypeEntry(id);
156 return entry ? entry->domain : "System";
157}
158
159std::string BTEventTypeRegistry::GetCategory(const std::string& id)
160{
161 const auto* entry = GetEventTypeEntry(id);
162 return entry ? entry->category : "Other";
163}
164
165std::vector<BTEventTypeRegistry::EventTypeEntry> BTEventTypeRegistry::GetEventTypesByCategory(const std::string& category)
166{
167 std::vector<EventTypeEntry> result;
168 for (const auto& entry : GetAllEventTypes())
169 {
170 if (entry.category == category)
171 {
172 result.push_back(entry);
173 }
174 }
175 return result;
176}
177
179{
180 std::vector<std::string> categories;
181 for (const auto& entry : GetAllEventTypes())
182 {
183 if (std::find(categories.begin(), categories.end(), entry.category) == categories.end())
184 {
185 categories.push_back(entry.category);
186 }
187 }
188 std::sort(categories.begin(), categories.end());
189 return categories;
190}
191
192bool BTEventTypeRegistry::Reload(const std::string& filepath)
193{
194 s_initialized = false;
195 return Initialize(filepath);
196}
197
198bool BTEventTypeRegistry::IsValid(const std::string& id)
199{
200 if (!s_initialized)
201 {
202 Initialize("./Gamedata/BehaviorTree/EventTypes.json");
203 }
204 return s_eventTypeMap.find(id) != s_eventTypeMap.end();
205}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static bool Initialize(const std::string &filepath)
Initialize registry from JSON file with fallback.
static std::map< std::string, size_t > s_eventTypeMap
static std::string GetDisplayName(const std::string &id)
Get display name for event type id.
static std::vector< EventTypeEntry > s_eventTypes
static std::string GetCategory(const std::string &id)
Get category for event type id.
static std::vector< EventTypeEntry > GetEventTypesByCategory(const std::string &category)
Get all event types for a specific category.
static void InitializeDefaults()
Initialize default hardcoded event types (fallback)
static std::vector< std::string > GetAllCategories()
Get all unique categories.
static bool LoadFromJSON(const std::string &filepath)
Load event types from JSON file.
static bool Reload(const std::string &filepath)
Reload registry from file (for live editing)
static const EventTypeEntry * GetEventTypeEntry(const std::string &id)
Get event type entry by id.
static bool IsValid(const std::string &id)
Check if event type is registered.
static const std::vector< EventTypeEntry > & GetAllEventTypes()
Get all registered event types.
static std::string GetDomain(const std::string &id)
Get domain for event type id.
nlohmann::json json
Event type entry from JSON configuration.