-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.h
82 lines (60 loc) · 1.61 KB
/
Player.h
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
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "ResourceDispatcher.h"
class Animation {
public :
Animation(int x, int y, int width, int height,int nFrames, sf::Texture texture) {
_texture = texture ;
_nFrames = nFrames;
for (int i = 0; i < nFrames; i++) {
frames.push_back({ x + i * width, y,x + width , height });
// frames[i] = { x + i * width, y,x + width , height };
}
}
void ApplyToSprite(sf::Sprite& s) const {
s.setTexture(_texture);
s.setTextureRect(frames[_iFrame]);
}
void Update(float dt) {
time += dt;
while (time >= _holdTime) {
time -= _holdTime;
Advance();
}
}
private :
void Advance() {
if (++_iFrame >= _nFrames) {
_iFrame = 0;
}
}
int _nFrames ;
int _iFrame = 0;
const float _holdTime = 0.15;
sf::Texture _texture;
std::vector<sf::IntRect> frames;
float time = 0.0f;
};
class Player : public sf::Drawable
{
public:
Player(const Player&) = delete;
Player& operator=(const Player&) = delete;
Player() ;
void processEvents(float sec);
void update(sf::Time deltaTime);
void moveLeft(float mvt);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
void animatePlayer();
sf::Sprite _character;
sf::Vector2f _velocity;
sf::Vector2f _position;
Animation _animateRight;
Animation _animateIdle;
Animation _animateLeft;
bool _is_moving;
};
#endif