/
Player.cpp
99 lines (84 loc) · 2.55 KB
/
Player.cpp
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
#include <iostream>
#include "Player.hpp"
Player::Player(std::string name, int amplada, int altura, sf::Vector2f posIni){
positionInicial = posIni;
SPRITE_WIDTH = amplada;
SPRITE_HEIGHT = altura;
xcont = 0;
wins = 0;
position = posIni;
spriteSource = sf::Vector2i(0,0);
spriteSize = sf::Vector2i(SPRITE_WIDTH, SPRITE_HEIGHT);
sprite.setOrigin(0, SPRITE_HEIGHT);
if(!texture.loadFromFile(name)){
std::cout << "Filed at loading " << name << std::endl;
}
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
sprite.setPosition(position);
}
void Player::move(Dir mov, float dTime){
if (mov != None) {
if(mov-1 == spriteSource.y) {
position.x += Mx[mov]*SPEED;
position.y += My[mov]*SPEED;
if(mov != None)++xcont;
if(xcont >= maxCont) {
++spriteSource.x;
if(spriteSource.x >= 6) spriteSource.x = 0;
xcont = 0;
}
}
else spriteSource.y = mov-1;
sprite.setPosition(position);
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
}
}
void Player::incwins(){
++wins;
}
int Player::getwins(){
return wins;
}
void Player::setPosition(float x, float y){
position.x = x;
position.y = y;
sprite.setPosition(position);
}
void Player::setSpriteSource(int x, int y){
spriteSource.x = x;
spriteSource.y = y;
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
}
void Player::setSpriteSize(int x, int y){
spriteSize.x = x;
spriteSize.y = y;
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
}
sf::Vector2f Player::getPosition(){
return position;
}
sf::Vector2i Player::getSpriteSource(){
return spriteSource;
}
sf::Vector2i Player::getSpriteSize(){
return spriteSize;
}
void Player::incSpriteSourceX(){
++spriteSource.x;
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
}
void Player::setSpriteSourceY(int y){
spriteSource.y = y;
sprite.setTextureRect(sf::IntRect(spriteSource.x*SPRITE_WIDTH,
spriteSource.y*SPRITE_HEIGHT, spriteSize.x, spriteSize.y));
}
void Player::tornaInici(){
position = positionInicial;
sprite.setPosition(position);
}