-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
116 lines (91 loc) · 2.64 KB
/
Copy pathplayer.cpp
File metadata and controls
116 lines (91 loc) · 2.64 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
#include "player.hpp"
Player::Player(std::string path, int fw, int fh) : sprite(texture), animation(fw, fh){
move_speed = 48;
max_speed = 100;
height = fh;
width = fw;
drag = 5;
facing = sf::Vector2f({ 0.f, 1.f });
fov = sf::degrees(120);
if(!texture.loadFromFile(path)) throw "could not load player";
invulnerable = false;
animation.add_state("idle", 0, 4, 4);
animation.add_state("walk_right", 1, 8, 8);
animation.add_state("walk_left", 3, 8, 8);
animation.add_state("win", 12, 8, 3.9);
animation.add_state("die", 15, 8, 3.9);
// add state name, row, number of frames, frame per sec
lives_remaining = 3;
collision_box = sf::FloatRect({ 0, 0 }, { 10, 8 });
collision_box.position += sf::Vector2f({ fw / 2.f - 5.0f, fh / 2.f });
}
void Player::update(sf::Time dt) {
animation.next_frame(dt);
sprite.setTextureRect(animation.get_frame_rect());
if (!lives_remaining) return;
//alive functions
if (velocity.length() > 0) {
animation.set_state(velocity.x > 0 ? "walk_right" : "walk_left");
} else animation.set_state("idle");
move_offset(velocity * move_speed * dt.asSeconds());
velocity -= velocity*drag*dt.asSeconds();
if (velocity.length() < 0.1) velocity = { 0,0 };
}
void Player::move_offset(sf::Vector2f offset) {
position += offset;
collision_box.position += offset;
sprite.move(offset);
}
void Player::set_position(sf::Vector2f pos) {
sf::Vector2f offset = pos - position;
move_offset(offset);
}
void Player::set_center(sf::Vector2f pos) {
set_position(pos - sf::Vector2f({width/2.f, height/2.f}));
}
void Player::set_angle_vector(sf::Vector2f angle) {
facing = angle.normalized();
}
sf::Vector2f Player::get_center() {
return position + sf::Vector2f({ width / 2.f, height / 2.f });
}
void Player::move(sf::Keyboard::Scan key) {
switch (key)
{
case sf::Keyboard::Scan::Right:
velocity.x = 1;
break;
case sf::Keyboard::Scan::Left:
velocity.x = -1;
break;
case sf::Keyboard::Scan::Up:
velocity.y = -1;
break;
case sf::Keyboard::Scan::Down:
velocity.y = 1;
break;
}
velocity = velocity.normalized();
}
bool Player::check_collision(sf::Vector2f pos) {
return ((collision_box.position.x < pos.x) && (pos.x < collision_box.position.x + collision_box.size.x) &&
(collision_box.position.y < pos.y) && (pos.y < collision_box.position.y + collision_box.size.y));
}
void Player::rotate(sf::Angle angle) {
facing = facing.rotatedBy(angle);
}
void Player::hurt() {
invulnerable = true;
lives_remaining--;
}
void Player::stop_invulnerability() {
invulnerable = false;
}
void Player::die() {
animation.set_state("die");
animation.set_frame(0);
}
void Player::win() {
animation.set_state("win");
animation.set_frame(0);
}