generated from pimoroni/picosystem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
323 lines (280 loc) · 6.45 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "Player.h"
#include "Game.h"
#include "FixedMath.h"
#include "Projectile.h"
#include "Platform.h"
#include "Draw.h"
#include "Enemy.h"
#include "Map.h"
#include "Sounds.h"
#include "Particle.h"
#define USE_ROTATE_BOB 0
#define STRAFE_TILT 14
#define ROTATE_TILT 3
const char SignMessage1[] PROGMEM = "Abandon hope ye who enter!";
void Player::Init()
{
NextLevel();
hp = maxHP;
}
void Player::NextLevel()
{
x = CELL_SIZE * 1 + CELL_SIZE / 2;
y = CELL_SIZE * 1 + CELL_SIZE / 2;
angle = FIXED_ANGLE_45;
mana = maxMana;
damageTime = 0;
shakeTime = 0;
reloadTime = 0;
velocityX = 0;
velocityY = 0;
angularVelocity = 0;
}
void Player::Fire()
{
if (mana >= manaFireCost)
{
reloadTime = 8;
shakeTime = 6;
int16_t projectileX = x + FixedCos(angle + FIXED_ANGLE_90 / 2) / 4;
int16_t projectileY = y + FixedSin(angle + FIXED_ANGLE_90 / 2) / 4;
ProjectileManager::FireProjectile(this, projectileX, projectileY, angle);
mana -= manaFireCost;
Platform::PlaySound(Sounds::Attack);
}
}
void Player::Tick()
{
uint8_t input = Platform::GetInput();
int8_t turnDelta = 0;
int8_t targetTilt = 0;
int8_t moveDelta = 0;
int8_t strafeDelta = 0;
if (input & INPUT_A)
{
if (input & INPUT_LEFT)
{
strafeDelta--;
}
if (input & INPUT_RIGHT)
{
strafeDelta++;
}
}
else
{
if (input & INPUT_LEFT)
{
turnDelta -= TURN_SPEED * 2;
}
if (input & INPUT_RIGHT)
{
turnDelta += TURN_SPEED * 2;
}
}
// Testing shooting / recoil mechanic
if (reloadTime > 0)
{
reloadTime--;
}
else if (input & INPUT_B)
{
Fire();
}
if (angularVelocity < turnDelta)
{
angularVelocity++;
}
else if (angularVelocity > turnDelta)
{
angularVelocity--;
}
angle += angularVelocity >> 1;
if (input & INPUT_UP)
{
moveDelta++;
}
if (input & INPUT_DOWN)
{
moveDelta--;
}
static int tiltTimer = 0;
tiltTimer++;
if (moveDelta && USE_ROTATE_BOB)
{
targetTilt = (int8_t)(FixedSin(tiltTimer * 10) / 32);
}
else
{
targetTilt = 0;
}
targetTilt += angularVelocity * ROTATE_TILT;
targetTilt += strafeDelta * STRAFE_TILT;
int8_t targetBob = moveDelta || strafeDelta ? FixedSin(tiltTimer * 10) / 128 : 0;
if (shakeTime > 0)
{
shakeTime--;
targetBob += (Random() & 3) - 1;
targetTilt += (Random() & 31) - 16;
}
constexpr int tiltRate = 6;
if (Renderer::camera.tilt < targetTilt)
{
Renderer::camera.tilt += tiltRate;
if (Renderer::camera.tilt > targetTilt)
{
Renderer::camera.tilt = targetTilt;
}
}
else if (Renderer::camera.tilt > targetTilt)
{
Renderer::camera.tilt -= tiltRate;
if (Renderer::camera.tilt < targetTilt)
{
Renderer::camera.tilt = targetTilt;
}
}
constexpr int bobRate = 3;
if (Renderer::camera.bob < targetBob)
{
Renderer::camera.bob += bobRate;
if (Renderer::camera.bob > targetBob)
{
Renderer::camera.bob = targetBob;
}
}
else if (Renderer::camera.bob > targetBob)
{
Renderer::camera.bob -= bobRate;
if (Renderer::camera.bob < targetBob)
{
Renderer::camera.bob = targetBob;
}
}
int16_t cosAngle = FixedCos(angle);
int16_t sinAngle = FixedSin(angle);
int16_t cos90Angle = FixedCos(angle + FIXED_ANGLE_90);
int16_t sin90Angle = FixedSin(angle + FIXED_ANGLE_90);
//camera.x += (moveDelta * cosAngle) >> 4;
//camera.y += (moveDelta * sinAngle) >> 4;
velocityX += (moveDelta * cosAngle) / 24;
velocityY += (moveDelta * sinAngle) / 24;
velocityX += (strafeDelta * cos90Angle) / 24;
velocityY += (strafeDelta * sin90Angle) / 24;
Move(velocityX / 4, velocityY / 4);
velocityX = (velocityX * 7) / 8;
velocityY = (velocityY * 7) / 8;
if (mana < maxMana && reloadTime == 0)
{
mana += manaRechargeRate;
}
if (damageTime > 0)
damageTime--;
uint8_t cellX = x / CELL_SIZE;
uint8_t cellY = y / CELL_SIZE;
switch (Map::GetCellSafe(cellX, cellY))
{
case CellType::Potion:
if (hp < maxHP)
{
if (hp + potionStrength > maxHP)
hp = maxHP;
else
hp += potionStrength;
Map::SetCell(cellX, cellY, CellType::Empty);
Platform::PlaySound(Sounds::Pickup);
Game::ShowMessage(PSTR("Drank a potion of healing"));
}
break;
case CellType::Coins:
Map::SetCell(cellX, cellY, CellType::Empty);
Platform::PlaySound(Sounds::Pickup);
Game::ShowMessage(PSTR("Found some gold coins"));
Game::stats.coinsCollected++;
break;
case CellType::Crown:
Map::SetCell(cellX, cellY, CellType::Empty);
Platform::PlaySound(Sounds::Pickup);
Game::ShowMessage(PSTR("Found a jewel encrusted crown"));
Game::stats.crownsCollected++;
break;
case CellType::Scroll:
Map::SetCell(cellX, cellY, CellType::Empty);
Platform::PlaySound(Sounds::Pickup);
Game::ShowMessage(PSTR("Found an ancient scroll"));
Game::stats.scrollsCollected++;
break;
}
}
bool Player::IsWorldColliding() const
{
return Map::IsBlockedAtWorldPosition(x - collisionSize, y - collisionSize)
|| Map::IsBlockedAtWorldPosition(x + collisionSize, y - collisionSize)
|| Map::IsBlockedAtWorldPosition(x + collisionSize, y + collisionSize)
|| Map::IsBlockedAtWorldPosition(x - collisionSize, y + collisionSize);
}
bool Player::CheckCollisions()
{
int16_t lookAheadX = (x + (FixedCos(angle) * lookAheadDistance) / FIXED_ONE);
int16_t lookAheadY = (y + (FixedSin(angle) * lookAheadDistance) / FIXED_ONE);
uint8_t lookAheadCellX = (uint8_t)(lookAheadX / CELL_SIZE);
uint8_t lookAheadCellY = (uint8_t)(lookAheadY / CELL_SIZE);
const uint8_t chestCoinsColour = 0xe0;
CellType lookAheadCell = Map::GetCellSafe(lookAheadCellX, lookAheadCellY);
switch (lookAheadCell)
{
case CellType::Chest:
Map::SetCell(lookAheadCellX, lookAheadCellY, CellType::ChestOpened);
ParticleSystemManager::CreateExplosion(lookAheadX, lookAheadY, chestCoinsColour, false);
Platform::PlaySound(Sounds::Pickup);
Game::ShowMessage(PSTR("Found a chest full of gold!"));
Game::stats.chestsOpened++;
break;
case CellType::Sign:
Game::ShowMessage(SignMessage1);
break;
}
if (IsWorldColliding())
{
return true;
}
if (EnemyManager::GetOverlappingEnemy(*this))
{
return true;
}
return false;
}
void Player::Move(int16_t deltaX, int16_t deltaY)
{
x += deltaX;
y += deltaY;
if (CheckCollisions())
{
y -= deltaY;
if (CheckCollisions())
{
x -= deltaX;
y += deltaY;
if (CheckCollisions())
{
y -= deltaY;
}
}
}
}
void Player::Damage(uint8_t damageAmount)
{
if(shakeTime < 6)
shakeTime = 6;
damageTime = 10;
if (hp <= damageAmount)
{
Platform::PlaySound(Sounds::PlayerDeath);
hp = 0;
}
else
{
Platform::PlaySound(Sounds::Ouch);
hp -= damageAmount;
}
}