40 std::ifstream file(filePath);
42 throw std::runtime_error(
"Could not open JSON file: " + filePath);
45 nlohmann::json gameData;
49 std::unordered_map<std::string, nlohmann::json> objectDefinitions;
50 if (gameData.contains(
"object_definitions")) {
51 for (
const auto& [type, objects] : gameData[
"object_definitions"].items()) {
52 for (
const auto& [name, definition] : objects.items()) {
53 objectDefinitions[name] = definition;
57 std::cerr <<
"No object definitions found in JSON file.\n";
61 if (gameData.contains(
"levels")) {
62 for (
const auto& level : gameData[
"levels"]) {
65 for (
const auto&
object : level[
"objects"]) {
66 std::string name =
object[
"name"];
67 if (objectDefinitions.find(name) != objectDefinitions.end()) {
68 const auto& definition = objectDefinitions[name];
73 if (definition[
"properties"].contains(
"type") && definition[
"properties"][
"type"] ==
"Tile") {
74 std::cout <<
"Creating tile\n";
78 Vec2(definition[
"length"], definition[
"width"]),
79 Vec2(definition[
"size_width"], definition[
"size_height"]),
80 Vec2(definition[
"top_left_x"], definition[
"top_left_y"]),
81 Vec2(definition[
"rows"], definition[
"columns"])
83 std::cout <<
"Added tile: " << name <<
'\n' <<
" at (" << x <<
", " << y <<
")\n";
87 else if (definition[
"properties"].contains(
"type") && definition[
"properties"][
"type"] ==
"Wall") {
88 std::cout <<
"Creating wall\n";
92 Vec2(definition[
"length"], definition[
"width"]),
93 Vec2(definition[
"size_width"], definition[
"size_height"]),
94 Vec2(definition[
"top_left_x"], definition[
"top_left_y"])
96 std::cout <<
"Added wall: " << name <<
" at (" << x <<
", " << y <<
")\n";
99 else if (definition[
"properties"].contains(
"type") && definition[
"properties"][
"type"] ==
"Table") {
100 std::cout <<
"Creating table\n";
104 Vec2(definition[
"length"], definition[
"width"]),
105 Vec2(definition[
"size_width"], definition[
"size_height"]),
106 Vec2(definition[
"top_left_x"], definition[
"top_left_y"])
108 std::cout <<
"Added table: " << name <<
" at (" << x <<
", " << y <<
")\n";
113 else if (definition[
"properties"].contains(
"type") && definition[
"properties"][
"type"] ==
"Enemy") {
114 std::cout <<
"Creating enemy\n";
118 definition[
"columns"],
119 definition[
"animation_time"],
125 std::cout <<
"Created enemy\n";
128 std::cout <<
"Added enemy: " << name <<
" at (" << x <<
", " << y <<
")\n";
131 std::cerr <<
"Object definition not found for: " << name <<
"\n";
142 std::cout<<
"Loaded scene\n";
148 std::cerr <<
"No levels found in JSON file.\n";
165 std::cout <<
"Finished all scenes.\n";
179 for (
int i = -lenTiles; i < lenTiles; i++) {
180 for (
int j = -lenTiles; j < lenTiles; j++) {
187 for (
int i = 0; i < 10; i++)
189 int x = rand() % 1200 - 600;
190 int y = rand() % 1200 - 600;
201 for (
int i = 0; i < 10; i++) {
217 for (
int i = -lenTiles; i < lenTiles; i++) {
218 for (
int j = -lenTiles; j < lenTiles; j++) {
225 for (
int i = 0; i < 20; i++)
227 int x = rand() % 1200 - 600;
228 int y = rand() % 1200 - 600;
239 for (
int i = 0; i < 20; i++) {
255 for (
int i = -lenTiles; i < lenTiles; i++) {
256 for (
int j = -lenTiles; j < lenTiles; j++) {
263 for (
int i = 0; i < 30; i++)
265 int x = rand() % 1200 - 600;
266 int y = rand() % 1200 - 600;
277 for (
int i = 0; i < 30; i++) {
292 if (sceneTree ==
nullptr)
return;
306 if (sceneTree ==
nullptr)
return;
318 if (sceneTree ==
nullptr)
return;
327 if (sceneTree->gameStatus() == -1)
330 SDL_SetRenderDrawColor(
m_renderer, 255, 0, 0, 0);
347 else if (sceneTree->gameStatus() == 1)
350 SDL_SetRenderDrawColor(
m_renderer, 0, 255, 0, 255);
372 if (sceneTree ==
nullptr)
return;
379 sceneTree->resetCachedGameObjectsInFrame();
Manages the camera's view in a game, handling its position in the world space.
static GameObject * createEnemyWarriorCustom(std::string path, int row, int col, float animationTime, int pos_x, int pos_y, int size_x, int size_y)
Creates a customized enemy warrior object with specific texture and animations.
static GameObject * createTableCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
Creates a customized table object with specific texture.
static GameObject * createKey()
Creates a key object.
static GameObject * createEnemyWarrior()
Creates an enemy warrior object.
static GameObject * createTileCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos, Vec2 rc)
Creates a customized tile object with specific texture.
static GameObject * createTile1()
Creates a basic tile object.
static GameObject * createCrosshair()
Creates a crosshair object.
static GameObject * createWallCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
Creates a customized wall object with specific texture.
static GameObject * createPlayerTest()
Creates a test player object.
static GameObject * createBow()
Creates a bow object.
Core class representing an entity in the game world.
void render()
Renders this GameObject and its components.
SceneNode * getSceneNode()
Gets the SceneNode associated with this GameObject.
T * getComponent()
Gets a component of the specified type.
void input()
Processes input for this GameObject and its components.
void update()
Updates this GameObject and its components.
Singleton class that manages scenes and their transitions.
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * createSceneTest2()
std::vector< SceneTree * > m_sceneTrees
Array of scene trees.
SceneTree * createSceneTest1()
Creates a test scene. This method is an example of how to define specific scenes.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
void getNextScene()
Advances to the next scene based on the current scene index.
void render()
Renders the current scene.
void cleanTree()
Cleans up the scene tree, removing all inactive game objects.
void input()
Processes input for the current scene.
SceneManager()
Private constructor for singleton pattern.
SDL_Renderer * m_renderer
Renderer used to render the game objects.
int m_currentSceneIndex
Index of the current scene in the game.
void loadScenesFromJSON(const std::string &filePath)
void update()
Updates the state of the current scene.
SceneTree * createSceneTest3()
SDL_Renderer * getRenderer()
Retrieves the current SDL_Renderer used by the SceneManager.
int getSceneIndex()
Retrieves the index of the current scene.
static std::atomic< uint64_t > m_totalObjects
Total number of game objects created.
static std::atomic< uint64_t > m_aliveObjects
Current number of active game objects.
Vec2 getCameraWorldPosition()
Retrieves the world position of the camera.
void setRenderer(SDL_Renderer *renderer)
Sets the renderer used for drawing game objects.
Represents a node in the scene graph, encapsulating a game object and its hierarchy.
void addChild(GameObject *child, bool isBackground=false)
Adds a child game object to this node.
bool isBackground()
Checks if this node is part of the background.
bool readyToDestroy()
Checks if this node is marked for destruction.
GameObject * getGameObject()
Gets the GameObject associated with this node.
Manages a tree structure of SceneNodes, facilitating scene graph operations.
void addChild(GameObject *child, bool isBackground=false)
Adds a child GameObject to the scene tree.
void traverseTree(std::function< void(SceneNode *)> callback)
Traverses the entire tree, executing a callback on each node.
Manages resources such as textures, fonts, and potentially sounds for a game engine.
Defines the SceneManager class for managing scenes in the game.
A structure to represent 2D vectors.
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...