Scene now renders correctly, but I need to pull out the json in config_scene_element and make a component that's loaded.

This commit is contained in:
Zed A. Shaw 2026-02-13 23:30:57 -05:00
parent 2cef58be69
commit 458bf7e25e
2 changed files with 18 additions and 25 deletions

View file

@ -8,7 +8,7 @@
const bool DEBUG=false; const bool DEBUG=false;
namespace scene { namespace scene {
Element Engine::config_scene_element(nlohmann::json& config, bool and_play, bool duped) { Element Engine::config_scene_element(nlohmann::json& config, 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);
@ -20,7 +20,7 @@ namespace scene {
// BUG: put the .json file to load as a default/optional arg // BUG: put the .json file to load as a default/optional arg
auto anim = animate2::load("./assets/animate2.json", sprite_name); auto anim = animate2::load("./assets/animate2.json", sprite_name);
if(and_play) anim.play(); anim.play();
anim.transform.flipped = flipped; anim.transform.flipped = flipped;
@ -38,7 +38,7 @@ namespace scene {
$scene(scene) $scene(scene)
{ {
for(auto& config : $scene.actors) { for(auto& config : $scene.actors) {
auto element = config_scene_element(config, false, false); auto element = config_scene_element(config, false);
dbc::check(!$actor_name_ids.contains(element.name), dbc::check(!$actor_name_ids.contains(element.name),
fmt::format("actors key {} already exists", element.name)); fmt::format("actors key {} already exists", element.name));
$actors.push_back(element); $actors.push_back(element);
@ -46,7 +46,7 @@ namespace scene {
} }
for(auto& fixture : $scene.fixtures) { for(auto& fixture : $scene.fixtures) {
auto element = config_scene_element(fixture, true, true); auto element = config_scene_element(fixture, true);
$fixtures.push_back(element); $fixtures.push_back(element);
} }
@ -125,28 +125,20 @@ namespace scene {
if(!config.anim.playing) config.anim.play(); if(!config.anim.playing) config.anim.play();
} }
void Engine::play_animations() { inline void this_is_stupid_refactor(std::vector<Element>& elements) {
for(auto& fixture : $fixtures) { for(auto& element : elements) {
if(fixture.anim.playing) { if(element.anim.playing) {
fixture.anim.update(); element.anim.update();
fixture.anim.motion(*fixture.st.sprite, fixture.pos, fixture.scale); element.anim.motion(*element.st.sprite, element.pos, element.scale);
fixture.anim.apply(*fixture.st.sprite); element.anim.apply(*element.st.sprite);
} else { if(element.effect != nullptr) element.anim.apply_effect(element.effect);
fixture.effect = nullptr; }
} }
} }
for(auto& actor : $actors) { void Engine::play_animations() {
if(actor.anim.playing) { this_is_stupid_refactor($fixtures);
actor.anim.update(); this_is_stupid_refactor($actors);
actor.anim.motion(*actor.st.sprite, actor.pos, actor.scale);
actor.anim.apply(*actor.st.sprite);
// REFACTOR: actor.anim.apply(actor.text, actor.pos);
// if(actor.effect != nullptr) actor.anim.apply_effect(actor.effect);
} else {
actor.effect = nullptr;
}
}
} }
sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff, float y_diff) { sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff, float y_diff) {
@ -179,9 +171,10 @@ namespace scene {
void Engine::set_end_cb(std::function<void()> cb) { void Engine::set_end_cb(std::function<void()> cb) {
for(auto& actor : $actors) { for(auto& actor : $actors) {
actor.anim.onLoop = [=](auto& seq, auto& tr) -> bool { actor.anim.onLoop = [&,cb](auto& seq, auto& tr) -> bool {
seq.current = tr.toggled ? seq.frame_count - 1 : 0; seq.current = tr.toggled ? seq.frame_count - 1 : 0;
cb(); cb();
actor.effect = nullptr;
return tr.looped; return tr.looped;
}; };
} }

View file

@ -42,7 +42,7 @@ namespace scene {
void render(sf::RenderTexture& view); void render(sf::RenderTexture& view);
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); void attach_text(const std::string& actor, const std::string& text);
Element config_scene_element(nlohmann::json& config, bool and_play, bool duped); Element config_scene_element(nlohmann::json& config, bool duped);
sf::Vector2f position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, 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, sf::Vector2f scale, bool at_mid, float x_diff=0.0f, float y_diff=0.0f);