Now fixtures and actors are loaded the same.

This commit is contained in:
Zed A. Shaw 2025-10-23 13:48:58 -04:00
parent 4f39f2a504
commit 23f54bd4fe
3 changed files with 73 additions and 56 deletions

View file

@ -7,27 +7,39 @@ const bool YOU_REMOVED_MID_CELL_IDIOT=false;
namespace scene {
using namespace guecs;
Element config_scene_element(nlohmann::json& config, bool and_play, bool duped) {
std::string sprite_name = config["sprite"];
auto st = textures::get_sprite(sprite_name);
if(duped) {
st.sprite = std::make_shared<sf::Sprite>(*st.texture);
}
float scale_x = config["scale_x"];
float scale_y = config["scale_y"];
float x = config["x"];
float y = config["y"];
auto anim = animation::load(sprite_name);
if(and_play) anim.play();
anim.scale_x = scale_x;
anim.scale_y = scale_y;
std::string cell = config["cell"];
std::string name = config["name"];
return {name, st, anim, cell, scale_x, scale_y, x, y};
}
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);
auto element = config_scene_element(config, false, false);
dbc::check(!$actor_name_ids.contains(element.name),
fmt::format("actors key {} already exists", element.name));
$actors.push_back(element);
$actor_name_ids.try_emplace(element.name, $actors.size() - 1);
}
// floor is std::optional
@ -36,23 +48,8 @@ namespace scene {
}
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);
auto element = config_scene_element(fixture, true, true);
$fixtures.push_back(element);
}
}
@ -90,19 +87,6 @@ namespace scene {
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);
@ -121,7 +105,6 @@ namespace scene {
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;
@ -147,4 +130,16 @@ namespace scene {
}
}
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;
}
}