A retro platform game
git clone git://soophie.de/freezo
blob: 9f4ae24925b5ff125f0de397f41d81e03a973b9c (9670 bytes)[doc][blame][raw]
1
#include <stdio.h>2
#include <stdlib.h>3
#include <string.h>5
#include "../lib/raylib.h"6
#include "../include/entity.h"7
#include "../include/game.h"8
#include "../include/tile.h"9
#include "../include/util.h"10
#include "../include/player.h"11
#include "../include/const.h"12
#include "../include/level.h"14
game_t *game_create(void) {15
game_t *game = malloc(sizeof(game_t));16
*game = (game_t) {17
.state = STATE_GAME,18
.level = NULL,19
.quit = false,20
.defeat = false,21
.victory = false,22
.menu = menu_create(),23
.player = NULL,24
.tiles = NULL,25
.tiles_len = 0,26
.entities = NULL,27
.entities_len = 0,28
.effects = NULL,29
.effects_len = 0,30
.assets.tiles = texture_load("assets/tiles.png", SCALE),31
.assets.entities = texture_load("assets/entities.png", SCALE),32
.assets.font = texture_load("assets/font.png", SCALE - 1),33
.assets.images = texture_load("assets/images.png", SCALE),34
.assets.background = texture_load("assets/background.png", SCALE),35
.assets.track = LoadSound("assets/song.wav"),36
.assets.win = LoadSound("assets/win.wav"),37
.assets.lose = LoadSound("assets/lose.wav"),38
.assets.locked = LoadSound("assets/locked.wav"),39
.camera = (Camera2D) {40
.offset = (pos_t) { 0.0, 0.0 },41
.zoom = 1,42
},43
.xp = 0,44
.sceen_timer = 0,45
.door = NULL,46
.overlay = LoadRenderTexture(WINDOW_WIDTH, WINDOW_HEIGHT),47
.is_muted = false,48
};49
level_load(game, LEVEL_1);50
SetSoundVolume(game->assets.track, GAME_MUSIC_VOLUME);51
SetSoundVolume(game->assets.win, GAME_SOUND_VOLUME);52
SetSoundVolume(game->assets.lose, GAME_SOUND_VOLUME);53
SetSoundVolume(game->assets.locked, GAME_SOUND_VOLUME);54
return game;55
}57
void game_update(game_t *game) {58
if (IsKeyPressed(KEY_M)) {59
game->is_muted = !game->is_muted;60
if (game->is_muted) {61
SetSoundVolume(game->assets.track, 0.0f);62
SetSoundVolume(game->assets.win, 0.0f);63
SetSoundVolume(game->assets.lose, 0.0f);64
SetSoundVolume(game->assets.locked, 0.0f);65
}66
else {67
SetSoundVolume(game->assets.track, GAME_MUSIC_VOLUME);68
SetSoundVolume(game->assets.win, GAME_SOUND_VOLUME);69
SetSoundVolume(game->assets.lose, GAME_SOUND_VOLUME);70
SetSoundVolume(game->assets.locked, GAME_SOUND_VOLUME);71
}72
}73
if (!IsSoundPlaying(game->assets.track) && !game->victory && !game->defeat) {74
PlaySound(game->assets.track);75
}76
switch (game->state) {77
case STATE_MENU: {78
menu_update(game->menu, game);79
break;80
}81
case STATE_GAME: {82
if (IsKeyPressed(KEY_ESCAPE)) {83
game->state = STATE_MENU;84
game->menu->idx = 0;85
}86
if (game->level != NULL) {87
// defeat88
if (game->defeat) {89
if (game->sceen_timer == 0) {90
StopSound(game->assets.track);91
}92
if (game->sceen_timer == 20) {93
PlaySound(game->assets.lose);94
}95
if (game->sceen_timer == GAME_SCEEN_TIMER) {96
level_e level = game->level->type;97
int max_health = game->player->max_health;98
max_health -= 2;99
if (max_health <= 0) {100
level = LEVEL_1;101
max_health = PLAYER_MAX_HEALTH;102
}103
level_unload(game);104
level_load(game, level);105
game->player->max_health = max_health;106
game->player->health = max_health;107
game->defeat = false;108
game->xp = 0;109
game->sceen_timer = 0;110
}111
else {112
game->sceen_timer++;113
}114
}115
// victory116
if (game->victory) {117
if (game->sceen_timer == 0) {118
StopSound(game->assets.track);119
}120
if (game->sceen_timer == 20) {121
PlaySound(game->assets.win);122
}123
if (game->sceen_timer == GAME_SCEEN_TIMER) {124
if (game->level->type < LEVELS) {125
level_e level = game->level->type;126
int max_health = game->player->max_health;127
level++;128
level_unload(game);129
level_load(game, level);130
game->player->max_health = max_health;131
game->player->health = max_health;132
}133
game->victory = false;134
game->sceen_timer = 0;135
}136
else {137
if (game->sceen_timer == 125) {138
game->door->is_locked = true;139
}140
game->sceen_timer++;141
}142
}143
if (!game->victory && !game->defeat) {144
player_update(game->player, game);145
}146
for (int i = 0; i < game->tiles_len; i++) {147
tile_update(game->tiles[i], game);148
}149
for (int i = 0; i < game->entities_len; i++) {150
int len = game->entities_len;151
entity_update(&game->entities[i], game);152
// check if entity was removed153
if (game->entities_len < len) {154
i--;155
}156
}157
for (int i = 0; i < game->effects_len; i++) {158
int len = game->effects_len;159
effect_update(game->effects[i], game);160
// check if enemy was removed161
if (game->effects_len < len) {162
i--;163
}164
}165
pos_t camera_pos = (pos_t) {166
.x = WINDOW_WIDTH / 2.0 - game->player->pos.x - PLAYER_WIDTH / 2.0,167
.y = WINDOW_HEIGHT / 2.0 - game->player->pos.y,168
};169
if (camera_pos.x > 0.0) {170
camera_pos.x = 0.0;171
}172
if (camera_pos.x + TILE_WIDTH * game->level->width < WINDOW_WIDTH) {173
camera_pos.x = WINDOW_WIDTH - TILE_WIDTH * game->level->width;174
}175
if (camera_pos.y > 0.0) {176
camera_pos.y = 0.0;177
}178
if (camera_pos.y + TILE_HEIGHT * game->level->height < WINDOW_HEIGHT) {179
camera_pos.y = WINDOW_HEIGHT - TILE_HEIGHT * game->level->height;180
}181
game->camera.offset = pos_snap(camera_pos);182
}183
break;184
}185
}186
}188
void game_draw(game_t *game) {189
// render overlay190
BeginTextureMode(game->overlay);191
ClearBackground(BLACK);192
DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, BLACK);193
BeginBlendMode(BLEND_SUBTRACT_COLORS);194
int radius = WINDOW_WIDTH;195
radius -= radius / 80.0 * game->sceen_timer;196
if (radius < PLAYER_HEIGHT) {197
radius = PLAYER_HEIGHT;198
}199
DrawCircle(game->player->pos.x + PLAYER_WIDTH / 2.0, WINDOW_HEIGHT - (game->player->pos.y + PLAYER_HEIGHT / 2.0), radius, WHITE);200
EndBlendMode();201
EndTextureMode();202
// render sceen203
BeginDrawing();204
ClearBackground((Color) { 5, 5, 5, 255 });205
if (game->level != LEVEL_NULL) {206
BeginMode2D(game->camera);207
for (int i = 0; i < game->tiles_len; i++) {208
tile_draw(game->tiles[i], game);209
}210
for (int i = 0; i < game->entities_len; i++) {211
entity_t *entity = &game->entities[i];212
if (entity->type == ENTITY_GATE || entity->type == ENTITY_DOOR) {213
entity_draw(entity, game);214
}215
}216
for (int i = 0; i < game->entities_len; i++) {217
entity_t *entity = &game->entities[i];218
if (entity->type != ENTITY_GATE && entity->type != ENTITY_DOOR) {219
entity_draw(entity, game);220
}221
}222
if (game->sceen_timer < 100) {223
player_draw(game->player, game);224
}225
for (int i = 0; i < game->effects_len; i++) {226
effect_draw(game->effects[i], game);227
}228
// draw overlay229
DrawTexture(game->overlay.texture, 0, 0, WHITE);230
EndMode2D();231
// draw interface232
int i = 0;233
for (i = 0; i < game->player->health / 2; i++) {234
DrawTextureRec(game->assets.tiles, texture_rect(0, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0 + i * (TILE_WIDTH + TILE_WIDTH / 4.0), TILE_WIDTH / 2.0 }, WHITE);235
}236
if (game->player->health % 2 != 0) {237
DrawTextureRec(game->assets.tiles, texture_rect(1, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0 + i * (TILE_WIDTH + TILE_WIDTH / 4.0), TILE_WIDTH / 2.0 }, WHITE);238
i++;239
}240
for (; i < game->player->max_health / 2; i++) {241
DrawTextureRec(game->assets.tiles, texture_rect(2, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0 + i * (TILE_WIDTH + TILE_WIDTH / 4.0), TILE_WIDTH / 2.0 }, WHITE);242
}243
if (game->is_muted) {244
DrawTextureRec(game->assets.tiles, texture_rect(5, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE);245
}246
else {247
DrawTextureRec(game->assets.tiles, texture_rect(4, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE);248
}249
char xp_text[10];250
snprintf(xp_text, 10, "%dXP", game->xp);251
pos_t xp_pos = (pos_t) {252
.x = WINDOW_WIDTH - TILE_WIDTH / 2.0,253
.y = TILE_WIDTH / 1.5,254
};255
text_draw(xp_pos, xp_text, TEXT_ALIGNMENT_RIGHT, game);256
char level_text[10];257
snprintf(level_text, 10, "L%d", game->level->type);258
pos_t level_pos = (pos_t) {259
.x = WINDOW_WIDTH - TILE_WIDTH / 2.0,260
.y = TILE_WIDTH / 1.5 + 25,261
};262
text_draw(level_pos, level_text, TEXT_ALIGNMENT_RIGHT, game);263
}264
if (game->state == STATE_MENU) {265
menu_draw(game->menu, game);266
}267
EndDrawing();268
}270
void game_free(game_t *game) {271
menu_free(game->menu);272
if (game->level != LEVEL_NULL) {273
level_unload(game);274
}275
UnloadTexture(game->assets.tiles);276
UnloadTexture(game->assets.entities);277
UnloadTexture(game->assets.font);278
UnloadTexture(game->assets.images);279
UnloadTexture(game->assets.background);280
UnloadSound(game->assets.track);281
UnloadSound(game->assets.win);282
UnloadSound(game->assets.lose);283
UnloadSound(game->assets.locked);284
UnloadRenderTexture(game->overlay);285
free(game);286
}