-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
84 lines (78 loc) · 2.13 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
#include "entity.h"
Player::Player(SquareMap* square_map, TextureGroup player_texture_group, sf::Vector2i pos)
{
m_square_map = square_map;
m_animation = Animation(player_texture_group, true, 5);
m_square_pos = sf::Vector2<double>(pos.x, pos.y);
m_inventory = Inventory();
update_sprite();
m_counter = Utils::TimeCounter(1000);
m_counter.start();
// Stats
m_hp = 400;
m_max_hp = 400;
}
void Player::update(void)
{
m_animation.update();
update_sprite();
}
void Player::nonticked_update(void)
{
// Movement
float speed = (float)(m_counter.progress() * PLAYER_SPEED);
m_direction.x = 0;
m_direction.y = 0;
sf::Vector2f old_pos;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
m_direction.x = -1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
m_direction.x += 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
m_direction.y = -1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
m_direction.y += 1;
if (m_direction.x != 0)
m_square_pos.x += (double)speed * (m_direction.x > 0 ? 1 : -1);
if (m_direction.y != 0)
m_square_pos.y += (double)speed * (m_direction.y > 0 ? 1 : -1);
// Rotate
if (m_direction.x == 0 && m_direction.y == -1)
m_sprite.setRotation(0);
if (m_direction.x == 1 && m_direction.y == -1)
m_sprite.setRotation(45);
if (m_direction.x == 1 && m_direction.y == 0)
m_sprite.setRotation(90);
if (m_direction.x == 1 && m_direction.y == 1)
m_sprite.setRotation(135);
if (m_direction.x == 0 && m_direction.y == 1)
m_sprite.setRotation(180);
if (m_direction.x == -1 && m_direction.y == 1)
m_sprite.setRotation(225);
if (m_direction.x == -1 && m_direction.y == 0)
m_sprite.setRotation(270);
if (m_direction.x == -1 && m_direction.y == -1)
m_sprite.setRotation(315);
// Anim
if (m_direction.x == 0 && m_direction.y == 0)
m_animation.stop(2);
else if (!m_animation.is_running())
m_animation.run(3);
// Update
collision_update();
update_sprite();
// Counter
m_counter.start();
}
void Player::give_items(ItemStack items)
{
m_inventory.add_items(items);
}
Inventory* Player::get_inventory(void)
{
return &m_inventory;
}
void Player::draw(sf::RenderWindow& window) const
{
window.draw(m_sprite);
}