Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AnimationBank.cpp
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Animation System - Animation Bank Implementation
4*/
5
6#include "AnimationBank.h"
7#include "../json_helper.h"
8#include "../DataManager.h"
9#include "../system/system_utils.h"
10#include <fstream>
11
13
15{
16 // ========================================================================
17 // AnimationBank Implementation
18 // ========================================================================
19
20 bool AnimationBank::LoadFromFile(const std::string& filePath)
21 {
22 json j;
23 if (!JsonHelper::LoadJsonFromFile(filePath, j))
24 {
25 SYSTEM_LOG << "AnimationBank: Failed to load file: " << filePath << "\n";
26 return false;
27 }
28
29 return ParseJSON(j.dump());
30 }
31
32 bool AnimationBank::ParseJSON(const std::string& jsonContent)
33 {
34 try
35 {
36 json j = json::parse(jsonContent);
37
38 // Parse basic info
39 m_bankName = JsonHelper::GetString(j, "bankName", "unknown");
40 m_description = JsonHelper::GetString(j, "description", "");
41
42 // Parse spritesheets
43 if (j.contains("spritesheets") && j["spritesheets"].is_array())
44 {
45 for (const auto& sheetJson : j["spritesheets"])
46 {
49 sheet.path = JsonHelper::GetString(sheetJson, "path", "");
50 sheet.frameWidth = JsonHelper::GetInt(sheetJson, "frameWidth", 32);
51 sheet.frameHeight = JsonHelper::GetInt(sheetJson, "frameHeight", 32);
52 sheet.columns = JsonHelper::GetInt(sheetJson, "columns", 1);
53 sheet.rows = JsonHelper::GetInt(sheetJson, "rows", 1);
54 sheet.totalFrames = JsonHelper::GetInt(sheetJson, "totalFrames", 1);
55
56 // Parse hotspot
57 if (sheetJson.contains("hotspot") && sheetJson["hotspot"].is_object())
58 {
59 sheet.hotspot.x = JsonHelper::GetFloat(sheetJson["hotspot"], "x", 16.0f);
60 sheet.hotspot.y = JsonHelper::GetFloat(sheetJson["hotspot"], "y", 16.0f);
61 }
62 else
63 {
64 // Default to center of frame
65 sheet.hotspot.x = sheet.frameWidth / 2.0f;
66 sheet.hotspot.y = sheet.frameHeight / 2.0f;
67 }
68
69 // Load texture via DataManager
70 sheet.texture = DataManager::Get().GetTexture(sheet.id);
71 if (sheet.texture == nullptr && !sheet.path.empty())
72 {
74 sheet.texture = DataManager::Get().GetTexture(sheet.id);
75 }
76
78 }
79 }
80
81 // Parse animations
82 if (j.contains("animations") && j["animations"].is_array())
83 {
84 for (const auto& animJson : j["animations"])
85 {
88 anim.spritesheetId = JsonHelper::GetString(animJson, "spritesheetId", "");
89 anim.startFrame = JsonHelper::GetInt(animJson, "startFrame", 0);
90 anim.endFrame = JsonHelper::GetInt(animJson, "endFrame", 0);
91 anim.framerate = JsonHelper::GetFloat(animJson, "framerate", 12.0f);
92 anim.looping = JsonHelper::GetBool(animJson, "looping", true);
93
94 // Parse events
95 if (animJson.contains("events") && animJson["events"].is_array())
96 {
97 for (const auto& eventJson : animJson["events"])
98 {
100 event.frame = JsonHelper::GetInt(eventJson, "frame", 0);
101 event.type = JsonHelper::GetString(eventJson, "type", "");
102
103 // Store the entire "data" field as JSON string
104 if (eventJson.contains("data"))
105 {
106 event.dataJson = eventJson["data"].dump();
107 }
108
109 anim.events.push_back(event);
110 }
111 }
112
113 m_animations[anim.name] = anim;
114 }
115 }
116
117 m_isValid = true;
118 SYSTEM_LOG << "AnimationBank: Successfully loaded '" << m_bankName
119 << "' with " << m_spritesheets.size() << " spritesheets and "
120 << m_animations.size() << " animations\n";
121 return true;
122 }
123 catch (const std::exception& e)
124 {
125 SYSTEM_LOG << "AnimationBank: Error parsing JSON: " << e.what() << "\n";
126 return false;
127 }
128 }
129
130 Animation* AnimationBank::GetAnimation(const std::string& name)
131 {
132 auto it = m_animations.find(name);
133 if (it != m_animations.end())
134 return &it->second;
135 return nullptr;
136 }
137
138 const Animation* AnimationBank::GetAnimation(const std::string& name) const
139 {
140 auto it = m_animations.find(name);
141 if (it != m_animations.end())
142 return &it->second;
143 return nullptr;
144 }
145
147 {
148 auto it = m_spritesheets.find(id);
149 if (it != m_spritesheets.end())
150 return &it->second;
151 return nullptr;
152 }
153
154 const SpriteSheet* AnimationBank::GetSpriteSheet(const std::string& id) const
155 {
156 auto it = m_spritesheets.find(id);
157 if (it != m_spritesheets.end())
158 return &it->second;
159 return nullptr;
160 }
161
162} // namespace OlympeAnimation
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static DataManager & Get()
Definition DataManager.h:87
Sprite * GetTexture(const std::string &id) const
bool PreloadTexture(const std::string &id, const std::string &path, ResourceCategory category=ResourceCategory::System)
Animation * GetAnimation(const std::string &name)
std::unordered_map< std::string, Animation > m_animations
bool LoadFromFile(const std::string &filePath)
SpriteSheet * GetSpriteSheet(const std::string &id)
bool ParseJSON(const std::string &jsonContent)
std::unordered_map< std::string, SpriteSheet > m_spritesheets
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
bool LoadJsonFromFile(const std::string &filepath, json &j)
Load and parse a JSON file.
Definition json_helper.h:42
int GetInt(const json &j, const std::string &key, int defaultValue=0)
Safely get an integer value from JSON.
float GetFloat(const json &j, const std::string &key, float defaultValue=0.0f)
Safely get a float value from JSON.
bool GetBool(const json &j, const std::string &key, bool defaultValue=false)
Safely get a boolean value from JSON.
nlohmann::json json
#define SYSTEM_LOG