-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
206 lines (182 loc) · 4.12 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
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
#include <cassert>
#include <cmath>
#include <iostream>
#include "entities.hpp"
#include "util.hpp"
using namespace sf;
using namespace std;
const float MIN_SPEED = 40;
const float MAX_SPEED = 200;
const float ACCELERATION = 50;
const float SMALL_JUMP = 8;
const float BIG_JUMP = 10;
const float JUMP_SPEED = 15;
const float FALL_SPEED = 10;
const float CRASH_STOP_TIME = 2;
static sf::Texture frames[6];
void Player::preload()
{
frames[0] = loadTexture("images/sprite_1.png");
frames[1] = loadTexture("images/sprite_2.png");
frames[2] = loadTexture("images/sprite_3.png");
frames[3] = loadTexture("images/sprite_4.png");
frames[4] = loadTexture("images/sprite_5.png");
frames[5] = loadTexture("images/sprite_6.png");
}
Player::Player()
{
sprite.setTexture(frames[3]);
sprite.setOrigin(16, 16);
speed = 0;
downTime = 0;
jumping = false;
crash = false;
travelAmount = 0;
}
void Player::goUp()
{
if (jumping || crash) return;
if (state.down) state.down = false;
else {
if (!state.up) state.up = true;
else state.left = state.right = false;
}
}
void Player::goDown()
{
if (jumping || crash) return;
if (state.up) state.up = false;
else {
if (!state.down) state.down = true;
else state.left = state.right = false;
}
}
void Player::goLeft()
{
if (jumping || crash) return;
if (state.right) state.right = false;
else {
if (!state.left) state.left = true;
else state.up = state.down = false;
}
}
void Player::goRight()
{
if (jumping || crash) return;
if (state.left) state.left = false;
else {
if (!state.right) state.right = true;
else state.up = state.down = false;
}
}
void Player::update(float deltaTime)
{
downTime += deltaTime;
if (!state.down || crash) {
// stopped going downhill
if (state.up) downTime = 0;
else {
// slow down
downTime = fmin(MAX_SPEED, downTime - downTime * 2 * deltaTime);
}
}
if (jumping) {
if (!fall) {
height += deltaTime * JUMP_SPEED;
if (!isBigJump) {
if (height >= SMALL_JUMP) {
fall = true;
}
} else {
if (height >= BIG_JUMP) {
fall = true;
}
}
} else {
height -= deltaTime * FALL_SPEED;
if (height <= 0) {
height = 0;
jumping = false;
}
}
}
// generate direction vector
Vector2f dir(0, 0);
if (state.up ^ state.down) {
if (state.up) dir.y = -1;
if (state.down) dir.y = 1;
}
if (state.left ^ state.right) {
if (state.left) dir.x = -1;
if (state.right) dir.x = 1;
}
// set the correct texture
int id = state.up << 3 | state.down << 2 | state.left << 1 | state.right;
float xScale = 1;
switch (id) {
case 10: sprite.setTexture(frames[2], true); xScale = -1; break;
case 9: sprite.setTexture(frames[2], true); xScale = 1; break;
case 8: sprite.setTexture(frames[0], true); xScale = 1; break;
case 6: sprite.setTexture(frames[4], true); xScale = -1; break;
case 5: sprite.setTexture(frames[4], true); xScale = 1; break;
case 4: sprite.setTexture(frames[1], true); xScale = 1; break;
case 2: sprite.setTexture(frames[3], true); xScale = -1; break;
case 1: sprite.setTexture(frames[3], true); xScale = 1; break;
case 0: break;
default: cerr << "invalid player state"; assert(0);
}
float speed = min(MAX_SPEED, MIN_SPEED + downTime * ACCELERATION);
if (crash) {
crashTime += deltaTime;
if (crashTime > CRASH_STOP_TIME) {
crash = false;
} else {
sprite.setTexture(frames[5], true);
speed = 0;
}
}
Vector2f velocity = normalize(dir) * speed;
Vector2f movement = velocity * deltaTime;
travelAmount += movement.y;
sprite.move(movement);
this->speed = magnitude(velocity);
// zoom when jumping
sprite.setScale(xScale * (1 + height * 0.1), 1 + height * 0.1);
}
void Player::collide(Actor &actor)
{
// not used
}
void Player::jump()
{
if (!jumping && state.down) {
jumping = true;
isBigJump = false;
fall = false;
height = 0;
}
}
void Player::bigJump()
{
jump();
isBigJump = true;
}
void Player::knock()
{
if (!jumping && !crash && downTime > 3) {
crash = true;
crashTime = 0;
downTime = 0;
state.up = state.down = state.left = state.right = false;
}
}
void Player::knockHard()
{
// todo
knock();
}
void Player::slowDown()
{
if (jumping) return;
downTime = 0;
}