Super awesome twitchy animation for axe guy.

master
Zed A. Shaw 2 weeks ago
parent 80a0f2ba75
commit 83df9ff03b
  1. 21
      animator.cpp
  2. 25
      animator.hpp
  3. BIN
      assets/axe_ranger-256.png
  4. 13
      assets/enemies.json
  5. 27
      components.cpp
  6. 15
      components.hpp
  7. 1
      meson.build
  8. 5
      raycaster.cpp
  9. 1
      raycaster.hpp
  10. 2
      systems.cpp

@ -1,21 +0,0 @@
#include "animator.hpp"
#include "constants.hpp"
#include <SFML/Audio.hpp>
#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();
}

@ -1,25 +0,0 @@
#pragma once
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Audio.hpp>
#include <string>
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);
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 107 KiB

@ -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"}
]
}

@ -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<Sprite>(component_map);
components::enroll<Animation>(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;
}
}
}

@ -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);

@ -52,7 +52,6 @@ endif
sources = [
'animator.cpp',
'ansi_parser.cpp',
'camera.cpp',
'combat.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<components::Animation>(rec.second)) {
auto& animation = $level.world->get<components::Animation>(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

@ -2,7 +2,6 @@
#include <SFML/Graphics.hpp>
#include <SFML/System/Clock.hpp>
#include "animator.hpp"
#include "spatialmap.hpp"
#include "levelmanager.hpp"
#include "textures.hpp"

@ -164,7 +164,7 @@ void System::combat(GameLevel &level) {
if(world.has<Animation>(entity)) {
auto& animation = world.get<Animation>(entity);
animation.current = 0;
animation.play();
}
world.send<Events::GUI>(Events::GUI::COMBAT, entity, result);

Loading…
Cancel
Save