-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
179 lines (158 loc) · 3.05 KB
/
Player.cpp
File metadata and controls
179 lines (158 loc) · 3.05 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "Player.h"
/****** private *******/
void Player::update()
{
oldPosition.x = player.getPosX();
oldPosition.y = player.getPosY();
}
/******* public *******/
Player::Player()
{
player = az::Image();
}
Player::~Player()
{
}
void Player::loadPlayer()
{
//load sprit img
player.loadImage("Ressources/Graphic/Player.png");
//cut image into several textures
player.TexturesLoadFromImage(3,4,20,27);
//create player sprite
player.SetTexture(1);
player.SetPosition(32,32);
}
void Player::movePlayer(sf::RenderWindow &win)
{
//move top
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
update();
player.move(0,-4*m_frametime);
changeTexture(0);
}
//move bottom
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
update();
player.move(0,4*m_frametime);
changeTexture(1);
}
//move left
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
update();
player.move(-4*m_frametime,0);
changeTexture(2);
}
//move right
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
update();
player.move(4*m_frametime,0);
changeTexture(3);
}
}
void Player::changeTexture(int direction)
{
if (mcl_animation.getElapsedTime().asSeconds() >= 0.3)
{
mcl_animation.restart();
}
if (mcl_animation.getElapsedTime().asSeconds() >= 0 && mcl_animation.getElapsedTime().asSeconds() <= 0.1)
{
if (direction == 0)
player.SetTexture(6);
else
if (direction == 1)
player.SetTexture(0);
else
if (direction == 2)
player.SetTexture(3);
else
if (direction == 3)
player.SetTexture(9);
}
if (mcl_animation.getElapsedTime().asSeconds() >= 0.1 && mcl_animation.getElapsedTime().asSeconds() <= 0.2)
{
if (direction == 0)
player.SetTexture(7);
if (direction == 1)
player.SetTexture(1);
else
if (direction == 2)
player.SetTexture(4);
else
if (direction == 3)
player.SetTexture(10);
}
if (mcl_animation.getElapsedTime().asSeconds() >= 0.2 && mcl_animation.getElapsedTime().asSeconds() <= 0.3)
{
if (direction == 0)
player.SetTexture(8);
if (direction == 1)
player.SetTexture(2);
else
if (direction == 2)
player.SetTexture(5);
else
if (direction == 3)
player.SetTexture(11);
}
}
void Player::SetOldPosition()
{
player.SetPosition(oldPosition.x,oldPosition.y);
}
void Player::moveTiles(float tx,float ty)
{
m_gotox = static_cast<int>(m_tileposx + tx);
m_gotoy = static_cast<int>(m_tileposy + ty);
//right
if(m_tileposx < tx)
{
update();
changeTexture(0);
}
//bottom
else if(m_tileposy < ty)
{
update();
changeTexture(1);
}
//left
else if(m_tileposx > tx)
{
update();
changeTexture(2);
}
//top
else if(m_tileposy > ty)
{
update();
changeTexture(3);
}
}
void Player::getFrameTime(float ft)
{
m_frametime = ft;
}
int Player::getTilePosX()
{
return m_tileposx;
}
int Player::getTilePosY()
{
return m_tileposy;
}
sf::Vector2f Player::getPosition()
{
actPosition.x = player.getPosX();
actPosition.y = player.getPosY();
return actPosition;
}
void Player::draw(sf::RenderTarget &target, sf::RenderStates states)const
{
target.draw(player);
}