#include "boss/ui.hpp" #include "constants.hpp" #include "components.hpp" #include "animation.hpp" #include #include #include "sound.hpp" namespace boss { using namespace guecs; UI::UI(shared_ptr world, Entity boss_id) : $world(world), $boss_id(boss_id), $scene(world->get($boss_id)), $combat_ui(true) { std::string sprite_name = $scene.boss["sprite"]; $boss_sprite = textures::get_sprite(sprite_name); // floor is std::optional if($scene.floor) { $floor_sprite = textures::get_sprite(*$scene.floor); } $player_sprite = textures::get_sprite($scene.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); // clone the sprite so it can be positioned st.sprite = std::make_shared(*st.texture); auto anim = animation::load(name); float scale = fixture["scale"]; std::string cell = fixture["cell"]; $fixtures.emplace_back(st, anim, cell, scale); } } void UI::init() { $arena.position(SCREEN_WIDTH-BOSS_VIEW_WIDTH,0, BOSS_VIEW_WIDTH, SCREEN_HEIGHT); $arena.set($arena.MAIN, {$arena.$parser, THEME.TRANSPARENT}); auto& background = $arena.get($arena.MAIN); background.set_sprite($scene.background, true); $arena.layout( "[status|boss1 |boss2 |boss3 |boss4 |_]" "[torch1|boss5 |boss6 |boss7 |boss8 |torch2]" "[floor1|boss9 |boss10|boss11|boss12|_]" "[floor2|boss13|boss14|boss15|boss16|_]" "[floor3|player1|player2|player3|player4|_]" "[floor4|player5|player6|player7|player8|_]" ); move_boss($scene.boss["start_pos"]); move_player($scene.player["start_pos"]); if($scene.floor) { position_sprite($floor_sprite, $scene.floor_pos, 1.0, false); } for(auto& fixture : $fixtures) { position_sprite(fixture.st, fixture.cell, fixture.scale, false); } $arena.init(); $actions.position(0,0, SCREEN_WIDTH-BOSS_VIEW_WIDTH, SCREEN_HEIGHT); $actions.layout( "[*(200,400)combat|_]" "[_|_]" "[_|_]" "[_|_]" "[*(200,300)stats]" "[_]" "[_]"); auto stats = $actions.entity("stats"); $actions.set(stats, {}); $actions.set(stats, {L"stats"}); $actions.init(); auto& cell = $actions.cell_for("combat"); $combat_ui.init(cell.x, cell.y, cell.w, cell.h); } void UI::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale, bool at_mid) { 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); st.sprite->setPosition({x, y}); st.sprite->setScale({scale, scale}); } void UI::render(sf::RenderWindow& window) { $actions.render(window); $combat_ui.render(window); if($floor_sprite.sprite) { window.draw(*$floor_sprite.sprite); } $arena.render(window); window.draw(*$boss_sprite.sprite); window.draw(*$player_sprite.sprite); for(auto& fixture : $fixtures) { window.draw(*fixture.st.sprite); } } bool UI::mouse(float x, float y, Modifiers mods) { return $arena.mouse(x, y, mods) || $combat_ui.mouse(x, y, mods) || $actions.mouse(x, y, mods); } void UI::status(const std::wstring& msg) { $arena.show_text("status", msg); } void UI::move_boss(const std::string& cell_name) { position_sprite($boss_sprite, cell_name, $scene.boss["scale"], $scene.boss["mid_cell"]); auto& cell = $arena.cell_for(cell_name); $boss_pos = {float(cell.mid_x), float(cell.mid_y)}; } void UI::move_player(const std::string& cell_name) { position_sprite($player_sprite, cell_name, $scene.player["scale"], $scene.player["mid_cell"]); } void UI::play_animations(sf::RenderWindow& window) { if($boss_anim.playing) { using namespace std::chrono_literals; auto& sound = $world->get($boss_id); animation::apply($boss_anim, *$boss_sprite.sprite, $boss_pos); if($boss_anim.current == 1 && !sound::playing(sound.attack)) { sound::play(sound.attack); } // https://www.construct.net/en/blogs/ashleys-blog-2/using-lerp-delta-time-924 // frame start render(window); window.display(); // frame end // deltaTime = end - start // lerp(a, b, 1 - f ^ deltaTime) // std::this_thread::sleep_for(16ms); } else { animation::apply($boss_anim, *$boss_sprite.sprite, $boss_pos); } } }