AnimatedScene now defines how a scene with animated sprites and actors is structured and played.

This commit is contained in:
Zed A. Shaw 2025-10-22 00:11:36 -04:00
parent e1da089600
commit 25f7096489
10 changed files with 43 additions and 48 deletions

View file

@ -72,4 +72,4 @@ money:
scc --exclude-dir subprojects
arena:
./builddir/arena
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args ./builddir/arena

View file

@ -134,9 +134,9 @@
"max_scale": 0.9,
"simple": true,
"frames": 1,
"speed": 1.0,
"speed": 0.5,
"stationary": false,
"toggled": false,
"toggled": true,
"looped": false
},
"hairy_spider": {

View file

@ -1,21 +1,23 @@
{
"RAT_KING": {
"components": [
{"_type": "BossFight",
{"_type": "AnimatedScene",
"background": "test_background",
"floor": false,
"floor_pos": "floor1",
"player": {
"sprite": "peasant_girl_rear_view",
"start_pos": "player2",
"scale": 0.5,
"mid_cell": false
},
"boss": {
"start_pos": "boss5",
"scale": 0.6,
"mid_cell": true,
"sprite": "rat_king_boss"
"actors": {
"player": {
"sprite": "peasant_girl_rear_view",
"start_pos": "player2",
"scale": 0.5,
"mid_cell": false
},
"boss": {
"sprite": "rat_king_boss",
"start_pos": "boss5",
"scale": 0.6,
"mid_cell": true
}
},
"fixtures": [
{"name": "torch_fixture", "scale_x": 0.5, "scale_y": 0.5, "cell": "torch1", "x": 66, "y": -50},

View file

@ -84,7 +84,7 @@ namespace boss {
break;
case ATTACK: {
$ui.status(L"PLAYER TURN");
$ui.move_player(run % 10 < 5 ? "player1" : "player2");
$ui.move_actor($ui.$player_sprite, "player", run % 10 < 5 ? "player1" : "player2");
int attack_id = std::any_cast<int>(data);
boss::System::combat(attack_id);
state(State::PLAYER_TURN);
@ -105,7 +105,7 @@ namespace boss {
break;
case ATTACK: {
$ui.status(L"BOSS TURN");
$ui.move_boss(run % 10 < 5 ? "boss5" : "boss6");
$ui.$boss_pos = $ui.move_actor($ui.$boss_sprite, "boss", run % 10 < 5 ? "boss5" : "boss6");
$ui.$boss_anim.play();
int attack_id = std::any_cast<int>(data);
boss::System::combat(attack_id);

View file

@ -14,10 +14,10 @@ namespace boss {
UI::UI(shared_ptr<World> world, Entity boss_id) :
$world(world),
$boss_id(boss_id),
$scene(world->get<components::BossFight>($boss_id)),
$scene(world->get<components::AnimatedScene>($boss_id)),
$combat_ui(true)
{
std::string sprite_name = $scene.boss["sprite"];
std::string sprite_name = $scene.actors["boss"]["sprite"];
$boss_sprite = textures::get_sprite(sprite_name);
// floor is std::optional
@ -25,7 +25,7 @@ namespace boss {
$floor_sprite = textures::get_sprite(*$scene.floor);
}
$player_sprite = textures::get_sprite($scene.player["sprite"]);
$player_sprite = textures::get_sprite($scene.actors["player"]["sprite"]);
dbc::check(animation::has(sprite_name), "add boss animation to animations.json");
$boss_anim = animation::load(sprite_name);
@ -66,8 +66,8 @@ namespace boss {
"[floor4|player5|player6|player7|player8|_]"
);
move_boss($scene.boss["start_pos"]);
move_player($scene.player["start_pos"]);
$boss_pos = move_actor($boss_sprite, "boss", $scene.actors["boss"]["start_pos"]);
move_actor($player_sprite, "player", $scene.actors["player"]["start_pos"]);
if($scene.floor) {
position_sprite($floor_sprite, $scene.floor_pos, 1.0f, 1.0f, false);
@ -100,13 +100,16 @@ namespace boss {
$combat_ui.init(cell.x, cell.y, cell.w, cell.h);
}
void UI::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) {
sf::Vector2f UI::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 = $arena.cell_for(cell_name);
float x = float(at_mid ? cell.mid_x : cell.x);
float y = float(at_mid ? cell.mid_y : cell.y);
st.sprite->setPosition({x + x_diff, y + y_diff});
sf::Vector2f pos{x + x_diff, y + y_diff};
st.sprite->setPosition(pos);
st.sprite->setScale({scale_x, scale_y});
return pos;
}
void UI::render(sf::RenderWindow& window) {
@ -139,17 +142,9 @@ namespace boss {
$arena.show_text("status", msg);
}
void UI::move_boss(const std::string& cell_name) {
float scale = $scene.boss["scale"];
position_sprite($boss_sprite, cell_name, scale, scale, $scene.boss["mid_cell"]);
auto& cell = $arena.cell_for(cell_name);
$boss_pos = {float(cell.mid_x), float(cell.mid_y)};
}
void UI::move_player(const std::string& cell_name) {
float scale = $scene.player["scale"];
position_sprite($player_sprite, cell_name, scale, scale, $scene.player["mid_cell"]);
sf::Vector2f UI::move_actor(textures::SpriteTexture& st, const std::string& actor, const std::string& cell_name) {
float scale = $scene.actors[actor]["scale"];
return position_sprite(st, cell_name, scale, scale, $scene.actors[actor]["mid_cell"]);
}
void UI::play_animations() {

View file

@ -27,7 +27,7 @@ namespace boss {
struct UI {
shared_ptr<World> $world = nullptr;
Entity $boss_id = NONE;
components::BossFight& $scene;
components::AnimatedScene $scene;
gui::CombatUI $combat_ui;
SpriteTexture $boss_sprite;
SpriteTexture $player_sprite;
@ -44,10 +44,9 @@ namespace boss {
void init();
void render(sf::RenderWindow& window);
bool mouse(float x, float y, guecs::Modifiers mods);
void 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 status(const std::wstring& msg);
void move_boss(const std::string& cell_name);
void move_player(const std::string& cell_name);
sf::Vector2f move_actor(SpriteTexture& st, const std::string& actor, const std::string& cell_name);
void play_animations();
};
}

View file

@ -17,7 +17,7 @@ namespace components {
void init() {
if(!MAP_configured) {
components::enroll<BossFight>(MAP);
components::enroll<AnimatedScene>(MAP);
components::enroll<Combat>(MAP);
components::enroll<Position>(MAP);
components::enroll<Curative>(MAP);

View file

@ -81,12 +81,11 @@ namespace components {
float scale;
};
struct BossFight {
struct AnimatedScene {
std::string background;
std::optional<std::string> floor;
std::string floor_pos;
json player;
json boss;
json actors;
json fixtures;
};
@ -152,7 +151,7 @@ namespace components {
using ComponentMap = std::unordered_map<std::string, ReflFuncSignature>;
ENROLL_COMPONENT(Tile, display, foreground, background);
ENROLL_COMPONENT(BossFight, background, floor, floor_pos, player, boss, fixtures);
ENROLL_COMPONENT(AnimatedScene, background, floor, floor_pos, actors, fixtures);
ENROLL_COMPONENT(Sprite, name, scale);
ENROLL_COMPONENT(Curative, hp);
ENROLL_COMPONENT(LightSource, strength, radius);

View file

@ -35,8 +35,8 @@ TEST_CASE("make sure json_mods works", "[components]") {
// this confirms that loading something with an optional
// field works with the json conversions in json_mods.hpp
for(auto& comp_data : config["RAT_KING"]["components"]) {
if(comp_data["_type"] == "BossFight") {
auto comp = components::convert<components::BossFight>(comp_data);
if(comp_data["_type"] == "AnimatedScene") {
auto comp = components::convert<components::AnimatedScene>(comp_data);
// the boss fight for the rat king doesn't have a stage so false=optional
REQUIRE(comp.floor == std::nullopt);
}
@ -50,6 +50,6 @@ TEST_CASE("make sure json_mods works", "[components]") {
components::configure_entity(world, rat_king, config["RAT_KING"]["components"]);
auto boss = world.get<BossFight>(rat_king);
auto boss = world.get<AnimatedScene>(rat_king);
REQUIRE(boss.floor == std::nullopt);
}

View file

@ -36,7 +36,7 @@ int main(int, char*[]) {
gui::routing::Router router;
sound::mute(false);
sound::mute(true);
sound::play("ambient_1", true);
GameDB::create_level();