Moved the animated scene into its own system for better development.
This commit is contained in:
parent
49c9702041
commit
4f39f2a504
7 changed files with 209 additions and 159 deletions
150
scene.cpp
Normal file
150
scene.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue