#include #include #include #include #include "graphics/animation.h" #include "entity.h" class Unit: Entity{ private: sf::Texture spritesheet_; public: enum class Anim : int{ standing = 0, walking = 1, shooting = 2, complaining = 3, dying = 4, }; std::vector animation_frame_count { 6, 8, 6, 1, 14 }; Graphics::Animation walking_animation; Graphics::Animation standing_animation; Graphics::OneShotAnimation shooting_animation; Graphics::OneShotAnimation complaining_animation; Graphics::OneShotAnimation dying_animation; Graphics::Animation* current_animation = &standing_animation; sf::Sprite sprite; sf::Vector2f position; sf::Vector2f size; sf::Vector2f speed; Unit(){ spritesheet_.loadFromFile("assets/img/Player/Player Angle 1 Sheet.png"); standing_animation = Graphics::Animation(spritesheet_, 6, 60, 44, 48, 0); walking_animation = Graphics::Animation(spritesheet_, 8, 60, 44, 48, 1); shooting_animation = Graphics::OneShotAnimation(spritesheet_, 6, 60, 44, 48, 2); complaining_animation = Graphics::OneShotAnimation(spritesheet_, 1, 60, 44, 48, 3); dying_animation = Graphics::OneShotAnimation(spritesheet_, 14, 60, 44, 48, 4); position = sf::Vector2f{0,0}; speed = sf::Vector2f{0,0}; }; void walk(float vx, float vy){ speed.y = vy; speed.x = vx; current_animation->reset(); current_animation = &walking_animation; } void shoot(){ current_animation->reset(); current_animation = &shooting_animation; } ~Unit(){}; void update(int dt){ //if (current_animation->finished()){ // current_animation->reset(); // current_animation = &standing_animation; //} position.x += round(speed.x * dt); sprite.setTexture(spritesheet_); sprite.setScale(8,8); sprite.setTextureRect(current_animation->next(dt)); sprite.setPosition(position.x, position.y); } }; int main() { sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game"); sf::Event event; sf::Clock clock; Unit unit; renderWindow.setFramerateLimit(60); long elapsed_time = 0; bool once = true; while (renderWindow.isOpen()){ int dt = clock.getElapsedTime().asMilliseconds(); elapsed_time += dt; clock.restart(); while (renderWindow.pollEvent(event)){ if (event.type == sf::Event::EventType::Closed) renderWindow.close(); } if(elapsed_time > 1000 && once){ unit.shoot(); once = false; } unit.update(dt); renderWindow.clear(); renderWindow.draw(unit.sprite); renderWindow.display(); } }