diff --git a/animator.cpp b/animator.cpp deleted file mode 100644 index 4b3d40f..0000000 --- a/animator.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "animator.hpp" -#include "constants.hpp" -#include -#include "dbc.hpp" - -void Animator::step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h) { - if(playing) { - count++; - frame = ((count / 4) % max_frames); - playing = frame != 0; - } - - sprite.setTextureRect(sf::IntRect({ - {rect_x + frame * width, rect_y}, - {rect_w, rect_h}})); -} - -void Animator::play(bool sound_too) { - playing = true; - if(sound_too) sound.play(); -} diff --git a/animator.hpp b/animator.hpp deleted file mode 100644 index 96380ca..0000000 --- a/animator.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include -#include - -struct Animator { - int width = 0; - int height = 0; - int max_frames = 0; - sf::SoundBuffer buffer; - sf::Sound sound; - size_t count = 0; - int frame = 0; - bool playing = false; - - Animator(int w, int h, int max, std::string sound_file) : - width(w), height(h), max_frames(max), - buffer(sound_file), - sound(buffer) - { - } - - void step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h); - void play(bool sound_too=true); -}; diff --git a/assets/axe_ranger-256.png b/assets/axe_ranger-256.png index e184452..e5387f3 100644 Binary files a/assets/axe_ranger-256.png and b/assets/axe_ranger-256.png differ diff --git a/assets/enemies.json b/assets/enemies.json index d5275fe..9238eae 100644 --- a/assets/enemies.json +++ b/assets/enemies.json @@ -10,7 +10,7 @@ {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "LightSource", "strength": 60, "radius": 1.8}, {"_type": "EnemyConfig", "hearing_distance": 5}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10} + {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10, "speed": 1.0} ] }, "KNIGHT": { @@ -22,7 +22,7 @@ {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "EnemyConfig", "hearing_distance": 5}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10}, + {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10, "speed": 0.3}, {"_type": "Sprite", "name": "armored_knight"} ] }, @@ -32,12 +32,11 @@ "foreground": [156, 172, 197], "background": [30, 20, 75] }, - {"_type": "LightSource", "strength": 60, "radius": 1.8}, {"_type": "Combat", "hp": 40, "max_hp": 40, "damage": 10, "dead": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": true}, {"_type": "EnemyConfig", "hearing_distance": 5}, {"_type": "Sprite", "name": "axe_ranger"}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10} + {"_type": "Animation", "scale": 0.2, "simple": false, "frames": 10, "speed": 0.6} ] }, "EVIL_EYE": { @@ -50,7 +49,7 @@ {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "EnemyConfig", "hearing_distance": 5}, {"_type": "Sprite", "name": "evil_eye"}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10} + {"_type": "Animation", "scale": 0.2, "simple": false, "frames": 10, "speed": 0.3} ] }, "RAT_GIANT": { @@ -62,7 +61,7 @@ {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "EnemyConfig", "hearing_distance": 10}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10}, + {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10, "speed": 1.0}, {"_type": "Sprite", "name": "rat_with_sword"} ] }, @@ -75,7 +74,7 @@ {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "EnemyConfig", "hearing_distance": 10}, - {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10}, + {"_type": "Animation", "scale": 0.2, "simple": true, "frames": 10, "speed": 1.0}, {"_type": "Sprite", "name": "hairy_spider"} ] } diff --git a/components.cpp b/components.cpp index ef64806..446f8fb 100644 --- a/components.cpp +++ b/components.cpp @@ -12,7 +12,7 @@ namespace components { ENROLL_COMPONENT(LightSource, strength, radius); ENROLL_COMPONENT(Device, config, events); ENROLL_COMPONENT(Sprite, name); - ENROLL_COMPONENT(Animation, scale, simple, frames); + ENROLL_COMPONENT(Animation, scale, simple, frames, speed); void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data) { for (auto &i : data) { @@ -36,4 +36,29 @@ namespace components { components::enroll(component_map); components::enroll(component_map); } + + void Animation::play() { + if(!playing) { + current = 0; + playing = true; + } + } + + void Animation::step(sf::Vector2f& scale_out, sf::IntRect& rect_out) { + if(playing && current < frames) { + if(simple) { + scale_out.x += scale; + scale_out.y += scale; + } else { + rect_out.position.x += current * TEXTURE_WIDTH; + } + + subframe += speed; + current = int(subframe); + } else { + playing = false; + current = 0; + subframe = 0.0f; + } + } } diff --git a/components.hpp b/components.hpp index e68667d..f93cb53 100644 --- a/components.hpp +++ b/components.hpp @@ -1,6 +1,7 @@ #pragma once #include "dinkyecs.hpp" #include "components.hpp" +#include "constants.hpp" #include "config.hpp" #include "dinky_components.hpp" #include "point.hpp" @@ -89,17 +90,13 @@ namespace components { float scale = 0.0f; bool simple = true; int frames = 10; + float speed = 0.3f; int current = 0; + bool playing = false; + float subframe = 0; - void step(sf::Vector2f& scale_out, sf::IntRect& rect_out) { - if(current < frames) { - scale_out.x += scale; - scale_out.y += scale; - current++; - } - - (void) rect_out; - } + void play(); + void step(sf::Vector2f& scale_out, sf::IntRect& rect_out); }; void configure(ComponentMap& component_map); diff --git a/meson.build b/meson.build index 639ec22..9733cbb 100644 --- a/meson.build +++ b/meson.build @@ -52,7 +52,6 @@ endif sources = [ - 'animator.cpp', 'ansi_parser.cpp', 'camera.cpp', 'combat.cpp', diff --git a/raycaster.cpp b/raycaster.cpp index fa5a2f9..5213ea8 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -147,16 +147,17 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { int tex_y = ((d * texture_height) / sprite_height) / texture_height; sf::Vector2f scale{sprite_scale_w, sprite_scale_h}; + sf::Vector2f position{x, y}; sf::IntRect in_texture{ {tex_x, tex_y}, {tex_render_width, texture_height}}; if($level.world->has(rec.second)) { auto& animation = $level.world->get(rec.second); - animation.step(scale, in_texture); + if(animation.playing) animation.step(scale, in_texture); } sf_sprite->setScale(scale); sf_sprite->setTextureRect(in_texture); - sf_sprite->setPosition({x, y}); + sf_sprite->setPosition(position); $brightness.setUniform("offsetFactor", sf::Glsl::Vec2{0.0f, 0.0f}); // the SpatialMap.distance_sorted only calculates the diff --git a/raycaster.hpp b/raycaster.hpp index b10c9fd..304e815 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -2,7 +2,6 @@ #include #include -#include "animator.hpp" #include "spatialmap.hpp" #include "levelmanager.hpp" #include "textures.hpp" diff --git a/systems.cpp b/systems.cpp index 1ee23e5..12c0cf8 100644 --- a/systems.cpp +++ b/systems.cpp @@ -164,7 +164,7 @@ void System::combat(GameLevel &level) { if(world.has(entity)) { auto& animation = world.get(entity); - animation.current = 0; + animation.play(); } world.send(Events::GUI::COMBAT, entity, result);