Moved the animated scene into its own system for better development.

This commit is contained in:
Zed A. Shaw 2025-10-23 11:37:35 -04:00
parent 49c9702041
commit 4f39f2a504
7 changed files with 209 additions and 159 deletions

View file

@ -6,7 +6,7 @@ namespace boss {
Fight::Fight(shared_ptr<World> world, Entity boss_id) :
$world(world),
$boss_id(boss_id),
$ui(world, boss_id)
$ui(world->get<components::AnimatedScene>($boss_id), boss_id)
{
$ui.init();
}

View file

@ -8,6 +8,7 @@
#include <any>
namespace boss {
using namespace DinkyECS;
using std::shared_ptr;
enum class State {

View file

@ -1,99 +1,20 @@
#include "boss/ui.hpp"
#include "scene.hpp"
#include "constants.hpp"
#include "components.hpp"
#include "animation.hpp"
#include <chrono>
#include <thread>
#include "sound.hpp"
const bool DEBUG=false;
const bool YOU_REMOVED_MID_CELL_IDIOT=false;
namespace boss {
using namespace guecs;
UI::UI(shared_ptr<World> world, Entity boss_id) :
$world(world),
UI::UI(components::AnimatedScene& scene, Entity boss_id) :
$boss_id(boss_id),
$scene(world->get<components::AnimatedScene>($boss_id)),
$combat_ui(true)
$combat_ui(true),
$arena(scene)
{
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);
}
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<sf::Sprite>(*st.texture);
float scale_x = fixture["scale_x"];
float scale_y = fixture["scale_y"];
auto anim = animation::load(name);
anim.scale_x = scale_x;
anim.scale_y = scale_y;
anim.play();
std::string cell = fixture["cell"];
float x = fixture["x"];
float y = fixture["y"];
$fixtures.emplace_back(st, anim, cell, scale_x, scale_y, x, y);
}
}
void UI::init() {
$arena.position(SCREEN_WIDTH-BOSS_VIEW_WIDTH,0, BOSS_VIEW_WIDTH, SCREEN_HEIGHT);
$arena.set<Background>($arena.MAIN, {$arena.$parser, THEME.TRANSPARENT});
auto& background = $arena.get<Background>($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|_]"
);
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) {
fixture.pos = position_sprite(fixture.st, fixture.cell,
fixture.scale_x, fixture.scale_y, false, fixture.x, fixture.y);
}
$arena.init();
$actions.position(0,0, SCREEN_WIDTH-BOSS_VIEW_WIDTH, SCREEN_HEIGHT);
@ -115,38 +36,10 @@ namespace boss {
$combat_ui.init(cell.x, cell.y, cell.w, cell.h);
}
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);
sf::Vector2f pos{x + x_diff, y + y_diff};
st.sprite->setPosition(pos);
st.sprite->setScale({scale_x, scale_y});
return pos;
}
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);
for(auto& fixture : $fixtures) {
window.draw(*fixture.st.sprite);
}
for(auto& actor : $actors) {
window.draw(*actor.st.sprite);
}
if(DEBUG) $arena.debug_layout(window);
}
bool UI::mouse(float x, float y, Modifiers mods) {
@ -156,31 +49,18 @@ namespace boss {
}
void UI::status(const std::wstring& msg) {
$arena.show_text("status", msg);
$arena.$ui.show_text("status", msg);
}
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);
$arena.move_actor(actor, cell_name);
}
void UI::animate_actor(const std::string& actor) {
auto& config = $actors.at($actor_name_ids.at(actor));
config.anim.play();
$arena.animate_actor(actor);
}
void UI::play_animations() {
for(auto& fixture : $fixtures) {
if(fixture.anim.playing) {
animation::apply(fixture.anim, *fixture.st.sprite, fixture.pos);
}
}
for(auto& actor : $actors) {
if(actor.anim.playing) {
animation::apply(actor.anim, *actor.st.sprite, actor.pos);
}
}
$arena.play_animations();
}
}

View file

