-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
198 lines (170 loc) · 3.84 KB
/
Player.cpp
File metadata and controls
198 lines (170 loc) · 3.84 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
#include "Scene.h"
#include "Map.h"
#include "Window.h"
#include "SharedState.h"
#include "Player.h"
#include <QGuiApplication>
Player::Player(Map *map, Scene *scene, Window *window)
: Actor("resources/player.spb", scene),
m_xVelocity(200),
m_yVelocity(100),
m_xThrust(0),
m_yThrust(1),
m_lastDirection(0),
m_x(0),
m_y(0),
m_isOnGround(false),
m_map(map)
// m_stepSoundTimer(0)
{
const QPoint &p = map->spawnPoint();
m_window = window;
m_x = p.x();
m_y = p.y();
// m_jumpSound.setSource(QUrl::fromLocalFile("resources/sound/jump.wav"));
// m_jumpSound.setVolume(0.5);
// m_stepSound.setSource(QUrl::fromLocalFile("resources/sound/step.wav"));
}
float Player::x() const
{
return m_x;
}
float Player::y() const
{
return m_y;
}
void Player::tick(const long delta)
{
float x = m_x + (m_xThrust * m_xVelocity) * (delta / 1000.0f);
float y = m_y + (m_yThrust * m_yVelocity) * (delta / 1000.0f);
const QSGTexture *t = texture();
const QSize playerSize = t->textureSize();
const QPointF playerCenter(playerSize.width() / 2, playerSize.height() / 2);
const QPointF cameraPosition(m_x + playerCenter.x(), m_y + playerCenter.y());
const QRectF playerBoundingBox(x, y, playerSize.width(), playerSize.height());
const QRect &goal = m_map->goal();
if(m_isOnGround)
{
if(m_xThrust)
m_sprite.setImageIndex(Walking);
else
m_sprite.setImageIndex(Idle);
}
else
{
if(m_yThrust < 0)
m_sprite.setImageIndex(Jumping);
else
m_sprite.setImageIndex(Falling);
}
m_sprite.update(delta);
m_scene->setCameraPosition(cameraPosition, m_map);
m_isOnGround = false;
for(const Collidable &collidable : m_map->collidables())
{
const QPolygonF &polygon = collidable.polygon();
const QRectF &boundingBox = collidable.boundingBox();
if(boundingBox.intersects(playerBoundingBox))
{
// Check bottom collision
for(int i = m_x + 6; i < m_x + playerSize.width() - 6; i += 2)
{
while(polygon.containsPoint(QPointF(i, y + playerSize.height() - 2), Qt::OddEvenFill))
{
y--;
if(m_yVelocity > 0)
m_yVelocity = 0;
}
if(!m_isOnGround && polygon.containsPoint(QPointF(i, y + playerSize.height() - 1), Qt::OddEvenFill))
{
m_isOnGround = true;
}
}
// Check left collision
for(int i = y + 6; i < y + playerSize.height() - 6; i += 2)
{
while(polygon.containsPoint(QPointF(x + 4, i), Qt::OddEvenFill))
{
x++;
}
}
// Check right collision
for(int i = y + 6; i < y + playerSize.height() - 6; i += 2)
{
while(polygon.containsPoint(QPointF(x + playerSize.width() - 4, i), Qt::OddEvenFill))
{
x--;
}
}
// Check top collision
for(int i = x + 6; i < x + playerSize.width() - 6; i += 2)
{
while(polygon.containsPoint(QPointF(i, y + 1), Qt::OddEvenFill))
{
y++;
m_yVelocity = 100;
}
}
}
}
// if(m_isOnGround && m_x != x)
// {
// m_stepSoundTimer += delta;
// if(m_stepSoundTimer > 250)
// {
// m_stepSound.play();
// m_stepSoundTimer = 0;
// }
// }
if(!m_isOnGround && m_yThrust > 0)
{
m_yVelocity += delta / 3.0f;
if(m_yVelocity > 1000)
m_yVelocity = 1000;
}
if(m_yThrust < 0 && m_yVelocity > 100)
{
m_yVelocity -= delta / 2.0f;
}
else
{
m_yThrust = 1;
}
bool reachedGoal = goal.contains(x, y);
if(reachedGoal)
{
m_window->setActiveState("GoalState");
}
m_x = x;
m_y = y;
}
void Player::setVelocity(const float velocity)
{
m_xVelocity = velocity;
}
void Player::setDirection(const float direction)
{
m_xThrust = direction;
if(m_xThrust != 0)
{
m_flipped = m_xThrust < 0 || (!m_xThrust && m_xThrust < 0);
m_lastDirection = m_xThrust;
}
else
{
m_sprite.setImageIndex(Idle);
}
}
void Player::jump()
{
if(!m_isOnGround)
return;
// m_jumpSound.play();
m_yThrust = -1;
m_yVelocity = 323;
}
void Player::respawn()
{
m_x = m_map->spawnPoint().x();
m_y = m_map->spawnPoint().y();
}