Refactored the bossfight_ui so it will work with any description of a boss fight.

This commit is contained in:
Zed A. Shaw 2025-03-02 12:36:08 -05:00
parent 43835da88f
commit a3f6ba3c03
12 changed files with 99 additions and 69 deletions

View file

@ -3,45 +3,56 @@
#include "sound.hpp"
namespace gui {
BossFightUI::BossFightUI(GameLevel level)
: $level(level)
BossFightUI::BossFightUI(GameLevel level, std::string boss_name)
: $level(level),
$config($level.world->get_the<components::GameConfig>()),
$boss_name(boss_name)
{
$status.position(0, 0, 300, SCREEN_HEIGHT);
$status.position(0, 0, BOSS_VIEW_X, SCREEN_HEIGHT);
$status.layout(
"[main_status]"
"[(150)status_3|(150)status_4]"
"[(150)status_5|(150)status_6]"
"[(150)status_7|(150)status_8]"
);
"[(150)status_7|(150)status_8]");
$overlay.position(BOSS_VIEW_X, BOSS_VIEW_Y,
BOSS_VIEW_WIDTH, BOSS_VIEW_HEIGHT);
$overlay.position(300, 0, SCREEN_WIDTH - 300, SCREEN_HEIGHT);
$overlay.layout("[overlay_1|overlay_2|overlay_4]"
"[overlay_5|overlay_6|overlay_8]"
"[overlay_9|overlay_10|overlay_12]"
"[overlay_13|overlay_14|overlay_16]");
"[overlay_5|overlay_6|overlay_8]"
"[overlay_9|overlay_10|overlay_12]"
"[overlay_13|overlay_14|overlay_16]");
$boss_background = textures::get("boss_fight_background");
auto bg_bounds = $boss_background.sprite->getLocalBounds();
$boss_background.sprite->setPosition({300, 0});
$boss_image = textures::get("rat_king_boss");
sf::IntRect frame_rect{{0,0},{720,720}};
$boss_image.sprite->setTextureRect(frame_rect);
$boss_image.sprite->setScale($scale);
auto bounds = $boss_image.sprite->getLocalBounds();
float x_diff = bg_bounds.size.x / 2;
$boss_image.sprite->setOrigin({bounds.size.x / 2, bounds.size.y / 2});
$boss_image.sprite->setPosition({300.0f + x_diff, bounds.size.y / 2});
$sounds = components::get<components::Sound>($config.bosses[boss_name]);
$weapon_hit_sound = $config.bosses[$boss_name]["weapon_sound"];
}
void BossFightUI::init() {
auto& config = $level.world->get_the<components::GameConfig>();
$sounds = components::get<components::Sound>(config.enemies["RAT_KING"]);
$animation = components::get<components::Animation>(config.enemies["RAT_KING"]);
$animation.texture_width = 720;
void BossFightUI::configure_sprite() {
auto& boss = $config.bosses[$boss_name];
$sprite_config = components::get<components::Sprite>(boss);
$animation = components::get<components::Animation>(boss);
$animation.texture_width = $sprite_config.width;
$boss_image = textures::get($sprite_config.name);
sf::IntRect frame_rect{{0,0},{$sprite_config.width,$sprite_config.height}};
$boss_image.sprite->setTextureRect(frame_rect);
$boss_image.sprite->setScale($scale);
auto bounds = $boss_image.sprite->getLocalBounds();
auto bg_bounds = $boss_background.sprite->getLocalBounds();
float x_diff = bg_bounds.size.x / 2;
$boss_image.sprite->setOrigin({bounds.size.x / 2, bounds.size.y / 2});
$boss_image.sprite->setPosition({float(BOSS_VIEW_X) + x_diff, bounds.size.y / 2});
}
void BossFightUI::configure_background() {
auto& boss = $config.bosses[$boss_name];
$boss_background = textures::get(boss["background"]);
$boss_background.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y});
$status.world().set_the<Background>({$status.$parser});
}
void BossFightUI::configure_gui() {
for(auto& [name, cell] : $status.cells()) {
auto button = $status.entity(name);
$status.set<Rectangle>(button, {});
@ -66,8 +77,15 @@ namespace gui {
$overlay.init();
}
void BossFightUI::init() {
// background must come first
configure_background();
configure_sprite();
configure_gui();
}
void BossFightUI::bounce_boss(sf::RenderWindow& window) {
sf::IntRect frame_rect{{0,0},{720,720}};
sf::IntRect frame_rect{{0,0},{$sprite_config.width,$sprite_config.height}};
auto scale = $scale;
$animation.step(scale, frame_rect);
$boss_image.sprite->setScale(scale);
@ -76,8 +94,8 @@ namespace gui {
sound::play($sounds.attack);
}
if(!sound::playing("Sword_Hit_2") && $animation.subframe > 1.2 && $animation.subframe < 1.5) {
sound::play("Sword_Hit_2");
if(!sound::playing($weapon_hit_sound) && $animation.subframe > 1.2 && $animation.subframe < 1.5) {
sound::play($weapon_hit_sound);
}
$boss_image.sprite->setTextureRect(frame_rect);