A retro platform game
git clone git://soophie.de/freezo
blob: 224571e24c17c961f536129b78c3a5668b0a8ab3 (14747 bytes)[doc][blame][raw]
1
#include <stdlib.h>2
#include <math.h>4
#include "../include/player.h"5
#include "../include/const.h"6
#include "../include/tile.h"7
#include "../include/util.h"8
#include "../include/entity.h"9
#include "../include/effect.h"11
player_t *player_create(pos_t pos) {12
player_t *player = malloc(sizeof(player_t));13
*player = (player_t) {14
.pos = pos,15
.health = 6,16
.on_ground = false,17
.velocity = 0.0,18
.gravity = 0.0,19
.dir = 1,20
.sneaking = false,21
.shooting = false,22
.damage_timer = 0,23
.fall_height = 0.0,24
.held_enemy = NULL,25
.moving = false,26
.timer_walking = fz_timer_create(9, 4),27
.timer_sneaking = fz_timer_create(9, 8),28
.target_entity = NULL,29
.shooting_distance = PLAYER_SHOOTING_RANGE,30
};31
return player;32
}34
void player_update(player_t *player, game_t *game) {35
float move = 0.0;36
bool go_down = false;37
bool sneaking = false;38
bool holding = false;39
player->shooting = false;40
player->moving = false;41
tile_t *left_ground_tile = tile_get(game, player->pos.x + 0.2 * TILE_WIDTH, player->pos.y + PLAYER_HEIGHT + 1);42
tile_t *right_ground_tile = tile_get(game, player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH, player->pos.y + PLAYER_HEIGHT + 1);43
// game over44
if (player->health <= 0) {45
game->defeat = true;46
}47
if (IsKeyDown(KEY_A) && !IsKeyDown(KEY_D)) {48
move = -3.0;49
player->dir = -1;50
player->moving = true;51
}52
if (IsKeyDown(KEY_D) && !IsKeyDown(KEY_A)) {53
move = 3.0;54
player->dir = 1;55
player->moving = true;56
}57
if (IsKeyDown(KEY_S)) {58
sneaking = true;59
}60
if (IsKeyDown(KEY_S) && IsKeyDown(KEY_SPACE)) {61
if (62
(left_ground_tile == NULL || !tile_solid(left_ground_tile)) &&63
(right_ground_tile == NULL || !tile_solid(right_ground_tile))64
) {65
go_down = true;66
}67
}68
if (IsKeyPressed(KEY_SPACE)) {69
if (player->on_ground && !player->sneaking && !go_down) {70
player->velocity = 13.0;71
}72
}73
if (IsKeyDown(KEY_ENTER)) {74
player->shooting = true;75
}76
if (IsKeyPressed(KEY_W)) {77
holding = true;78
}79
// detect on ground80
bool on_ground = false;81
if (!(player->velocity > 0.0)) {82
if (!go_down) {83
for (int i = 0; i < game->tiles_len; i++) {84
tile_t *tile = game->tiles[i];85
if (tile_ground(tile)) {86
if (87
player->pos.x + PLAYER_WIDTH > tile->pos.x &&88
player->pos.x < tile->pos.x + TILE_WIDTH89
) {90
float tolerance = 4.0;91
if (player->gravity > tolerance) {92
tolerance = player->gravity;93
}94
if (fabs(player->pos.y + PLAYER_HEIGHT - tile->pos.y) < tolerance) {95
player->pos.y = tile->pos.y - PLAYER_HEIGHT;96
on_ground = true;97
if (player->fall_height >= 1.5 * TILE_HEIGHT) {98
effect_play(player->pos, EFFECT_FALL, game);99
}100
// fall damage101
if (player->fall_height >= 6 * TILE_HEIGHT) {102
if (player->damage_timer == 0) {103
if (player->health > 0) {104
player->health--;105
player->damage_timer = 80;106
player->velocity = 6.0;107
if (player->held_enemy != NULL) {108
player->held_enemy = NULL;109
}110
}111
}112
}113
}114
}115
}116
}117
}118
for (int i = 0; i < game->entities_len; i++) {119
if (game->entities[i].type == ENTITY_ENEMY) {120
enemy_t *enemy = game->entities[i].enemy;121
if (enemy->frozen) {122
if (123
player->pos.x + PLAYER_WIDTH > enemy->pos.x &&124
player->pos.x < enemy->pos.x + ENEMY_WIDTH125
) {126
if (fabs(player->pos.y + PLAYER_HEIGHT - enemy->pos.y) < 4.0) {127
player->pos.y = enemy->pos.y - PLAYER_HEIGHT;128
on_ground = true;129
if (player->sneaking && player->fall_height >= 2 * TILE_HEIGHT) {130
effect_play(enemy->pos, EFFECT_BREAK, game);131
effect_play(enemy->pos, EFFECT_PARTICLE, game);132
enemy_kill(enemy, game);133
i--;134
player->velocity = 6.0;135
}136
}137
}138
}139
}140
}141
}142
// detect ceiling143
if (player->velocity > 0.0) {144
for (int i = 0; i < game->tiles_len; i++) {145
tile_t *tile = game->tiles[i];146
if (tile_solid(tile)) {147
if (148
player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH > tile->pos.x &&149
player->pos.x + 0.2 * TILE_WIDTH < tile->pos.x + TILE_WIDTH150
) {151
float tolerance = 4.0;152
if (player->velocity > tolerance) {153
tolerance = player->velocity;154
}155
if (fabs(player->pos.y - (tile->pos.y + 0.2 * TILE_HEIGHT)) < tolerance) {156
player->pos.y = tile->pos.y + 0.2 * TILE_HEIGHT;157
player->velocity = 0.0;158
}159
}160
}161
}162
}163
// detect wall164
if (fabs(move) > 0.0) {165
for (int i = 0; i < game->tiles_len; i++) {166
tile_t *tile = game->tiles[i];167
if (tile_wall(tile)) {168
if (169
player->pos.y + PLAYER_HEIGHT > tile->pos.y &&170
player->pos.y < tile->pos.y + TILE_HEIGHT171
) {172
float tolerance = 4.0;173
if (move > tolerance) {174
tolerance = player->velocity;175
}176
if (player->dir > 0) {177
if (fabs((player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH) - tile->pos.x) < tolerance) {178
player->pos.x = tile->pos.x - (PLAYER_WIDTH - 0.2 * TILE_WIDTH);179
}180
}181
else {182
if (fabs((player->pos.x + 0.2 * TILE_WIDTH) - (tile->pos.x + TILE_WIDTH)) < tolerance) {183
player->pos.x = tile->pos.x + TILE_WIDTH - 0.2 * TILE_WIDTH;184
}185
}186
}187
}188
}189
}190
player->on_ground = on_ground;191
player->sneaking = sneaking;192
// handle move193
if (player->sneaking || player->shooting) {194
player->pos.x += 0.5 * move;195
}196
else {197
player->pos.x += move;198
}199
// handle map border200
if (player->pos.x < 0.0) {201
player->pos.x = 0.0;202
}203
if (player->pos.x + PLAYER_WIDTH > TILE_WIDTH * game->level->width) {204
player->pos.x = TILE_WIDTH * game->level->width - PLAYER_WIDTH;205
}206
// handle jump207
if (player->velocity > 0.0) {208
player->pos.y -= player->velocity;209
player->velocity -= 1.1;210
}211
if (player->on_ground) {212
player->gravity = 0.0;213
player->fall_height = 0.0;214
}215
else {216
player->pos.y += player->gravity;217
player->fall_height += player->gravity;218
if (player->gravity < PLAYER_GRAVITY) {219
player->gravity += 0.2;220
}221
else {222
player->gravity = PLAYER_GRAVITY;223
}224
}225
if (player->damage_timer > 0) {226
player->damage_timer--;227
}228
// detect shooting229
if (player->shooting) {230
// determine target231
float shooting_range = 2 * TILE_WIDTH;232
rect_t shooting_rect = {233
.x = player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x - shooting_range,234
.y = player->sneaking ? player->pos.y + 8 * SCALE : player->pos.y + 7 * SCALE,235
.width = shooting_range,236
.height = SCALE,237
};238
float closest_distance = shooting_range;239
entity_t *closest_entity = NULL;240
bool target_in_range = false;241
for (int i = 0; i < game->entities_len; i++) {242
entity_t *entity = &game->entities[i];243
// ignore held enemies244
if (entity->type == ENTITY_ENEMY && player->held_enemy == entity->enemy) {245
continue;246
}247
// ignore frozen gates248
if (entity->type == ENTITY_GATE && entity->gate->frozen) {249
continue;250
}251
// ignore non-bad entities252
if (!entity->is_bad) {253
continue;254
}255
rect_t entity_rect = entity_get_rect(entity);256
if (rect_collide(shooting_rect, entity_rect)) {257
float entity_x = entity_rect.x + entity_rect.width / 2.0;258
float entity_distance = fabs(entity_x - (player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x));259
if (player->target_entity != NULL && player->target_entity == entity) {260
target_in_range = true;261
// if target entity set distance to that262
player->shooting_distance = entity_distance;263
}264
if (entity_distance < closest_distance) {265
closest_distance = entity_distance;266
closest_entity = entity;267
}268
}269
}270
if (closest_entity != NULL) {271
if (!target_in_range) {272
// if no target entity use closest distance273
player->shooting_distance = closest_distance;274
player->target_entity = closest_entity;275
}276
}277
else {278
player->target_entity = NULL;279
player->shooting_distance = PLAYER_SHOOTING_RANGE;280
}281
// detect shooting wall/solid282
for (int i = 0; i < game->tiles_len; i++) {283
tile_t *tile = game->tiles[i];284
if (tile_wall(tile) || tile_solid(tile)) {285
rect_t tile_rect = { tile->pos.x, tile->pos.y, TILE_WIDTH, TILE_HEIGHT };286
if (rect_collide(shooting_rect, tile_rect)) {287
if (player->dir > 0) {288
player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH);289
// clear target if it stands behind a wall290
if (player->target_entity != NULL) {291
rect_t rect = entity_get_rect(player->target_entity);292
if (rect.x > tile->pos.x) {293
player->target_entity = NULL;294
}295
}296
}297
else {298
player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH);299
// clear target if it stands behind a wall300
if (player->target_entity != NULL) {301
rect_t rect = entity_get_rect(player->target_entity);302
if (rect.x + rect.width < tile->pos.x + TILE_WIDTH) {303
player->target_entity = NULL;304
}305
}306
}307
}308
}309
}310
}311
else {312
player->target_entity = NULL;313
player->shooting_distance = PLAYER_SHOOTING_RANGE;314
}315
// detect holding316
if (holding) {317
bool picked_up = false;318
for (int i = 0; i < game->entities_len; i++) {319
if (game->entities[i].type == ENTITY_ENEMY) {320
enemy_t *enemy = game->entities[i].enemy;321
if (enemy->frozen) {322
float tolerance = 0.0;323
if (324
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&325
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&326
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&327
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance328
) {329
if (player->held_enemy == NULL) {330
player->held_enemy = enemy;331
picked_up = true;332
}333
}334
}335
}336
}337
if (!picked_up && player->held_enemy != NULL) {338
player->held_enemy = NULL;339
}340
}341
if (player->held_enemy != NULL) {342
player->held_enemy->pos = (pos_t) {343
.x = player->pos.x,344
.y = player->pos.y - (player->sneaking ? 0.3 : 0.5) * TILE_HEIGHT,345
};346
}347
// detect door entering348
if (holding) {349
for (int i = 0; i < game->entities_len; i++) {350
if (game->entities[i].type == ENTITY_DOOR) {351
door_t *door = game->entities[i].door;352
float tolerance = 0.0;353
if (354
player->pos.x <= door->pos.x + ENEMY_WIDTH - tolerance &&355
player->pos.x + PLAYER_WIDTH >= door->pos.x + tolerance &&356
player->pos.y <= door->pos.y + ENEMY_HEIGHT - tolerance &&357
player->pos.y + PLAYER_HEIGHT >= door->pos.y + tolerance358
) {359
if (door_unlock(door, game)) {360
game->door = door;361
game->victory = true;362
}363
else {364
PlaySound(game->assets.locked);365
}366
}367
}368
}369
}370
// detect damage371
for (int i = 0; i < game->entities_len; i++) {372
if (game->entities[i].type == ENTITY_ENEMY) {373
enemy_t *enemy = game->entities[i].enemy;374
if (!enemy->frozen) {375
float tolerance = TILE_WIDTH / 5.0;376
if (377
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&378
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&379
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&380
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance381
) {382
if (player->damage_timer == 0) {383
if (player->health > 0) {384
player->health--;385
player->damage_timer = 80;386
player->velocity = 8.0;387
if (player->pos.x + PLAYER_WIDTH / 2.0 > enemy->pos.x + ENEMY_WIDTH / 2.0) {388
player->pos.x += 20.0;389
}390
else {391
player->pos.x -= 20.0;392
}393
if (player->held_enemy != NULL) {394
player->held_enemy = NULL;395
}396
}397
}398
}399
}400
}401
}402
// fall out of map403
if (player->pos.y > TILE_HEIGHT * game->level->height) {404
if (player->health > 0) {405
player->health = 0;406
}407
}408
// update timer409
if (player->on_ground && player->moving) {410
fz_timer_update(player->timer_walking);411
fz_timer_update(player->timer_sneaking);412
}413
else {414
fz_timer_reset(player->timer_walking);415
fz_timer_reset(player->timer_sneaking);416
}417
}419
void player_draw(player_t *player, game_t *game) {420
pos_t pos = pos_snap(player->pos);421
int frame = (player->sneaking || player->shooting)422
? fz_timer_get(player->timer_sneaking)423
: fz_timer_get(player->timer_walking);424
if (player->damage_timer / 4 % 2 == 0) {425
if (player->sneaking) {426
if (player->dir > 0) {427
DrawTextureRec(game->assets.entities, texture_rect(frame, 14, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);428
}429
else {430
DrawTextureRec(game->assets.entities, texture_rect(frame, 15, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);431
}432
}433
else {434
if (player->dir > 0) {435
DrawTextureRec(game->assets.entities, texture_rect(frame, 12, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);436
}437
else {438
DrawTextureRec(game->assets.entities, texture_rect(frame, 13, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);439
}440
}441
if (player->shooting) {442
float y = player->sneaking ? pos.y + 8 * SCALE : pos.y + 7 * SCALE;443
if (player->dir > 0) {444
DrawRectangle(pos.x + PLAYER_WIDTH, y, player->shooting_distance, 1 * SCALE, BLUE);445
}446
else {447
DrawRectangle(pos.x - player->shooting_distance, y, player->shooting_distance, 1 * SCALE, BLUE);448
}449
}450
}451
}453
void player_free(player_t *player) {454
fz_timer_free(player->timer_walking);455
fz_timer_free(player->timer_sneaking);456
free(player);457
}