127 lines
3.7 KiB
C++
127 lines
3.7 KiB
C++
#include "scene.hpp"
|
|
#include "animation.hpp"
|
|
#include <ranges>
|
|
|
|
const bool DEBUG=false;
|
|
const bool YOU_REMOVED_MID_CELL_IDIOT=false;
|
|
|
|
namespace scene {
|
|
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, duped);
|
|
|
|
float scale_x = config["scale_x"];
|
|
float scale_y = config["scale_y"];
|
|
float x = config["x"];
|
|
float y = config["y"];
|
|
|
|
// BUG: need to make animation optional
|
|
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"];
|
|
|
|
bool at_mid = config["at_mid"];
|
|
|
|
return {name, st, anim, cell, scale_x, scale_y, x, y, at_mid};
|
|
}
|
|
|
|
Engine::Engine(components::AnimatedScene& scene) :
|
|
$scene(scene)
|
|
{
|
|
for(auto& config : $scene.actors) {
|
|
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);
|
|
}
|
|
|
|
for(auto& fixture : $scene.fixtures) {
|
|
auto element = config_scene_element(fixture, true, true);
|
|
$fixtures.push_back(element);
|
|
}
|
|
|
|
for(auto& line : $scene.layout) {
|
|
$layout.append(line);
|
|
}
|
|
}
|
|
|
|
void Engine::init() {
|
|
$ui.position(SCREEN_WIDTH-BOSS_VIEW_WIDTH,0, BOSS_VIEW_WIDTH, SCREEN_HEIGHT);
|
|
$ui.set<guecs::Background>($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT});
|
|
auto& background = $ui.get<guecs::Background>($ui.MAIN);
|
|
background.set_sprite($scene.background, true);
|
|
|
|
$ui.layout($layout);
|
|
|
|
for(auto& actor : $actors) {
|
|
actor.pos = position_sprite(actor.st, actor.cell,
|
|
actor.scale_x, actor.scale_y, actor.at_mid, actor.x, actor.y);
|
|
}
|
|
|
|
for(auto& fixture : $fixtures) {
|
|
fixture.pos = position_sprite(fixture.st, fixture.cell,
|
|
fixture.scale_x, fixture.scale_y, fixture.at_mid, fixture.x, fixture.y);
|
|
}
|
|
}
|
|
|
|
bool Engine::mouse(float x, float y, guecs::Modifiers mods) {
|
|
return $ui.mouse(x, y, mods);
|
|
}
|
|
|
|
void Engine::render(sf::RenderWindow& window) {
|
|
$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, config.at_mid);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|