diff --git a/animator.cpp b/animator.cpp new file mode 100644 index 0000000..bf97fba --- /dev/null +++ b/animator.cpp @@ -0,0 +1,2 @@ +#include "animator.hpp" +#include "constants.hpp" diff --git a/animator.hpp b/animator.hpp new file mode 100644 index 0000000..b1c9810 --- /dev/null +++ b/animator.hpp @@ -0,0 +1,27 @@ +#pragma once +#include + +struct Animator { + int width = 0; + int height = 0; + int max_frames = 0; + size_t count = 0; + int frame = 0; + bool playing = false; + + inline void 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}})); + } + + inline void play() { + playing = true; + } +}; diff --git a/assets/config.json b/assets/config.json index a84df36..de2ddf1 100644 --- a/assets/config.json +++ b/assets/config.json @@ -9,7 +9,7 @@ "armored_knight": "assets/armored_knight_1-256.png", "sword": "assets/cinqueda_1-512.png", "barrel": "assets/wood_barrel_large-256.png", - "evil_eye": "assets/evil_eye_test-256.png", + "evil_eye": "assets/evil_eye-sprites.png", "peasant_girl": "assets/undead_peasant-256.png", "floor": "assets/floor_tile_test-256.png", "ceiling": "assets/ceiling_test-256.png" diff --git a/assets/evil_eye-sprites.png b/assets/evil_eye-sprites.png new file mode 100644 index 0000000..4a38728 Binary files /dev/null and b/assets/evil_eye-sprites.png differ diff --git a/main.cpp b/main.cpp index af28dda..07070b4 100644 --- a/main.cpp +++ b/main.cpp @@ -96,6 +96,7 @@ int main() { } if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + rayview.$anim.play(); rotation = -30.0f; } else { rotation = -10.0f; diff --git a/raycaster.cpp b/raycaster.cpp index c4b9602..4c1dd30 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -150,8 +150,9 @@ void Raycaster::sprite_casting() { int texY = ((d * textureHeight) / spriteHeight) / textureHeight; sf_sprite->setScale({sprite_w, sprite_h}); - sf_sprite->setTextureRect(sf::IntRect({texX, texY}, {texX_end - texX, textureHeight})); + $anim.step(*sf_sprite, texX, texY, texX_end - texX, textureHeight); sf_sprite->setPosition({x, y}); + $window.draw(*sf_sprite); } } diff --git a/raycaster.hpp b/raycaster.hpp index f1d4e64..c4bf766 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -12,6 +12,7 @@ #include #include "texture.hpp" #include +#include "animator.hpp" using matrix::Matrix; using RGBA = uint32_t; @@ -43,6 +44,7 @@ struct Raycaster { std::vector spriteOrder; std::vector spriteDistance; std::vector ZBuffer; // width + Animator $anim{256, 256, 10, 0}; Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height);