Now fixtures and actors are loaded the same.
This commit is contained in:
parent
4f39f2a504
commit
23f54bd4fe
3 changed files with 73 additions and 56 deletions
|
|
@ -11,19 +11,39 @@
|
||||||
"sprite": "rat_king_boss",
|
"sprite": "rat_king_boss",
|
||||||
"cell": "boss5",
|
"cell": "boss5",
|
||||||
"scale_x": 0.6,
|
"scale_x": 0.6,
|
||||||
"scale_y": 0.6
|
"scale_y": 0.6,
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "player",
|
"name": "player",
|
||||||
"sprite": "peasant_girl_rear_view",
|
"sprite": "peasant_girl_rear_view",
|
||||||
"cell": "player2",
|
"cell": "player2",
|
||||||
"scale_x": 0.5,
|
"scale_x": 0.5,
|
||||||
"scale_y": 0.5
|
"scale_y": 0.5,
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"fixtures": [
|
"fixtures": [
|
||||||
{"name": "torch_fixture", "scale_x": 0.5, "scale_y": 0.5, "cell": "torch1", "x": 66, "y": -50},
|
{
|
||||||
{"name": "torch_fixture", "scale_x": -0.5, "scale_y": 0.5, "cell": "torch2", "x": 132, "y": -30}
|
"name": "torch_fixture",
|
||||||
|
"sprite": "torch_fixture",
|
||||||
|
"scale_x": 0.5,
|
||||||
|
"scale_y": 0.5,
|
||||||
|
"cell": "torch1",
|
||||||
|
"x": 66,
|
||||||
|
"y": -50
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "torch_fixture",
|
||||||
|
"sprite": "torch_fixture",
|
||||||
|
"scale_x": -0.5,
|
||||||
|
"scale_y": 0.5,
|
||||||
|
"cell": "torch2",
|
||||||
|
"x": 132,
|
||||||
|
"y": -30
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},
|
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},
|
||||||
|
|
|
||||||
91
scene.cpp
91
scene.cpp
|
|
@ -7,27 +7,39 @@ const bool YOU_REMOVED_MID_CELL_IDIOT=false;
|
||||||
namespace scene {
|
namespace scene {
|
||||||
using namespace guecs;
|
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) :
|
Engine::Engine(components::AnimatedScene& scene) :
|
||||||
$scene(scene)
|
$scene(scene)
|
||||||
{
|
{
|
||||||
for(auto& config : $scene.actors) {
|
for(auto& config : $scene.actors) {
|
||||||
std::string sprite_name = config["sprite"];
|
auto element = config_scene_element(config, false, false);
|
||||||
auto st = textures::get_sprite(sprite_name);
|
dbc::check(!$actor_name_ids.contains(element.name),
|
||||||
float scale_x = config["scale_x"];
|
fmt::format("actors key {} already exists", element.name));
|
||||||
float scale_y = config["scale_y"];
|
$actors.push_back(element);
|
||||||
|
$actor_name_ids.try_emplace(element.name, $actors.size() - 1);
|
||||||
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
|
// floor is std::optional
|
||||||
|
|
@ -36,23 +48,8 @@ namespace scene {
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& fixture : $scene.fixtures) {
|
for(auto& fixture : $scene.fixtures) {
|
||||||
std::string name = fixture["name"];
|
auto element = config_scene_element(fixture, true, true);
|
||||||
auto st = textures::get_sprite(name);
|
$fixtures.push_back(element);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,19 +87,6 @@ namespace scene {
|
||||||
return $ui.mouse(x, y, 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) {
|
void Engine::render(sf::RenderWindow& window) {
|
||||||
if($floor_sprite.sprite) {
|
if($floor_sprite.sprite) {
|
||||||
window.draw(*$floor_sprite.sprite);
|
window.draw(*$floor_sprite.sprite);
|
||||||
|
|
@ -121,7 +105,6 @@ namespace scene {
|
||||||
if(DEBUG) $ui.debug_layout(window);
|
if(DEBUG) $ui.debug_layout(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Engine::move_actor(const std::string& actor, const std::string& cell_name) {
|
void Engine::move_actor(const std::string& actor, const std::string& cell_name) {
|
||||||
auto& config = $actors.at($actor_name_ids.at(actor));
|
auto& config = $actors.at($actor_name_ids.at(actor));
|
||||||
config.cell = cell_name;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
scene.hpp
10
scene.hpp
|
|
@ -11,13 +11,14 @@ namespace scene {
|
||||||
using namespace textures;
|
using namespace textures;
|
||||||
|
|
||||||
struct Element {
|
struct Element {
|
||||||
|
std::string name;
|
||||||
textures::SpriteTexture st;
|
textures::SpriteTexture st;
|
||||||
components::Animation anim;
|
components::Animation anim;
|
||||||
std::string cell;
|
std::string cell;
|
||||||
float scale_x;
|
float scale_x = 1.0f;
|
||||||
float scale_y;
|
float scale_y = 1.0f;
|
||||||
float x;
|
float x = 0;
|
||||||
float y;
|
float y = 0;
|
||||||
bool at_mid=false;
|
bool at_mid=false;
|
||||||
sf::Vector2f pos{0,0};
|
sf::Vector2f pos{0,0};
|
||||||
};
|
};
|
||||||
|
|
@ -35,6 +36,7 @@ namespace scene {
|
||||||
void init();
|
void init();
|
||||||
void render(sf::RenderWindow& window);
|
void render(sf::RenderWindow& window);
|
||||||
bool mouse(float x, float y, guecs::Modifiers mods);
|
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);
|
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 move_actor(const std::string& actor, const std::string& cell_name);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue