A retro platform game
git clone git://soophie.de/freezo
blob: 08b38f2c6cb32fffdad72426b122df40e256bfe4 (12899 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
rect_t entity_rect;252
switch (entity->type) {253
case ENTITY_ENEMY:254
entity_rect = rect_from_pos(entity->enemy->pos, ENEMY_WIDTH, ENEMY_HEIGHT);255
break;256
case ENTITY_GATE:257
entity_rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);258
break;259
}260
if (rect_collide(shooting_rect, entity_rect)) {261
float entity_x = entity_rect.x + entity_rect.width / 2.0;262
float entity_distance = fabs(entity_x - (player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x));263
if (player->target_entity != NULL && player->target_entity == entity) {264
target_in_range = true;265
}266
if (entity_distance < closest_distance) {267
closest_distance = entity_distance;268
closest_entity = entity;269
}270
}271
}272
if (closest_entity != NULL) {273
player->shooting_distance = closest_distance;274
if (!target_in_range) {275
player->target_entity = closest_entity;276
}277
}278
else {279
player->target_entity = NULL;280
player->shooting_distance = PLAYER_SHOOTING_RANGE;281
}282
}283
else {284
player->target_entity = NULL;285
player->shooting_distance = PLAYER_SHOOTING_RANGE;286
}287
// detect holding288
if (holding) {289
bool picked_up = false;290
for (int i = 0; i < game->entities_len; i++) {291
if (game->entities[i].type == ENTITY_ENEMY) {292
enemy_t *enemy = game->entities[i].enemy;293
if (enemy->frozen) {294
float tolerance = 0.0;295
if (296
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&297
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&298
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&299
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance300
) {301
if (player->held_enemy == NULL) {302
player->held_enemy = enemy;303
picked_up = true;304
}305
}306
}307
}308
}309
if (!picked_up && player->held_enemy != NULL) {310
player->held_enemy = NULL;311
}312
}313
if (player->held_enemy != NULL) {314
player->held_enemy->pos = (pos_t) {315
.x = player->pos.x,316
.y = player->pos.y - (player->sneaking ? 0.3 : 0.5) * TILE_HEIGHT,317
};318
}319
// detect damage320
for (int i = 0; i < game->entities_len; i++) {321
if (game->entities[i].type == ENTITY_ENEMY) {322
enemy_t *enemy = game->entities[i].enemy;323
if (!enemy->frozen) {324
float tolerance = TILE_WIDTH / 5.0;325
if (326
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&327
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&328
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&329
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance330
) {331
if (player->damage_timer == 0) {332
if (player->health > 0) {333
player->health--;334
player->damage_timer = 80;335
player->velocity = 8.0;336
if (player->pos.x + PLAYER_WIDTH / 2.0 > enemy->pos.x + ENEMY_WIDTH / 2.0) {337
player->pos.x += 20.0;338
}339
else {340
player->pos.x -= 20.0;341
}342
if (player->held_enemy != NULL) {343
player->held_enemy = NULL;344
}345
}346
}347
}348
}349
}350
}351
// fall out of map352
if (player->pos.y > TILE_HEIGHT * game->level->height) {353
if (player->health > 0) {354
player->health = 0;355
}356
}357
// update timer358
if (player->on_ground && player->moving) {359
fz_timer_update(player->timer_walking);360
fz_timer_update(player->timer_sneaking);361
}362
else {363
fz_timer_reset(player->timer_walking);364
fz_timer_reset(player->timer_sneaking);365
}366
}368
void player_draw(player_t *player, game_t *game) {369
pos_t pos = pos_snap(player->pos);370
int frame = (player->sneaking || player->shooting)371
? fz_timer_get(player->timer_sneaking)372
: fz_timer_get(player->timer_walking);373
if (player->damage_timer / 4 % 2 == 0) {374
if (player->sneaking) {375
if (player->dir > 0) {376
DrawTextureRec(game->assets.entities, texture_rect(frame, 6, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);377
}378
else {379
DrawTextureRec(game->assets.entities, texture_rect(frame, 7, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);380
}381
}382
else {383
if (player->dir > 0) {384
DrawTextureRec(game->assets.entities, texture_rect(frame, 4, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);385
}386
else {387
DrawTextureRec(game->assets.entities, texture_rect(frame, 5, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);388
}389
}390
if (player->shooting) {391
float y = player->sneaking ? pos.y + 8 * SCALE : pos.y + 7 * SCALE;392
if (player->dir > 0) {393
DrawRectangle(pos.x + PLAYER_WIDTH, y, player->shooting_distance, 1 * SCALE, BLUE);394
}395
else {396
DrawRectangle(pos.x - player->shooting_distance, y, player->shooting_distance, 1 * SCALE, BLUE);397
}398
}399
}400
}402
void player_free(player_t *player) {403
fz_timer_free(player->timer_walking);404
fz_timer_free(player->timer_sneaking);405
free(player);406
}