From 49c970204192f22a66888ba6cccbd862f2110d74 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 23 Oct 2025 00:29:15 -0400 Subject: [PATCH] Cleaned up the arena code more and closer to pulling it out for a scene system. --- assets/bosses.json | 26 +++++++++--------- boss/fight.cpp | 6 ++--- boss/ui.cpp | 66 ++++++++++++++++++++++++++++++---------------- boss/ui.hpp | 10 +++---- 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/assets/bosses.json b/assets/bosses.json index a2495c0..488a67a 100644 --- a/assets/bosses.json +++ b/assets/bosses.json @@ -5,20 +5,22 @@ "background": "test_background", "floor": false, "floor_pos": "floor1", - "actors": { - "player": { - "sprite": "peasant_girl_rear_view", - "start_pos": "player2", - "scale": 0.5, - "mid_cell": false - }, - "boss": { + "actors": [ + { + "name": "boss", "sprite": "rat_king_boss", - "start_pos": "boss5", - "scale": 0.6, - "mid_cell": true + "cell": "boss5", + "scale_x": 0.6, + "scale_y": 0.6 + }, + { + "name": "player", + "sprite": "peasant_girl_rear_view", + "cell": "player2", + "scale_x": 0.5, + "scale_y": 0.5 } - }, + ], "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} diff --git a/boss/fight.cpp b/boss/fight.cpp index e05bb0e..0302ead 100644 --- a/boss/fight.cpp +++ b/boss/fight.cpp @@ -84,7 +84,7 @@ namespace boss { break; case ATTACK: { $ui.status(L"PLAYER TURN"); - $ui.move_actor($ui.$player_sprite, "player", run % 10 < 5 ? "player1" : "player2"); + $ui.move_actor("player", run % 10 < 5 ? "player1" : "player2"); int attack_id = std::any_cast(data); boss::System::combat(attack_id); state(State::PLAYER_TURN); @@ -105,8 +105,8 @@ namespace boss { break; case ATTACK: { $ui.status(L"BOSS TURN"); - $ui.$boss_pos = $ui.move_actor($ui.$boss_sprite, "boss", run % 10 < 5 ? "boss5" : "boss6"); - $ui.$boss_anim.play(); + $ui.move_actor("boss", run % 10 < 5 ? "boss5" : "boss6"); + $ui.animate_actor("boss"); int attack_id = std::any_cast(data); boss::System::combat(attack_id); state(State::BOSS_TURN); diff --git a/boss/ui.cpp b/boss/ui.cpp index 051fff8..2a351ae 100644 --- a/boss/ui.cpp +++ b/boss/ui.cpp @@ -7,6 +7,7 @@ #include "sound.hpp" const bool DEBUG=false; +const bool YOU_REMOVED_MID_CELL_IDIOT=false; namespace boss { using namespace guecs; @@ -17,19 +18,32 @@ namespace boss { $scene(world->get($boss_id)), $combat_ui(true) { - std::string sprite_name = $scene.actors["boss"]["sprite"]; - $boss_sprite = textures::get_sprite(sprite_name); + + 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); + } // floor is std::optional if($scene.floor) { $floor_sprite = textures::get_sprite(*$scene.floor); } - $player_sprite = textures::get_sprite($scene.actors["player"]["sprite"]); - - dbc::check(animation::has(sprite_name), "add boss animation to animations.json"); - $boss_anim = animation::load(sprite_name); - for(auto& fixture : $scene.fixtures) { std::string name = fixture["name"]; auto st = textures::get_sprite(name); @@ -66,17 +80,18 @@ namespace boss { "[floor4|player5|player6|player7|player8|_]" ); - $boss_pos = move_actor($boss_sprite, "boss", $scene.actors["boss"]["start_pos"]); - move_actor($player_sprite, "player", $scene.actors["player"]["start_pos"]); - if($scene.floor) { position_sprite($floor_sprite, $scene.floor_pos, 1.0f, 1.0f, false); } + for(auto& actor : $actors) { + actor.pos = position_sprite(actor.st, actor.cell, + actor.scale_x, actor.scale_y, false, actor.x, actor.y); + } + for(auto& fixture : $fixtures) { - position_sprite(fixture.st, fixture.cell, + fixture.pos = position_sprite(fixture.st, fixture.cell, fixture.scale_x, fixture.scale_y, false, fixture.x, fixture.y); - fixture.pos = fixture.st.sprite->getPosition(); } $arena.init(); @@ -101,6 +116,7 @@ namespace boss { } sf::Vector2f UI::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 = $arena.cell_for(cell_name); float x = float(at_mid ? cell.mid_x : cell.x); float y = float(at_mid ? cell.mid_y : cell.y); @@ -126,8 +142,9 @@ namespace boss { window.draw(*fixture.st.sprite); } - window.draw(*$boss_sprite.sprite); - window.draw(*$player_sprite.sprite); + for(auto& actor : $actors) { + window.draw(*actor.st.sprite); + } if(DEBUG) $arena.debug_layout(window); } @@ -142,9 +159,15 @@ namespace boss { $arena.show_text("status", msg); } - sf::Vector2f UI::move_actor(textures::SpriteTexture& st, const std::string& actor, const std::string& cell_name) { - float scale = $scene.actors[actor]["scale"]; - return position_sprite(st, cell_name, scale, scale, $scene.actors[actor]["mid_cell"]); + void UI::move_actor(const std::string& actor, const std::string& cell_name) { + auto& config = $actors.at($actor_name_ids.at(actor)); + config.cell = cell_name; + config.pos = position_sprite(config.st, config.cell, config.scale_x, config.scale_y, YOU_REMOVED_MID_CELL_IDIOT); + } + + void UI::animate_actor(const std::string& actor) { + auto& config = $actors.at($actor_name_ids.at(actor)); + config.anim.play(); } void UI::play_animations() { @@ -154,13 +177,10 @@ namespace boss { } } - if($boss_anim.playing) { - if($boss_anim.current == 0) { - auto scream = $world->get($boss_id); - sound::play(scream.attack); + for(auto& actor : $actors) { + if(actor.anim.playing) { + animation::apply(actor.anim, *actor.st.sprite, actor.pos); } - - animation::apply($boss_anim, *$boss_sprite.sprite, $boss_pos); } } } diff --git a/boss/ui.hpp b/boss/ui.hpp index aa32c3f..2c33594 100644 --- a/boss/ui.hpp +++ b/boss/ui.hpp @@ -6,6 +6,7 @@ #include "textures.hpp" #include "gui/combat_ui.hpp" #include "components.hpp" +#include struct AnimatedFixture { textures::SpriteTexture st; @@ -29,15 +30,13 @@ namespace boss { Entity $boss_id = NONE; components::AnimatedScene $scene; gui::CombatUI $combat_ui; - SpriteTexture $boss_sprite; - SpriteTexture $player_sprite; SpriteTexture $floor_sprite; guecs::UI $arena; guecs::UI $actions; - components::Animation $boss_anim; - sf::Vector2f $boss_pos; + std::unordered_map $actor_name_ids; std::vector $fixtures; + std::vector $actors; UI(shared_ptr world, Entity boss_id); @@ -46,7 +45,8 @@ namespace boss { 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 status(const std::wstring& msg); - sf::Vector2f move_actor(SpriteTexture& st, const std::string& actor, const std::string& cell_name); + void move_actor(const std::string& actor, const std::string& cell_name); + void animate_actor(const std::string& actor); void play_animations(); }; }