-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
382 lines (318 loc) · 10.9 KB
/
Copy pathPlayer.cpp
File metadata and controls
382 lines (318 loc) · 10.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "Player.hpp"
#include "Enemy.hpp"
#include "Weapon.hpp"
#include "Object.hpp"
#include "FairyShoot.hpp"
#include "DungeonDoor.hpp"
#include "RockProjectile.hpp"
Player::Player(){
_sprite.setPosition(120,70); // Hardcoded
_dir = directions::down;
_action = linkActions::move;
_description = Resources::descriptions[linkSpritesDescriptions];
_hp = DataManager::getFloat("playerHp", 4.0f);
_maxHp = DataManager::getFloat("playerMaxHp", 4.0f);
_rupias = DataManager::getFloat("nRupies", 0);
_keys = DataManager::getFloat("nKeys", 0);
_bombs = DataManager::getFloat("nBombs", 0);
_elapsedAnimation = 0;
_currentAnimation = 0;
_moving = false;
_attacking = false;
_speaking = false;
_bounds = sf::IntRect(4,2,8,12);
_walkBounds = sf::IntRect(4,13,8,2);
std::vector<sf::Texture*> textures(9);
textures[directions::none] = &Resources::linkSet;
textures[directions::up] = &Resources::linkSetT;
textures[directions::left] = &Resources::linkSetL;
textures[directions::down] = &Resources::linkSetB;
textures[directions::right] = &Resources::linkSetR;
textures[directions::topLeft] = &Resources::linkSetTL;
textures[directions::botLeft] = &Resources::linkSetBL;
textures[directions::topRight] = &Resources::linkSetTR;
textures[directions::botRight] = &Resources::linkSetBR;
_lightSprite = LightSprite(_description, textures);
_dead = false;
_hitedTimer = 0;
_collisionMask = collisionMapMask::ground | collisionMapMask::passage;
}
Player::~Player(){}
void Player::update(float deltaTime) {
_hitedTimer -= deltaTime;
if(_moving) {
_elapsedAnimation -= deltaTime;
if (_elapsedAnimation < 0) {
_elapsedAnimation += ELAPSEDWALKING;
_currentAnimation = (_currentAnimation+1)%_description[_action*directionsQtty+_dir].size();
if((_currentAnimation % 2) == 0) SoundManager::playSound("stepGround");
}
}
if (_attacking) {
_elapsedAttack -= deltaTime;
if (_elapsedAttack <= 0) {
_action = linkActions::move;
_attacking = false;
}
}
else if (_moving) {
sf::Vector2f movement, speed(50,50), initial;
initial = _sprite.getPosition();
movement.x = getHorizontal(_dir) * speed.x;
movement.y = getVertical(_dir) * speed.y;
cmove(_map->getMaxMovement(initial,movement*deltaTime,_walkBounds,_collisionMask, true));
_moving = false;
}
_lightSprite.update(_sprite.getPosition(),_dir,_action,_currentAnimation);
}
void Player::draw(sf::RenderTarget* w) {
if (_attacking) {
_sword.draw(w);
}
if (_hitedTimer > 0) {
Resources::cInvert.setParameter("deltaTime", _hitedTimer);
_lightSprite.draw(w,&Resources::cInvert);
}
else {
_lightSprite.draw(w);
}
if(_speaking){
// TextBoxManager::setSize(w->getView().getSize().x/2, w->getView().getSize().y/4);
TextBoxManager::drawText(w,getPosition().x -TextBoxManager::getSize().x/2,
getPosition().y -TextBoxManager::getSize().y);
}
}
bool Player::isAlive() {
return !_dead;
}
sf::Vector2f Player::getBotPosition() {
return sf::Vector2f(_sprite.getPosition().x+_bounds.left+_bounds.width/2, _sprite.getPosition().y + 16);
}
float Player::getHp() {
return _hp;
}
void Player::setHp(float hp) {
_hp = hp;
}
float Player::getMaxHp() {
return _maxHp;
}
void Player::getHit(float much, sf::Vector2f) {
if (_hitedTimer > 0) return;
Resources::cInvert.setParameter("Time", 1.5);
_hitedTimer = 1.5; // One second of invulneravility;
_hp -= much;
_dead = _hp <= 0;
}
void Player::move(directions dir) {
if (_attacking) return;
_moving = true;
_dir = dir;
}
void Player::attack() {
if(!_attacking) {
SoundManager::playSound("stabSword");
int n = rand()%4;
if(n < 4)
SoundManager::playSound("linkAttack"+std::to_string(n));
_attacking = true;
_action = linkActions::attack;
_sword.init(_dir, _sprite.getPosition());
_sword.setPosition(sf::Vector2f( // V posar la posició del player
_sword.getPosition().x + ((10) * (_dir == directions::up ))
+ ((5) * (_dir == directions::down ))
+ ((11) * (_dir == directions::right))
+ (4 * (_dir == directions::left )),
_sword.getPosition().y + ((12) * (_dir == directions::right))
+ ((5) * (_dir == directions::left ))
+ ((11) * (_dir == directions::down ))
+ (4 * (_dir == directions::up ))
));
_elapsedAttack = ATTACKTIMERANIMATION;
}
}
sf::Vector2f Player::getPositionTransition() {
sf::Vector2f pos = _sprite.getPosition();
pos.x += _walkBounds.left + _walkBounds.width/2;
pos.y += _walkBounds.top + _walkBounds.height/2;
return pos;
}
directions Player::getDirection() {
return _dir;
}
sf::IntRect Player::getSwordRect() {
sf::Vector2f pos = _sword.getPosition();
sf::IntRect gSword = _sword.getGlobalBound();
sf::IntRect sword(pos.x - gSword.left, pos.y - gSword.top, gSword.width, gSword.height);
switch(_dir){
case directions::down:
return sf::IntRect(pos.x+sword.left, pos.y+sword.top, sword.width, sword.height);
case directions::up:
return sf::IntRect(pos.x-sword.left, pos.y-sword.top, -sword.width, -sword.height);
case directions::right:
return sf::IntRect(pos.x+sword.top, pos.y-sword.left, sword.height, -sword.width);
case directions::left:
return sf::IntRect(pos.x-sword.top, pos.y+sword.left, -sword.height, sword.width);
default:
return sf::IntRect(0,0,0,0); // This will never be called
}
}
sf::IntRect Player::getWalkBounds() {
return _walkBounds;
}
sf::IntRect Player::getGlobalWalkBounds() {
return sf::IntRect(_walkBounds.left+_sprite.getPosition().x, _walkBounds.top + _sprite.getPosition().y, _walkBounds.width, _walkBounds.height);
}
float Player::getSwordDamage() {
return 4; //SWORDDAMAGE sword.getDamage()
}
bool Player::isAttacking() {
return _attacking;
}
void Player::setPosition(sf::Vector2f pos) {
Collisionable::setPosition(pos);
_lightSprite.setPosition(pos);
}
void Player::resetMove() {
Collisionable::resetMove();
_lightSprite.setPosition(_pastPosition);
}
void Player::intersectsWith(Collisionable* c) {
Enemy* enemy = dynamic_cast<Enemy*>(c);
if (enemy != nullptr) {
getHit(enemy->getDamage(),sf::Vector2f(0,0));
return;
}
RockProjectile* rock = dynamic_cast<RockProjectile*>(c);
if (rock != nullptr) {
if (!counterDirection(getDirection(),rock->getDirection()) || isAttacking()) {
getHit(rock->getDamage(),sf::Vector2f(0,0));
}
return;
}
FairyShoot* fs = dynamic_cast<FairyShoot*>(c);
if (fs != nullptr) {
return;
}
Weapon* weapon = dynamic_cast<Weapon*>(c);
if (weapon != nullptr) {
getHit(weapon->getDamage(),sf::Vector2f(0,0));
return;
}
Prop* prop = dynamic_cast<Prop*>(c);
if (prop != nullptr) {
if (prop->getGid() <= 150) { // Bad practice!! every collisionable must change just himself
sf::IntRect movement;
if (getGlobalWalkBounds().intersects(prop->getGlobalBound(),movement)) {
sf::Vector2f mov(movement.width,movement.height);
if (getDirection() == directions::up) {
mov.x = 0;
mov.y *= -1;
}
else if (getDirection() == directions::left) {
mov.x *= -1;
mov.y = 0;
}
if (getDirection() == directions::down) {
mov.x = 0;
}
else if (getDirection() == directions::right) {
mov.y = 0;
}
prop->cmove(mov);
}
}
else resetMove();
return;
}
DungeonDoor* door = dynamic_cast<DungeonDoor*>(c);
if (door != nullptr) {
if (door->needKey() && _keys > 0) {
--_keys;
door->openWithKey(); // Easiest than making this at DungeonDoor... don't blame me...
}
if (!door->isOpened()) resetMove();
return;
}
Object* object = dynamic_cast<Object*>(c);
if (object != nullptr) {
objectType oType = object->getType();
switch (oType) {
case objectType::fullHeal:
_hp = (_hp + 0.5 <= _maxHp ? _hp + 0.5 : _hp);
// Without break. Trying to add 0.5 two times instead of 1 one time
case objectType::halfHeal:
_hp = (_hp + 0.5 <= _maxHp ? _hp + 0.5 : _hp);
break;
case objectType::life:
_maxHp += 1;
_hp += 1;
break;
case objectType::rupee:
++_rupias;
break;
case objectType::rupee5:
_rupias += 5;
break;
case objectType::bomb:
++_bombs;
break;
case objectType::key:
++_keys;
break;
default:
break;
}
return;
}
}
void Player::setLight(Light* light) {
_lightSprite.setLight(light);
_sword.setLight(light);
}
void Player::setMap(Map* map) {
_map = map;
}
void Player::setDirection(directions dir) {
_dir = dir;
}
bool Player::speaking() const{
return _speaking;
}
void Player::setSpeaking(bool speaking){
_speaking = speaking;
if(speaking) TextBoxManager::setSize(50,25);
if(speaking) TextBoxManager::setText("default", 8);
if(speaking) TextBoxManager::setTexture(1);
}
void Player::setSpeaking(bool speaking, std::string name){
_speaking = speaking;
if(speaking) TextBoxManager::setSize(50,25);
if(speaking) TextBoxManager::setText(name, 8);
}
int Player::rupias() const {
return _rupias;
}
void Player::setRupias(int rupias) {
_rupias = rupias;
}
int Player::bombs() const {
return _bombs;
}
void Player::setBombs(int bombs){
_bombs = bombs;
}
int Player::keys() const{
return _keys;
}
void Player::setKeys(int keys){
_keys = keys;
}
void Player::save() {
DataManager::setVector2f("playerPos", getPosition()-_map->getSceneCoord());
DataManager::setFloat("playerHp", _hp);
DataManager::setFloat("playerMaxHp", _maxHp);
DataManager::setFloat("nRupies", _rupias);
DataManager::setFloat("nKeys", _keys);
DataManager::setFloat("nBombs", _bombs);
}