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
94
95
96
97
98
99
100
101
102
|
#include<SFML/Graphics.hpp>
#include<vector>
#include<cstdio>
#include<cmath>
#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<int> 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();
}
}
|