From 23f54bd4fe6b9d95e7da10f53afc70b6ab504116 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 23 Oct 2025 13:48:58 -0400 Subject: [PATCH] Now fixtures and actors are loaded the same. --- assets/bosses.json | 28 ++++++++++++-- scene.cpp | 91 ++++++++++++++++++++++------------------------ scene.hpp | 10 +++-- 3 files changed, 73 insertions(+), 56 deletions(-) diff --git a/assets/bosses.json b/assets/bosses.json index 488a67a..f1fe420 100644 --- a/assets/bosses.json +++ b/assets/bosses.json @@ -11,19 +11,39 @@ "sprite": "rat_king_boss", "cell": "boss5", "scale_x": 0.6, - "scale_y": 0.6 + "scale_y": 0.6, + "x": 0, + "y": 0 }, { "name": "player", "sprite": "peasant_girl_rear_view", "cell": "player2", "scale_x": 0.5, - "scale_y": 0.5 + "scale_y": 0.5, + "x": 0, + "y": 0 } ], "fixtures": [ - {"name": "torch_fixture", "scale_x": 0.5, "scale_y": 0.5, "cell": "torch1", "x": 66, "y": -50}, - {"name": "torch_fixture", "scale_x": -0.5, "scale_y": 0.5, "cell": "torch2", "x": 132, "y": -30} + { + "name": "torch_fixture", + "sprite": "torch_fixture", + "scale_x": 0.5, + "scale_y": 0.5, + "cell": "torch1", + "x": 66, + "y": -50 + }, + { + "name": "torch_fixture", + "sprite": "torch_fixture", + "scale_x": -0.5, + "scale_y": 0.5, + "cell": "torch2", + "x": 132, + "y": -30 + } ] }, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, diff --git a/scene.cpp b/scene.cpp index 98570fa..5f345b2 100644 --- a/scene.cpp +++ b/scene.cpp @@ -7,27 +7,39 @@ const bool YOU_REMOVED_MID_CELL_IDIOT=false; namespace scene { using namespace guecs; + Element config_scene_element(nlohmann::json& config, bool and_play, bool duped) { + std::string sprite_name = config["sprite"]; + auto st = textures::get_sprite(sprite_name); + + if(duped) { + st.sprite = std::make_shared(*st.texture); + } + + float scale_x = config["scale_x"]; + float scale_y = config["scale_y"]; + float x = config["x"]; + float y = config["y"]; + + auto anim = animation::load(sprite_name); + if(and_play) anim.play(); + anim.scale_x = scale_x; + anim.scale_y = scale_y; + + std::string cell = config["cell"]; + std::string name = config["name"]; + + return {name, st, anim, cell, scale_x, scale_y, x, y}; + } + Engine::Engine(components::AnimatedScene& scene) : $scene(scene) { for(auto& config : $scene.actors) { - std::string sprite_name = config["sprite"]; - auto st = textures::get_sprite(sprite_name); - float scale_x = config["scale_x"]; - float scale_y = config["scale_y"]; - - auto anim = animation::load(sprite_name); - anim.scale_x = scale_x; - anim.scale_y = scale_y; - - auto& cell = config["cell"]; - std::string key = config["name"]; - - dbc::check(!$actor_name_ids.contains(key), - fmt::format("actors key {} already exists", key)); - - $actors.emplace_back(st, anim, cell, scale_x, scale_y, 0, 0); - $actor_name_ids.try_emplace(key, $actors.size() - 1); + auto element = config_scene_element(config, false, false); + dbc::check(!$actor_name_ids.contains(element.name), + fmt::format("actors key {} already exists", element.name)); + $actors.push_back(element); + $actor_name_ids.try_emplace(element.name, $actors.size() - 1); } // floor is std::optional @@ -36,23 +48,8 @@ namespace scene { } for(auto& fixture : $scene.fixtures) { - std::string name = fixture["name"]; - auto st = textures::get_sprite(name); - // clone the sprite so it can be positioned - st.sprite = std::make_shared(*st.texture); - float scale_x = fixture["scale_x"]; - float scale_y = fixture["scale_y"]; - - auto anim = animation::load(name); - anim.scale_x = scale_x; - anim.scale_y = scale_y; - anim.play(); - - std::string cell = fixture["cell"]; - float x = fixture["x"]; - float y = fixture["y"]; - - $fixtures.emplace_back(st, anim, cell, scale_x, scale_y, x, y); + auto element = config_scene_element(fixture, true, true); + $fixtures.push_back(element); } } @@ -90,19 +87,6 @@ namespace scene { return $ui.mouse(x, y, mods); } - sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff, float y_diff) { - - auto& cell = $ui.cell_for(cell_name); - float x = float(at_mid ? cell.mid_x : cell.x); - float y = float(at_mid ? cell.mid_y : cell.y); - - sf::Vector2f pos{x + x_diff, y + y_diff}; - st.sprite->setPosition(pos); - st.sprite->setScale({scale_x, scale_y}); - - return pos; - } - void Engine::render(sf::RenderWindow& window) { if($floor_sprite.sprite) { window.draw(*$floor_sprite.sprite); @@ -121,7 +105,6 @@ namespace scene { if(DEBUG) $ui.debug_layout(window); } - void Engine::move_actor(const std::string& actor, const std::string& cell_name) { auto& config = $actors.at($actor_name_ids.at(actor)); config.cell = cell_name; @@ -147,4 +130,16 @@ namespace scene { } } + sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff, float y_diff) { + + auto& cell = $ui.cell_for(cell_name); + float x = float(at_mid ? cell.mid_x : cell.x); + float y = float(at_mid ? cell.mid_y : cell.y); + + sf::Vector2f pos{x + x_diff, y + y_diff}; + st.sprite->setPosition(pos); + st.sprite->setScale({scale_x, scale_y}); + + return pos; + } } diff --git a/scene.hpp b/scene.hpp index 000f6a8..929de1b 100644 --- a/scene.hpp +++ b/scene.hpp @@ -11,13 +11,14 @@ namespace scene { using namespace textures; struct Element { + std::string name; textures::SpriteTexture st; components::Animation anim; std::string cell; - float scale_x; - float scale_y; - float x; - float y; + float scale_x = 1.0f; + float scale_y = 1.0f; + float x = 0; + float y = 0; bool at_mid=false; sf::Vector2f pos{0,0}; }; @@ -35,6 +36,7 @@ namespace scene { void init(); void render(sf::RenderWindow& window); bool mouse(float x, float y, guecs::Modifiers mods); + sf::Vector2f position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff=0.0f, float y_diff=0.0f); void move_actor(const std::string& actor, const std::string& cell_name);