]> git.lizzy.rs Git - nothing.git/blobdiff - src/game.c
(#639) Make rigid bodies interact with platforms
[nothing.git] / src / game.c
index c58c498151313568e0a95e6e6ab552036d9a77dd..415c7e8561100387d12aae0d04450b6d75c77b59 100644 (file)
@@ -13,6 +13,8 @@
 #include "ui/console.h"
 #include "ui/edit_field.h"
 #include "str.h"
+#include "ebisp/builtins.h"
+#include "broadcast.h"
 
 typedef enum Game_state {
     GAME_STATE_RUNNING = 0,
@@ -26,6 +28,7 @@ typedef struct Game {
     Lt *lt;
 
     Game_state state;
+    Broadcast *broadcast;
     LevelPicker *level_picker;
     Level *level;
     char *level_file_path;
@@ -54,7 +57,15 @@ Game *create_game(const char *level_file_path,
     }
     game->lt = lt;
 
-    game->state = GAME_STATE_RUNNING;
+    game->state = GAME_STATE_LEVEL_PICKER;
+
+    game->broadcast = PUSH_LT(
+        lt,
+        create_broadcast(game),
+        destroy_broadcast);
+    if (game->broadcast == NULL) {
+        RETURN_LT(lt, NULL);
+    }
 
     game->level_picker = PUSH_LT(
         lt,
@@ -64,13 +75,7 @@ Game *create_game(const char *level_file_path,
         RETURN_LT(lt, NULL);
     }
 
-    game->level = PUSH_LT(
-        lt,
-        create_level_from_file(level_file_path),
-        destroy_level);
-    if (game->level == NULL) {
-        RETURN_LT(lt, NULL);
-    }
+    game->level = NULL;
 
     game->level_file_path = PUSH_LT(
         lt,
@@ -110,7 +115,7 @@ Game *create_game(const char *level_file_path,
 
     game->console = PUSH_LT(
         lt,
-        create_console(game->level, game->font),
+        create_console(game->broadcast, game->font),
         destroy_console);
     if (game->console == NULL) {
         RETURN_LT(lt, NULL);
@@ -150,7 +155,7 @@ int game_render(const Game *game)
     } break;
 
     case GAME_STATE_LEVEL_PICKER: {
-        if (level_picker_render(game->level_picker, game->renderer) < 0) {
+        if (level_picker_render(game->level_picker, game->camera, game->renderer) < 0) {
             return -1;
         }
     } break;
@@ -163,7 +168,17 @@ int game_render(const Game *game)
 
 int game_sound(Game *game)
 {
-    return level_sound(game->level, game->sound_samples);
+    switch (game->state) {
+    case GAME_STATE_RUNNING:
+    case GAME_STATE_PAUSE:
+    case GAME_STATE_CONSOLE:
+        return level_sound(game->level, game->sound_samples);
+    case GAME_STATE_LEVEL_PICKER:
+    case GAME_STATE_QUIT:
+        return 0;
+    }
+
+    return 0;
 }
 
 int game_update(Game *game, float delta_time)
@@ -201,6 +216,26 @@ int game_update(Game *game, float delta_time)
         if (level_picker_update(game->level_picker, delta_time) < 0) {
             return -1;
         }
+
+        if (level_picker_enter_camera_event(game->level_picker, game->camera) < 0) {
+            return -1;
+        }
+
+        const char *level_file_path = level_picker_selected_level(game->level_picker);
+
+        trace_assert(game->level == NULL);
+
+        if (level_file_path != NULL) {
+            game->level = PUSH_LT(
+                game->lt,
+                create_level_from_file(level_file_path, game->broadcast),
+                destroy_level);
+            if (game->level == NULL) {
+                return -1;
+            }
+
+            game->state = GAME_STATE_RUNNING;
+        }
     } break;
 
     case GAME_STATE_PAUSE:
@@ -259,7 +294,7 @@ static int game_event_running(Game *game, const SDL_Event *event)
                 game->lt,
                 game->level,
                 create_level_from_file(
-                    game->level_file_path));
+                    game->level_file_path, game->broadcast));
 
             if (game->level == NULL) {
                 log_fail("Could not reload level %s\n", game->level_file_path);
@@ -273,7 +308,7 @@ static int game_event_running(Game *game, const SDL_Event *event)
 
         case SDLK_q:
             log_info("Reloading the level's platforms from '%s'...\n", game->level_file_path);
-            if (level_reload_preserve_player(game->level, game->level_file_path) < 0) {
+            if (level_reload_preserve_player(game->level, game->level_file_path, game->broadcast) < 0) {
                 log_fail("Could not reload level %s\n", game->level_file_path);
                 game->state = GAME_STATE_QUIT;
                 return -1;
@@ -331,6 +366,21 @@ static int game_event_console(Game *game, const SDL_Event *event)
     return console_handle_event(game->console, event);
 }
 
+static int game_event_level_picker(Game *game, const SDL_Event *event)
+{
+    trace_assert(game);
+    trace_assert(event);
+
+    switch (event->type) {
+    case SDL_QUIT:
+        game->state = GAME_STATE_QUIT;
+        return 0;
+
+    default:
+        return level_picker_event(game->level_picker, event);
+    }
+}
+
 int game_event(Game *game, const SDL_Event *event)
 {
     trace_assert(game);
@@ -346,6 +396,9 @@ int game_event(Game *game, const SDL_Event *event)
     case GAME_STATE_CONSOLE:
         return game_event_console(game, event);
 
+    case GAME_STATE_LEVEL_PICKER:
+        return game_event_level_picker(game, event);
+
     default: {}
     }
 
@@ -360,16 +413,45 @@ int game_input(Game *game,
     trace_assert(game);
     trace_assert(keyboard_state);
 
-    if (game->state == GAME_STATE_QUIT  ||
-        game->state == GAME_STATE_PAUSE ||
-        game->state == GAME_STATE_CONSOLE) {
+    switch (game->state) {
+    case GAME_STATE_QUIT:
+    case GAME_STATE_PAUSE:
+    case GAME_STATE_CONSOLE:
         return 0;
+
+    case GAME_STATE_RUNNING:
+        return level_input(game->level, keyboard_state, the_stick_of_joy);
+
+    case GAME_STATE_LEVEL_PICKER:
+        return level_picker_input(game->level_picker, keyboard_state, the_stick_of_joy);
     }
 
-    return level_input(game->level, keyboard_state, the_stick_of_joy);
+    return 0;
 }
 
 int game_over_check(const Game *game)
 {
     return game->state == GAME_STATE_QUIT;
 }
+
+struct EvalResult
+game_send(Game *game, Gc *gc, struct Scope *scope,
+          struct Expr path)
+{
+    trace_assert(game);
+    trace_assert(gc);
+    trace_assert(scope);
+
+    const char *target = NULL;
+    struct Expr rest = void_expr();
+    struct EvalResult res = match_list(gc, "q*", path, &target, &rest);
+    if (res.is_error) {
+        return res;
+    }
+
+    if (strcmp(target, "level") == 0) {
+        return level_send(game->level, gc, scope, rest);
+    }
+
+    return unknown_target(gc, "game", target);
+}