summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 45c2b3a4141220d1cde4f29d38a3de967391b230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<SFML/Graphics.hpp>
#include<vector>
#include<cstdio>
#include<cmath>

#include "graphics/animation.h"
#include "entity.h"
#include "resourceManager.h"
#include "resourceIds.h"

namespace gph = Graphics;


class Unit: Entity{
    private:
        sf::Texture spritesheet_;
    public:

        gph::Animation ne_shooting_animation;
        gph::Animation se_shooting_animation;
        gph::Animation& current_animation = ne_shooting_animation;
        sf::Sprite sprite;

        sf::Vector2f position;
        sf::Vector2f size;
        sf::Vector2f speed;


        Unit(TextureManager& textures){
            ne_shooting_animation = gph::Animation(textures.get(Textures::Id::Player_NE_Shooting), 6, sf::milliseconds(100 * 6), false);
            se_shooting_animation = gph::Animation(textures.get(Textures::Id::Player_SE_Shooting), 6, sf::milliseconds(100 * 6), false);
            current_animation = ne_shooting_animation;

            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(sf::Time dt){
            current_animation.update( dt);
        }
};


int main()
{
    sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game");

    sf::Event event;
    sf::Clock clock;

    TextureManager textureManager;
    textureManager.load(Textures::Id::Player_NE_Shooting, "assets/img/Player/angle2-shooting.png");
    textureManager.load(Textures::Id::Player_SE_Shooting, "assets/img/Player/angle1-shooting.png");

    Unit unit = Unit(textureManager);

    renderWindow.setFramerateLimit(60);

    sf::Time elapsed_time = sf::Time::Zero;
    bool once = true;

    while (renderWindow.isOpen()){
        sf::Time dt = clock.getElapsedTime();
        elapsed_time += dt;
        clock.restart();
        while (renderWindow.pollEvent(event)){
            if (event.type == sf::Event::EventType::Closed)
                renderWindow.close();
        }

        if(elapsed_time > sf::milliseconds(4000) && once){
            unit.current_animation.reset();
            unit.current_animation = unit.se_shooting_animation;
            once = false;
        }

        unit.update(dt);

        renderWindow.clear();
        renderWindow.draw(unit.current_animation);
        renderWindow.display();
    }
}