aboutsummaryrefslogtreecommitdiff
path: root/game.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-10-11 21:43:36 +1100
committerquou <quou@disroot.org>2024-10-11 21:43:36 +1100
commit24b72170d34cee515398f206f087bfeafc7b6b55 (patch)
tree24c2bc3d7e1c0bdac9de38dae3c5d2de7e669f25 /game.c
parent9add408984464bd6b3cc018bb14c3d69ad0a2898 (diff)
game is pretty much done kek
Diffstat (limited to 'game.c')
-rw-r--r--game.c110
1 files changed, 102 insertions, 8 deletions
diff --git a/game.c b/game.c
index 2a4731d..d9cb85c 100644
--- a/game.c
+++ b/game.c
@@ -3,6 +3,7 @@
#include "game.h"
#include "plat.h"
#include "render.h"
+#include "str.h"
const Rect logo_rect = { 47, 0, 54, 16 };
@@ -11,6 +12,8 @@ void init_game(Game* g, Game_State s) {
g->st = s;
g->frame = 0;
g->want_next = 0;
+ g->kills = 0;
+ g->cfloor = 0;
init_world(w);
}
@@ -19,9 +22,13 @@ void update_menu(Game* g, App* a) {
for (i = 0; i < btn_count; i++)
if (a->btn_states[i] & btn_state_pressed) {
queue_gs(g, game_state_fade_out);
- queue_gs(g, game_state_generate);
+ queue_gs(g, game_state_reset);
+ queue_gs(g, game_state_compute_ec);
+ queue_gs(g, game_state_reset);
+ queue_gs(g, game_state_restart);
+ queue_gs(g, game_state_generate_intro);
queue_gs(g, game_state_fade_in);
- queue_gs(g, game_state_play);
+ queue_gs(g, game_state_intro);
g->want_next = 1;
return;
}
@@ -33,9 +40,12 @@ void update_gameover(Game* g, App* a) {
if (a->btn_states[i] & btn_state_pressed) {
queue_gs(g, game_state_fade_out);
queue_gs(g, game_state_reset);
- queue_gs(g, game_state_generate);
+ queue_gs(g, game_state_compute_ec);
+ queue_gs(g, game_state_reset);
+ queue_gs(g, game_state_restart);
+ queue_gs(g, game_state_generate_intro);
queue_gs(g, game_state_fade_in);
- queue_gs(g, game_state_play);
+ queue_gs(g, game_state_intro);
g->want_next = 1;
return;
}
@@ -63,9 +73,18 @@ int detect_next(Game* g, const World* w) {
const Player* p = &w->player;
if (p->y < 0) /* next level */ {
queue_gs(g, game_state_fade_out);
- queue_gs(g, game_state_generate);
- queue_gs(g, game_state_fade_in);
- queue_gs(g, game_state_play);
+ queue_gs(g, game_state_next);
+ queue_gs(g, game_state_reset);
+ queue_gs(g, game_state_restore_player);
+ if (g->cfloor + 1 >= room_count()) {
+ queue_gs(g, game_state_generate_outro);
+ queue_gs(g, game_state_fade_in);
+ queue_gs(g, game_state_outro);
+ } else {
+ queue_gs(g, game_state_generate);
+ queue_gs(g, game_state_fade_in);
+ queue_gs(g, game_state_play);
+ }
g->want_next = 1;
return 1;
}
@@ -85,6 +104,30 @@ int detect_gameover(Game* g, const World* w) {
return 0;
}
+int detect_start(Game* g, const World* w) {
+ if (w->player.x < (30 << fbits)) {
+ queue_gs(g, game_state_fade_out);
+ queue_gs(g, game_state_reset);
+ queue_gs(g, game_state_generate);
+ queue_gs(g, game_state_fade_in);
+ queue_gs(g, game_state_play);
+ g->want_next = 1;
+ return 1;
+ }
+ return 0;
+}
+
+int compute_ec(World* w) {
+ int i, e = room_count();
+ int c = 0;
+ for (i = 0; i < e; i++) {
+ init_world(w);
+ generate_floor(&w->map, w, i);
+ c += w->enemy_count;
+ }
+ return c;
+}
+
void update_game(Game* g, App* a) {
const Animation* fa;
if (g->want_next) {
@@ -112,12 +155,30 @@ void update_game(Game* g, App* a) {
else
g->want_next = 1;
break;
+ case game_state_restart:
+ g->cfloor = 0;
+ g->kills = 0;
+ g->want_next = 1;
+ break;
case game_state_reset:
+ g->hp = g->w.player.hp;
+ g->ch = g->w.player.charge;
init_world(&g->w);
g->want_next = 1;
break;
+ case game_state_restore_player:
+ g->w.player.hp = g->hp;
+ g->w.player.charge = g->ch;
+ g->hp = -1;
+ g->want_next = 1;
+ break;
+ case game_state_next:
+ g->cfloor++;
+ g->kills += g->w.kills;
+ g->want_next = 1;
+ break;
case game_state_generate:
- generate_floor(&g->w.map, &g->w);
+ generate_floor(&g->w.map, &g->w, g->cfloor);
g->want_next = 1;
break;
case game_state_play:
@@ -125,6 +186,25 @@ void update_game(Game* g, App* a) {
if (detect_next(g, &g->w)) break;
detect_gameover(g, &g->w);
break;
+ case game_state_generate_intro:
+ generate_intro(&g->w.map, &g->w);
+ g->want_next = 1;
+ break;
+ case game_state_generate_outro:
+ generate_outro(&g->w.map, &g->w);
+ g->want_next = 1;
+ break;
+ case game_state_compute_ec:
+ g->me = compute_ec(&g->w);
+ g->want_next = 1;
+ break;
+ case game_state_intro:
+ update_world(&g->w, a);
+ detect_start(g, &g->w);
+ break;
+ case game_state_outro:
+ update_world(&g->w, a);
+ break;
}
g->frame++;
}
@@ -193,6 +273,20 @@ void ren_state(const Game* g, Game_State s, Renderer* r) {
break;
case game_state_generate:
break;
+ case game_state_outro: {
+ char buf[32];
+ int x = 228;
+ ren_text(r, 200, 100, "You win!");
+ ren_text(r, 200, 105, "Kills: ");
+ int_to_buf(g->kills, buf);
+ ren_text(r, x, 105, buf);
+ x += string_len(buf) * 4;
+ ren_text(r, x, 105, "/");
+ int_to_buf(g->me, buf);
+ ren_text(r, x + 4, 105, buf);
+ }
+ /* fall through */
+ case game_state_intro:
case game_state_play:
ren_world(&g->w, r);
break;