Cleaned up the arena code more and closer to pulling it out for a scene system.

This commit is contained in:
Zed A. Shaw 2025-10-23 00:29:15 -04:00
parent 25f7096489
commit 49c9702041
4 changed files with 65 additions and 43 deletions

View file

@ -5,20 +5,22 @@
"background": "test_background", "background": "test_background",
"floor": false, "floor": false,
"floor_pos": "floor1", "floor_pos": "floor1",
"actors": { "actors": [
"player": { {
"sprite": "peasant_girl_rear_view", "name": "boss",
"start_pos": "player2",
"scale": 0.5,
"mid_cell": false
},
"boss": {
"sprite": "rat_king_boss", "sprite": "rat_king_boss",
"start_pos": "boss5", "cell": "boss5",
"scale": 0.6, "scale_x": 0.6,
"mid_cell": true "scale_y": 0.6
},
{
"name": "player",
"sprite": "peasant_girl_rear_view",
"cell": "player2",
"scale_x": 0.5,
"scale_y": 0.5
} }
}, ],
"fixtures": [ "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": "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", "scale_x": -0.5, "scale_y": 0.5, "cell": "torch2", "x": 132, "y": -30}

View file

@ -84,7 +84,7 @@ namespace boss {
break; break;
case ATTACK: { case ATTACK: {
$ui.status(L"PLAYER TURN"); $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<int>(data); int attack_id = std::any_cast<int>(data);
boss::System::combat(attack_id); boss::System::combat(attack_id);
state(State::PLAYER_TURN); state(State::PLAYER_TURN);
@ -105,8 +105,8 @@ namespace boss {
break; break;
case ATTACK: { case ATTACK: {
$ui.status(L"BOSS TURN"); $ui.status(L"BOSS TURN");
$ui.$boss_pos = $ui.move_actor($ui.$boss_sprite, "boss", run % 10 < 5 ? "boss5" : "boss6"); $ui.move_actor("boss", run % 10 < 5 ? "boss5" : "boss6");
$ui.$boss_anim.play(); $ui.animate_actor("boss");
int attack_id = std::any_cast<int>(data); int attack_id = std::any_cast<int>(data);
boss::System::combat(attack_id); boss::System::combat(attack_id);
state(State::BOSS_TURN); state(State::BOSS_TURN);

View file

@ -7,6 +7,7 @@
#include "sound.hpp" #include "sound.hpp"
const bool DEBUG=false; const bool DEBUG=false;
const bool YOU_REMOVED_MID_CELL_IDIOT=false;
namespace boss { namespace boss {
using namespace guecs; using namespace guecs;
@ -17,19 +18,32 @@ namespace boss {
$scene(world->get<components::AnimatedScene>($boss_id)), $scene(world->get<components::AnimatedScene>($boss_id)),
$combat_ui(true) $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 // floor is std::optional
if($scene.floor) { if($scene.floor) {
$floor_sprite = textures::get_sprite(*$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) { for(auto& fixture : $scene.fixtures) {
std::string name = fixture["name"]; std::string name = fixture["name"];
auto st = textures::get_sprite(name); auto st = textures::get_sprite(name);
@ -66,17 +80,18 @@ namespace boss {
"[floor4|player5|player6|player7|player8|_]" "[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) { if($scene.floor) {
position_sprite($floor_sprite, $scene.floor_pos, 1.0f, 1.0f, false); 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) { 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.scale_x, fixture.scale_y, false, fixture.x, fixture.y);
fixture.pos = fixture.st.sprite->getPosition();
} }
$arena.init(); $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) { 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); auto& cell = $arena.cell_for(cell_name);
float x = float(at_mid ? cell.mid_x : cell.x); float x = float(at_mid ? cell.mid_x : cell.x);
float y = float(at_mid ? cell.mid_y : cell.y); float y = float(at_mid ? cell.mid_y : cell.y);
@ -126,8 +142,9 @@ namespace boss {
window.draw(*fixture.st.sprite); window.draw(*fixture.st.sprite);
} }
window.draw(*$boss_sprite.sprite); for(auto& actor : $actors) {
window.draw(*$player_sprite.sprite); window.draw(*actor.st.sprite);
}
if(DEBUG) $arena.debug_layout(window); if(DEBUG) $arena.debug_layout(window);
} }
@ -142,9 +159,15 @@ namespace boss {
$arena.show_text("status", msg); $arena.show_text("status", msg);
} }
sf::Vector2f UI::move_actor(textures::SpriteTexture& st, const std::string& actor, const std::string& cell_name) { void UI::move_actor(const std::string& actor, const std::string& cell_name) {
float scale = $scene.actors[actor]["scale"]; auto& config = $actors.at($actor_name_ids.at(actor));
return position_sprite(st, cell_name, scale, scale, $scene.actors[actor]["mid_cell"]); 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() { void UI::play_animations() {
@ -154,13 +177,10 @@ namespace boss {
} }
} }
if($boss_anim.playing) { for(auto& actor : $actors) {
if($boss_anim.current == 0) { if(actor.anim.playing) {
auto scream = $world->get<components::Sound>($boss_id); animation::apply(actor.anim, *actor.st.sprite, actor.pos);
sound::play(scream.attack);
} }
animation::apply($boss_anim, *$boss_sprite.sprite, $boss_pos);
} }
} }
} }

View file

@ -6,6 +6,7 @@
#include "textures.hpp" #include "textures.hpp"
#include "gui/combat_ui.hpp" #include "gui/combat_ui.hpp"
#include "components.hpp" #include "components.hpp"
#include <unordered_map>
struct AnimatedFixture { struct AnimatedFixture {
textures::SpriteTexture st; textures::SpriteTexture st;
@ -29,15 +30,13 @@ namespace boss {
Entity $boss_id = NONE; Entity $boss_id = NONE;
components::AnimatedScene $scene; components::AnimatedScene $scene;
gui::CombatUI $combat_ui; gui::CombatUI $combat_ui;
SpriteTexture $boss_sprite;
SpriteTexture $player_sprite;
SpriteTexture $floor_sprite; SpriteTexture $floor_sprite;
guecs::UI $arena; guecs::UI $arena;
guecs::UI $actions; guecs::UI $actions;
components::Animation $boss_anim;
sf::Vector2f $boss_pos;
std::unordered_map<std::string, int> $actor_name_ids;
std::vector<AnimatedFixture> $fixtures; std::vector<AnimatedFixture> $fixtures;
std::vector<AnimatedFixture> $actors;
UI(shared_ptr<World> world, Entity boss_id); UI(shared_ptr<World> world, Entity boss_id);
@ -46,7 +45,8 @@ namespace boss {
bool mouse(float x, float y, guecs::Modifiers mods); 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); 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); 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(); void play_animations();
}; };
} }