-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
108 lines (91 loc) · 2.12 KB
/
Copy pathPlayer.cpp
File metadata and controls
108 lines (91 loc) · 2.12 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
#include "Player.h"
#include "Sprite.h"
#include <SFML/Window/Keyboard.hpp>
Player::Player()
: Entity("player-default"),
m_side(Side::Left), m_huggingWall(false)
{
}
void Player::reset()
{
m_side = Side::Left;
m_huggingWall = false;
m_position.x = Global::window.getSize().x / 4;
m_position.y = 0;
m_speed = { 0.f, 0.f };
m_sprite.setPosition(m_position);
m_isActive = true;
}
void Player::handleInput()
{
if (m_side == Side::Middle || !m_isActive) {
return;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if (m_speed.y == 0.f)
{
m_speed.y = -400.f;
}
else if (m_speed.y > -1600.f)
{
m_speed.y *= 1.0005f;
}
}
else
{
m_speed.y = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if (m_side == Side::Left) {
m_huggingWall = true;
}
else if (m_side == Side::Right) {
m_side = Side::Middle;
m_speed.x = -m_jumpSpeed;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if (m_side == Side::Right) {
m_huggingWall = true;
}
else if (m_side == Side::Left) {
m_side = Side::Middle;
m_speed.x = m_jumpSpeed;
}
}
}
if (m_huggingWall) {
m_sprite.setTexture("player-hugging", true);
}
else {
m_sprite.setTexture("player-default", true);
}
}
void Player::update(float elapsed)
{
if (!m_isActive) { return; }
m_position.x += m_speed.x * elapsed;
m_position.y += m_speed.y * elapsed; //update just the score instead
auto bounds = m_sprite.getGlobalBounds();
if (bounds.left < Global::window.getSize().x / 4.f)
{
m_position.x = Global::window.getSize().x / 4;
m_side = Side::Left;
m_speed.x = 0.f;
}
else if (bounds.left + bounds.width > Global::window.getSize().x * 3.f / 4.f) {
m_position.x = Global::window.getSize().x * 3 / 4 - bounds.width;
m_side = Side::Right;
m_speed.x = 0.f;
}
if (m_huggingWall && m_side == Side::Right) {
m_position.x = Global::window.getSize().x * 3 / 4 - bounds.width;
}
m_sprite.setPosition(m_position);
m_huggingWall = false;
}