@ -1,49 +1,23 @@
#pragma once
#include "dinkyecs.hpp"
#include <memory>
#include <SFML/Graphics/RenderWindow.hpp>
#include <guecs/ui.hpp>
#include "textures.hpp"
#include "gui/combat_ui.hpp"
#include "components.hpp"
#include <unordered_map>
struct AnimatedFixture {
textures::SpriteTexture st;
components::Animation anim;
std::string cell;
float scale_x;
float scale_y;
float x;
float y;
bool at_mid=false;
sf::Vector2f pos{0,0};
};
#include "scene.hpp"
namespace boss {
using std::shared_ptr;
using namespace DinkyECS;
using namespace textures;
struct UI {
shared_ptr<World> $world = nullptr;
Entity $boss_id = NONE;
components::AnimatedScene $scene;
DinkyECS::Entity $boss_id = DinkyECS::NONE;
gui::CombatUI $combat_ui;
SpriteTexture $floor_sprite;
guecs::UI $arena;
scene::Engine $arena;
guecs::UI $actions;
std::unordered_map<std::string, int> $actor_name_ids;
std::vector<AnimatedFixture> $fixtures;
std::vector<AnimatedFixture> $actors;
UI(shared_ptr<World> world, Entity boss_id);
UI(components::AnimatedScene &scene, DinkyECS::Entity boss_id);
void init();
void render(sf::RenderWindow& window);
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);
void move_actor(const std::string& actor, const std::string& cell_name);
void animate_actor(const std::string& actor);

View file

@ -118,6 +118,7 @@ sources = [
'rand.cpp',
'raycaster.cpp',
'rituals.cpp',
'scene.cpp',
'shaders.cpp',
'shiterator.hpp',
'sound.cpp',

150
scene.cpp Normal file
View file

@ -0,0 +1,150 @@
#include "scene.hpp"
#include "animation.hpp"
const bool DEBUG=false;
const bool YOU_REMOVED_MID_CELL_IDIOT=false;
namespace scene {
using namespace guecs;
Engine::Engine(components::AnimatedScene& scene) :
$scene(scene)
{
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);
}
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<sf::Sprite>(*st.texture);
float scale_x = fixture["scale_x"];
float scale_y = fixture["scale_y"];
auto anim = animation::load(name);
anim.scale_x = scale_x;
anim.scale_y = scale_y;
anim.play();
std::string cell = fixture["cell"];
float x = fixture["x"];
float y = fixture["y"];
$fixtures.emplace_back(st, anim, cell, scale_x, scale_y, x, y);
}
}
void Engine::init() {
$ui.position(SCREEN_WIDTH-BOSS_VIEW_WIDTH,0, BOSS_VIEW_WIDTH, SCREEN_HEIGHT);
$ui.set<Background>($ui.MAIN, {$ui.$parser, THEME.TRANSPARENT});
auto& background = $ui.get<Background>($ui.MAIN);
background.set_sprite($scene.background, true);
$ui.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|_]"
);
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) {
fixture.pos = position_sprite(fixture.st, fixture.cell,
fixture.scale_x, fixture.scale_y, false, fixture.x, fixture.y);
}
}
bool Engine::mouse(float x, float y, Modifiers mods) {
return $ui.mouse(x, y, mods);
}
sf::Vector2f Engine::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 = $ui.cell_for(cell_name);
float x = float(at_mid ? cell.mid_x : cell.x);
float y = float(at_mid ? cell.mid_y : cell.y);
sf::Vector2f pos{x + x_diff, y + y_diff};
st.sprite->setPosition(pos);
st.sprite->setScale({scale_x, scale_y});
return pos;
}
void Engine::render(sf::RenderWindow& window) {
if($floor_sprite.sprite) {
window.draw(*$floor_sprite.sprite);
}
$ui.render(window);
for(auto& fixture : $fixtures) {
window.draw(*fixture.st.sprite);
}
for(auto& actor : $actors) {
window.draw(*actor.st.sprite);
}
if(DEBUG) $ui.debug_layout(window);
}
void Engine::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 Engine::animate_actor(const std::string& actor) {
auto& config = $actors.at($actor_name_ids.at(actor));
config.anim.play();
}
void Engine::play_animations() {
for(auto& fixture : $fixtures) {
if(fixture.anim.playing) {
animation::apply(fixture.anim, *fixture.st.sprite, fixture.pos);
}
}
for(auto& actor : $actors) {
if(actor.anim.playing) {
animation::apply(actor.anim, *actor.st.sprite, actor.pos);
}
}
}
}

44
scene.hpp Normal file
View file

@ -0,0 +1,44 @@
#pragma once
#include <memory>
#include <unordered_map>
#include "components.hpp"
#include "textures.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <guecs/ui.hpp>
namespace scene {
using std::shared_ptr;
using namespace textures;
struct Element {
textures::SpriteTexture st;
components::Animation anim;
std::string cell;
float scale_x;
float scale_y;
float x;
float y;
bool at_mid=false;
sf::Vector2f pos{0,0};
};
struct Engine {
guecs::UI $ui;
SpriteTexture $floor_sprite;
components::AnimatedScene& $scene;
std::unordered_map<std::string, int> $actor_name_ids;
std::vector<Element> $fixtures;
std::vector<Element> $actors;
Engine(components::AnimatedScene& scene);
void init();
void render(sf::RenderWindow& window);
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 move_actor(const std::string& actor, const std::string& cell_name);
void animate_actor(const std::string& actor);
void play_animations();
};
}