Moved the text attaching code to the arena.

This commit is contained in:
Zed A. Shaw 2025-12-24 00:58:38 -05:00
parent f50e713179
commit 0d23cf9537
5 changed files with 43 additions and 15 deletions

View file

@ -128,11 +128,14 @@ namespace boss {
case BattleHostState::disagree: { case BattleHostState::disagree: {
std::string player_move = Random::uniform(0, 1) == 0 ? "player1" : "player3"; std::string player_move = Random::uniform(0, 1) == 0 ? "player1" : "player3";
$ui.move_actor("player", player_move); $ui.move_actor("player", player_move);
if(result.player_did > 0) $ui.animate_actor("player");
$ui.damage("boss", result.player_did); $ui.damage("boss", result.player_did);
} break; } break;
case BattleHostState::not_host: { case BattleHostState::not_host: {
std::string boss_move = Random::uniform(0, 1) == 0 ? "boss5" : "boss6"; std::string boss_move = Random::uniform(0, 1) == 0 ? "boss5" : "boss6";
$ui.move_actor("boss", boss_move); $ui.move_actor("boss", boss_move);
if(result.enemy_did > 0) $ui.animate_actor("boss");
$ui.damage("player", result.enemy_did); $ui.damage("player", result.enemy_did);
} break; } break;
case BattleHostState::out_of_ap: case BattleHostState::out_of_ap:
@ -154,7 +157,7 @@ namespace boss {
void Fight::render(sf::RenderWindow& window) { void Fight::render(sf::RenderWindow& window) {
$ui.play_animations(); $ui.play_animations();
$ui.render(window); $ui.render();
} }
bool Fight::handle_mouse(game::Event ev) { bool Fight::handle_mouse(game::Event ev) {
@ -196,6 +199,7 @@ namespace boss {
void Fight::set_window(sf::RenderWindow* window) { void Fight::set_window(sf::RenderWindow* window) {
$window = window; $window = window;
$ui.set_window(window);
} }
bool Fight::handle_keyboard_mouse() { bool Fight::handle_keyboard_mouse() {

View file

@ -71,14 +71,18 @@ namespace boss {
$actions.show_text("stats", status); $actions.show_text("stats", status);
} }
void UI::render(sf::RenderWindow& window) { void UI::set_window(sf::RenderWindow* window) {
$actions.render(window); $window = window;
$combat_ui.render(window); }
void UI::render() {
$actions.render(*$window);
$combat_ui.render(*$window);
$arena.render($view_texture); $arena.render($view_texture);
$camera.render($view_texture); $camera.render($view_texture);
$view_texture.display(); $view_texture.display();
window.draw($view_sprite); $window->draw($view_sprite);
} }
bool UI::mouse(float x, float y, Modifiers mods) { bool UI::mouse(float x, float y, Modifiers mods) {
@ -105,16 +109,12 @@ namespace boss {
} }
void UI::damage(const std::string& actor, int amount) { void UI::damage(const std::string& actor, int amount) {
auto& config = $arena.actor_config(actor); // BUG: the $arena should really do this
if(amount == 0) { if(amount == 0) {
$arena.$ui.show_text(config.cell, $arena.attach_text(actor, "MISS!");
fmt::format(L"{} MISSED!", guecs::to_wstring(actor)));
} else { } else {
$arena.$ui.show_text(config.cell, $arena.attach_text(actor, "HIT!");
fmt::format(L"{} HIT {}!", guecs::to_wstring(actor), amount));
} }
} }
void UI::zoom(const std::string &cell_name, double ratio) { void UI::zoom(const std::string &cell_name, double ratio) {

View file

@ -18,6 +18,7 @@ namespace boss {
using std::shared_ptr; using std::shared_ptr;
struct UI { struct UI {
sf::RenderWindow* $window = nullptr;
shared_ptr<DinkyECS::World> $world = nullptr; shared_ptr<DinkyECS::World> $world = nullptr;
DinkyECS::Entity $boss_id = DinkyECS::NONE; DinkyECS::Entity $boss_id = DinkyECS::NONE;
DinkyECS::Entity $player_id = DinkyECS::NONE; DinkyECS::Entity $player_id = DinkyECS::NONE;
@ -31,7 +32,8 @@ namespace boss {
UI(shared_ptr<DinkyECS::World> world, DinkyECS::Entity boss_id, DinkyECS::Entity player_id); UI(shared_ptr<DinkyECS::World> world, DinkyECS::Entity boss_id, DinkyECS::Entity player_id);
void init(); void init();
void render(sf::RenderWindow& window); void set_window(sf::RenderWindow* window);
void render();
bool mouse(float x, float y, guecs::Modifiers mods); bool mouse(float x, float y, guecs::Modifiers mods);
void status(const std::wstring& msg, const std::wstring &button_msg); void status(const std::wstring& msg, const std::wstring &button_msg);
void move_actor(const std::string& actor, const std::string& cell_name); void move_actor(const std::string& actor, const std::string& cell_name);

View file

@ -5,7 +5,7 @@
const bool DEBUG=false; const bool DEBUG=false;
namespace scene { namespace scene {
Element config_scene_element(nlohmann::json& config, bool and_play, bool duped) { Element Engine::config_scene_element(nlohmann::json& config, bool and_play, bool duped) {
std::string sprite_name = config["sprite"]; std::string sprite_name = config["sprite"];
auto st = textures::get_sprite(sprite_name, duped); auto st = textures::get_sprite(sprite_name, duped);
@ -25,7 +25,9 @@ namespace scene {
bool at_mid = config["at_mid"]; bool at_mid = config["at_mid"];
return {name, st, anim, cell, scale_x, scale_y, x, y, at_mid, flipped}; sf::Text text(*$ui.$font, "", 60);
return {name, st, anim, cell, scale_x, scale_y, x, y, at_mid, flipped, text};
} }
Engine::Engine(components::AnimatedScene& scene) : Engine::Engine(components::AnimatedScene& scene) :
@ -68,6 +70,16 @@ namespace scene {
} }
} }
void Engine::attach_text(const std::string& actor, const std::string& text) {
auto& element = actor_config(actor);
element.text.setPosition(element.pos);
element.text.setScale({element.scale_x, element.scale_y});
element.text.setFillColor(sf::Color::Red);
element.text.setOutlineThickness(2.0f);
element.text.setOutlineColor(sf::Color::Black);
element.text.setString(text);
}
bool Engine::mouse(float x, float y, guecs::Modifiers mods) { bool Engine::mouse(float x, float y, guecs::Modifiers mods) {
return $ui.mouse(x, y, mods); return $ui.mouse(x, y, mods);
} }
@ -77,12 +89,15 @@ namespace scene {
for(auto& fixture : $fixtures) { for(auto& fixture : $fixtures) {
window.draw(*fixture.st.sprite); window.draw(*fixture.st.sprite);
window.draw(fixture.text);
} }
for(auto& actor : $actors) { for(auto& actor : $actors) {
window.draw(*actor.st.sprite); window.draw(*actor.st.sprite);
window.draw(actor.text);
} }
if(DEBUG) $ui.debug_layout(window); if(DEBUG) $ui.debug_layout(window);
} }
@ -105,12 +120,16 @@ namespace scene {
for(auto& fixture : $fixtures) { for(auto& fixture : $fixtures) {
if(fixture.anim.playing) { if(fixture.anim.playing) {
fixture.anim.apply(*fixture.st.sprite, fixture.pos); fixture.anim.apply(*fixture.st.sprite, fixture.pos);
fixture.text.setPosition(fixture.pos);
fixture.text.setScale({fixture.scale_x, fixture.scale_y});
} }
} }
for(auto& actor : $actors) { for(auto& actor : $actors) {
if(actor.anim.playing) { if(actor.anim.playing) {
actor.anim.apply(*actor.st.sprite, actor.pos); actor.anim.apply(*actor.st.sprite, actor.pos);
actor.text.setPosition(actor.pos);
actor.text.setScale({actor.scale_x, actor.scale_y});
} }
} }
} }

View file

@ -21,6 +21,7 @@ namespace scene {
float y = 0; float y = 0;
bool at_mid=false; bool at_mid=false;
bool flipped=false; bool flipped=false;
sf::Text text;
sf::Vector2f pos{0,0}; sf::Vector2f pos{0,0};
}; };
@ -37,6 +38,8 @@ namespace scene {
void init(); void init();
void render(sf::RenderTarget& window); void render(sf::RenderTarget& window);
bool mouse(float x, float y, guecs::Modifiers mods); bool mouse(float x, float y, guecs::Modifiers mods);
void attach_text(const std::string& actor, const std::string& text);
Element config_scene_element(nlohmann::json& config, bool and_play, bool duped);
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);