commit 12bdd63c09858a148d368d8239d7f5a925707757
parent a5e9ff259a83b6dcb534a2e141795a4f86322da8
Author: Sophie <info@soophie.de>
Date: Tue, 17 Dec 2024 22:52:14 +0100
feat: Added pause button & mouse click actions
Diffstat:
M | assets/tiles.png | | | 0 | |
M | src/game.c | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
M | src/menu.c | | | 119 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
3 files changed, 119 insertions(+), 63 deletions(-)
diff --git a/assets/tiles.png b/assets/tiles.png
Binary files differ.
diff --git a/src/game.c b/src/game.c
@@ -55,7 +55,47 @@ game_t *game_create(void) {
}
void game_update(game_t *game) {
+ bool do_toggle_mute = false;
+ if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) {
+ pos_t mouse_pos = GetMousePosition();
+ rect_t mouse_rect = {
+ .x = mouse_pos.x,
+ .y = mouse_pos.y,
+ .width = 1,
+ .height = 1,
+ };
+ rect_t mute_button_rect = {
+ .x = TILE_WIDTH / 2.0,
+ .y = WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0,
+ .width = TILE_WIDTH,
+ .height = TILE_HEIGHT,
+ };
+ rect_t pause_button_rect = {
+ .x = WINDOW_WIDTH - TILE_WIDTH - TILE_WIDTH / 2.0,
+ .y = WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0,
+ .width = TILE_WIDTH,
+ .height = TILE_HEIGHT,
+ };
+ if (rect_collide(mouse_rect, mute_button_rect)) {
+ do_toggle_mute = true;
+ }
+ if (!game->victory && !game->defeat) {
+ if (rect_collide(mouse_rect, pause_button_rect)) {
+ switch (game->state) {
+ case STATE_GAME:
+ game->state = STATE_MENU;
+ break;
+ case STATE_MENU:
+ game->state = STATE_GAME;
+ break;
+ }
+ }
+ }
+ }
if (IsKeyPressed(KEY_M)) {
+ do_toggle_mute = true;
+ }
+ if (do_toggle_mute) {
game->is_muted = !game->is_muted;
if (game->is_muted) {
SetSoundVolume(game->assets.track, 0.0f);
@@ -240,12 +280,6 @@ void game_draw(game_t *game) {
for (; i < game->player->max_health / 2; i++) {
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);
}
- if (game->is_muted) {
- 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);
- }
- else {
- 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);
- }
char xp_text[10];
snprintf(xp_text, 10, "%dXP", game->xp);
pos_t xp_pos = (pos_t) {
@@ -264,6 +298,23 @@ void game_draw(game_t *game) {
if (game->state == STATE_MENU) {
menu_draw(game->menu, game);
}
+ if (game->is_muted) {
+ 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);
+ }
+ else {
+ 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);
+ }
+ if (game->state == STATE_GAME) {
+ if (game->victory || game->defeat) {
+ DrawTextureRec(game->assets.tiles, texture_rect(8, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { WINDOW_WIDTH - TILE_WIDTH - TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE);
+ }
+ else {
+ DrawTextureRec(game->assets.tiles, texture_rect(6, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { WINDOW_WIDTH - TILE_WIDTH - TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE);
+ }
+ }
+ else {
+ DrawTextureRec(game->assets.tiles, texture_rect(7, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { WINDOW_WIDTH - TILE_WIDTH - TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE);
+ }
EndDrawing();
}
diff --git a/src/menu.c b/src/menu.c
@@ -14,71 +14,76 @@ menu_t *menu_create(void) {
}
void menu_update(menu_t *menu, game_t *game) {
- UNUSED(menu);
- UNUSED(game);
+ (void) menu;
if (IsKeyPressed(KEY_ESCAPE)) {
game->state = STATE_GAME;
}
- if (IsKeyPressed(KEY_W)) {
- if (menu->idx > 0) {
- menu->idx--;
- }
- }
- if (IsKeyPressed(KEY_S)) {
- if (menu->idx < 2) {
- menu->idx++;
- }
- }
- if (IsKeyPressed(KEY_ENTER)) {
- switch (menu->idx) {
- case 0:
- game->state = STATE_GAME;
- break;
- case 1: {
- if (game->level != LEVEL_NULL) {
- level_e level = game->level->type;
- level_unload(game);
- level_load(game, level);
- game->victory = false;
- game->defeat = false;
- }
- game->state = STATE_GAME;
- break;
- }
- case 2:
- game->quit = true;
- break;
- default:
- break;
- }
+ if (IsKeyPressed(KEY_Q)) {
+ game->quit = true;
}
+ // if (IsKeyPressed(KEY_W)) {
+ // if (menu->idx > 0) {
+ // menu->idx--;
+ // }
+ // }
+ // if (IsKeyPressed(KEY_S)) {
+ // if (menu->idx < 2) {
+ // menu->idx++;
+ // }
+ // }
+ // if (IsKeyPressed(KEY_ENTER)) {
+ // switch (menu->idx) {
+ // case 0:
+ // game->state = STATE_GAME;
+ // break;
+ // case 1: {
+ // if (game->level != LEVEL_NULL) {
+ // level_e level = game->level->type;
+ // int max_health = game->player->max_health;
+ // level_unload(game);
+ // level_load(game, level);
+ // game->player->max_health = max_health;
+ // game->player->health = max_health;
+ // game->victory = false;
+ // game->defeat = false;
+ // }
+ // game->state = STATE_GAME;
+ // break;
+ // }
+ // case 2:
+ // game->quit = true;
+ // break;
+ // default:
+ // break;
+ // }
+ // }
}
void menu_draw(menu_t *menu, game_t *game) {
- UNUSED(menu);
- UNUSED(game);
- int shift = 20;
+ (void) menu;
+ (void) game;
DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, (Color) { 0, 0, 0, 125 });
- DrawRectangle(WINDOW_WIDTH / 2.0 - 100, WINDOW_HEIGHT / 2.0 - 75 + shift - 10, 200, 150, (Color) { 50, 50, 50, 255 });
- DrawTextureRec(
- game->assets.images,
- (rect_t) { 0, 0, 56 * SCALE, 28 * SCALE },
- pos_snap((pos_t) { WINDOW_WIDTH / 2.0 - 3.5 * TILE_WIDTH, WINDOW_HEIGHT / 2.0 - 115 }),
- WHITE
- );
- text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + shift }, "RESUME", TEXT_ALIGNMENT_CENTER, game);
- text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + shift }, "RESTART", TEXT_ALIGNMENT_CENTER, game);
- text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + 30 + shift }, "QUIT", TEXT_ALIGNMENT_CENTER, game);
- pos_t cursor_pos = (pos_t) {
- .x = WINDOW_WIDTH / 2.0 - 70,
- .y = WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + menu->idx * 30 + shift,
- };
- DrawTextureRec(
- game->assets.font,
- texture_rect(0, 4, FONT_WIDTH, FONT_HEIGHT),
- pos_snap(cursor_pos),
- WHITE
- );
+ // int shift = 20;
+ // DrawRectangle(WINDOW_WIDTH / 2.0 - 100, WINDOW_HEIGHT / 2.0 - 75 + shift - 10, 200, 150, (Color) { 50, 50, 50, 255 });
+ // DrawTextureRec(
+ // game->assets.images,
+ // (rect_t) { 0, 0, 56 * SCALE, 28 * SCALE },
+ // pos_snap((pos_t) { WINDOW_WIDTH / 2.0 - 3.5 * TILE_WIDTH, WINDOW_HEIGHT / 2.0 - 115 }),
+ // WHITE
+ // );
+ // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + shift }, "RESUME", TEXT_ALIGNMENT_CENTER, game);
+ // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + shift }, "RESTART", TEXT_ALIGNMENT_CENTER, game);
+ // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + 30 + shift }, "QUIT", TEXT_ALIGNMENT_CENTER, game);
+ // pos_t cursor_pos = (pos_t) {
+ // .x = WINDOW_WIDTH / 2.0 - 70,
+ // .y = WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + menu->idx * 30 + shift,
+ // };
+ // DrawTextureRec(
+ // game->assets.font,
+ // texture_rect(0, 4, FONT_WIDTH, FONT_HEIGHT),
+ // pos_snap(cursor_pos),
+ // WHITE
+ // );
}
void menu_free(menu_t *menu) {