A retro platform game
git clone git://soophie.de/freezo
Binary file
Binary file
1
#pragma once3
typedef struct Door door_t;5
#include "game.h"6
#include "util.h"8
#define DOOR_COUNTDOWN 2010
struct Door {11
pos_t pos;12
bool is_locked;13
int countdown;14
};16
door_t *door_create(pos_t pos);17
void door_update(door_t *door, game_t *game);18
void door_draw(door_t *door, game_t *game);19
void door_free(door_t *door);21
bool door_unlock(door_t *door, game_t *game);
5
#include "enemy.h"6
#include "gate.h"7
#include "game.h"8
#include "door.h"10
typedef enum {11
ENTITY_ENEMY,12
ENTITY_GATE,13
ENTITY_DOOR,14
} entity_e;16
struct Entity {
18
union {19
enemy_t *enemy;20
gate_t *gate;21
door_t *door;22
};23
bool is_bad;24
};26
entity_t *entity_create(entity_e type);
1
#include <stdlib.h>3
#include "../include/door.h"4
#include "../include/const.h"6
door_t *door_create(pos_t pos) {7
door_t *door = malloc(sizeof(door_t));8
door->pos = pos;9
door->is_locked = true;10
door->countdown = 0;11
return door;12
}14
void door_update(door_t *door, game_t *game) {15
(void) game;16
if (door->countdown > 0) {17
door->countdown--;18
}19
}21
void door_draw(door_t *door, game_t *game) {22
if (door->is_locked) {23
DrawTextureRec(game->assets.tiles, texture_rect(1, 2, TILE_WIDTH, TILE_HEIGHT), (pos_t) { door->pos.x, door->pos.y - TILE_HEIGHT }, WHITE);24
DrawTextureRec(game->assets.tiles, texture_rect(1, 3, TILE_WIDTH, TILE_HEIGHT), door->pos, WHITE);25
if (door->countdown > 0) {26
pos_t lock_pos = {27
.x = door->pos.x,28
.y = door->pos.y - 2 * TILE_HEIGHT,29
};30
DrawTextureRec(game->assets.tiles, texture_rect(3, 1, TILE_WIDTH, TILE_HEIGHT), lock_pos, WHITE);31
}32
}33
else {34
DrawTextureRec(game->assets.tiles, texture_rect(2, 2, TILE_WIDTH, TILE_HEIGHT), (pos_t) { door->pos.x, door->pos.y - TILE_HEIGHT }, WHITE);35
DrawTextureRec(game->assets.tiles, texture_rect(2, 3, TILE_WIDTH, TILE_HEIGHT), door->pos, WHITE);36
}37
}39
void door_free(door_t *door) {40
free(door);41
}43
bool door_unlock(door_t *door, game_t *game) {44
if (!door->is_locked) {45
return true;46
}47
int enemies_len = 0;48
bool gates_frozen = true;49
for (int i = 0; i < game->entities_len; i++) {50
if(game->entities[i].type == ENTITY_ENEMY) {51
enemies_len++;52
}53
if(game->entities[i].type == ENTITY_GATE) {54
if (!game->entities[i].gate->frozen) {55
gates_frozen = false;56
}57
}58
}59
if (enemies_len == 0 && gates_frozen) {60
door->is_locked = false;61
return true;62
}63
door->countdown = DOOR_COUNTDOWN;64
return false;65
}
3
#include "../include/entity.h"4
#include "../include/enemy.h"5
#include "../include/gate.h"6
#include "../include/door.h"8
entity_t *entity_create(entity_e type) {9
entity_t *entity = malloc(sizeof(entity_t));10
entity->type = type;11
switch (entity->type) {12
case ENTITY_ENEMY:13
entity->is_bad = true;14
break;15
case ENTITY_GATE:16
entity->is_bad = true;17
break;18
case ENTITY_DOOR:19
entity->is_bad = false;20
break;21
}22
return entity;23
}
30
case ENTITY_GATE:31
gate_update(entity->gate, game);32
break;33
case ENTITY_DOOR:34
door_update(entity->door, game);35
break;36
}37
}
44
case ENTITY_GATE:45
gate_draw(entity->gate, game);46
break;47
case ENTITY_DOOR:48
door_draw(entity->door, game);49
break;50
}51
}
58
case ENTITY_GATE:59
gate_free(entity->gate);60
break;61
case ENTITY_DOOR:62
door_free(entity->door);63
break;64
}65
}
58
// defeat59
if (game->defeat) {60
if (game->sceen_timer == 100) {61
level_e level = game->level->type;62
level_unload(game);62
level_load(game, LEVEL_1);63
level_load(game, level);64
game->defeat = false;65
game->xp = 0;66
game->sceen_timer = 0;
70
}71
}72
// victory72
int enemies_len = 0;73
bool gates_frozen = true;74
for (int i = 0; i < game->entities_len; i++) {75
if(game->entities[i].type == ENTITY_ENEMY) {76
enemies_len++;77
}78
if(game->entities[i].type == ENTITY_GATE) {79
if (!game->entities[i].gate->frozen) {80
gates_frozen = false;73
if (game->victory) {74
if (game->sceen_timer == 100) {75
if (game->level->type < LEVELS) {76
level_e level = game->level->type;77
level++;78
level_unload(game);79
level_load(game, level);80
}81
game->victory = false;82
game->sceen_timer = 0;83
}83
}84
if (enemies_len == 0 && gates_frozen) {85
game->victory = true;86
if (game->victory) {87
if (game->sceen_timer == 100) {88
if (game->level->type < LEVELS) {89
level_e level = game->level->type;90
level++;91
level_unload(game);92
level_load(game, level);93
}94
game->victory = false;95
game->sceen_timer = 0;96
}97
else {98
game->sceen_timer++;99
}84
else {85
game->sceen_timer++;86
}87
}88
if (!game->victory && !game->defeat) {
14
"........................"15
"........................"16
"......e......3.........."17
"....xxxx................"17
"....xxxxx..............."18
".....b.................."19
"...........--...xxx....."20
"................e......."21
"...........2...--xxxxx.."22
"....p..................."22
"....p....u.............."23
"..xxxxxxxxx---xxxxxxxx.."24
"........................"25
"........................"
30
"........................"31
"......3....b............"32
"........................"33
"........................"33
"................u......."34
"...............xxxx....."35
"..o......s.e............"36
".......xxxxxxtxxx--xx..."
51
"............-txx-......."52
".............w.........."53
"............-w..-......."54
"........s....w.........."55
"..x---xxxxxxxtxxx--xx..."54
"........s....w......u..."55
"..x---xxxxxxxtxxx--xxx.."56
"....e........w....e....."57
"...---....2..w--txxx...."58
"....3........w..w......."
176
game->entities[game->entities_len] = (entity_t) {177
.type = ENTITY_ENEMY,178
.enemy = enemy,179
.is_bad = true,180
};181
game->entities_len++;182
break;
190
game->entities[game->entities_len] = (entity_t) {191
.type = ENTITY_GATE,192
.gate = gate,193
.is_bad = true,194
};195
game->entities_len++;196
break;197
}198
case 'u': {199
game->entities = realloc(game->entities, sizeof(enemy_t) * (game->entities_len + 1));200
door_t *door = door_create((pos_t) {201
.x = x * TILE_WIDTH + (TILE_WIDTH - ENEMY_WIDTH) / 2.0,202
.y = y * TILE_HEIGHT,203
});204
game->entities[game->entities_len] = (entity_t) {205
.type = ENTITY_DOOR,206
.door = door,207
.is_bad = false,208
};209
game->entities_len++;210
break;
248
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;256
switch (entity->type) {257
case ENTITY_ENEMY:
260
case ENTITY_GATE:261
entity_rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);262
break;263
case ENTITY_DOOR:264
entity_rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);265
break;266
}267
if (rect_collide(shooting_rect, entity_rect)) {268
float entity_x = entity_rect.x + entity_rect.width / 2.0;
321
float tolerance = 0.0;322
if (323
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&317
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&318
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&324
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&325
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&326
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance327
) {328
if (player->held_enemy == NULL) {
343
.y = player->pos.y - (player->sneaking ? 0.3 : 0.5) * TILE_HEIGHT,344
};345
}346
// detect door entering347
if (holding) {348
for (int i = 0; i < game->entities_len; i++) {349
if (game->entities[i].type == ENTITY_DOOR) {350
door_t *door = game->entities[i].door;351
float tolerance = 0.0;352
if (353
player->pos.x <= door->pos.x + ENEMY_WIDTH - tolerance &&354
player->pos.x + PLAYER_WIDTH >= door->pos.x + tolerance &&355
player->pos.y <= door->pos.y + ENEMY_HEIGHT - tolerance &&356
player->pos.y + PLAYER_HEIGHT >= door->pos.y + tolerance357
) {358
if (door_unlock(door, game)) {359
game->victory = true;360
}361
}362
}363
}364
}365
// detect damage366
for (int i = 0; i < game->entities_len; i++) {367
if (game->entities[i].type == ENTITY_ENEMY) {
370
float tolerance = TILE_WIDTH / 5.0;371
if (372
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&347
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&348
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&373
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&374
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&375
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance376
) {377
if (player->damage_timer == 0) {