added lost menu option
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 26 Nov 2021 04:38:32 +0000 (22:38 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 26 Nov 2021 04:38:32 +0000 (22:38 -0600)
tests/snake.onyx

index 7bea9a6e9135bb4fb6ad44dfe85e12d83a5533f4..66da1f53a8b374feb3e471072dddade8c765a174 100644 (file)
@@ -58,7 +58,7 @@ snake_grow :: (use this: ^Snake, amount := 1) {
 }
 
 snake_draw :: (use this: ^Snake, cell_size: f32) {
-    for it: iter.enumerate(iter.as_iterator(body)) {
+    for it: iter.enumerate(body) {
         hb.graphics.setColor(0, 0.6 - 0.4 * (~~it.index / cast(f32) body.count), 0);
         hb.graphics.rectangle(.Fill, ~~it.value.x * cell_size, ~~it.value.y * cell_size, cell_size, cell_size);
     }
@@ -105,9 +105,26 @@ Main_Menu_Options :: struct {
     };
 }
 
+Lost_Options :: struct {
+    ["Play again"]
+    option_1 := () {
+
+    };
+
+    ["Main menu"]
+    option_2 := () {
+        state = .MainMenu;
+        active_menu = .{ Main_Menu_Options };
+
+        the_snake = snake_make(.{ 0, 0 });
+        the_food = .{ 10, 10 };
+    };
+}
+
 active_menu: Menu;
 
 load :: () {
+    random.set_seed(clock.time());
     hb.window.setTitle("Snake 🐍");
 
     hb.utils.load_assets(^assets);
@@ -142,15 +159,18 @@ update :: (dt: f32) {
             last_down_down = down_down;
         }
 
-        if !up_down && last_up_down { active_menu.selected_option -= 1; }
-        if !down_down && last_down_down { active_menu.selected_option += 1; }
+        if up_down && !last_up_down { active_menu.selected_option -= 1; }
+        if down_down && !last_down_down { active_menu.selected_option += 1; }
 
         option_count := (cast(^type_info.Type_Info_Struct) type_info.get_type_info(active_menu.options)).members.count;
 
         if active_menu.selected_option < 0 do active_menu.selected_option = 0;
         if active_menu.selected_option >= option_count do active_menu.selected_option = option_count - 1;
 
-        if hb.input.keyIsDown(.Space) {
+        #persist last_space_down := false;
+        space_down := hb.input.keyIsDown(.Space);
+        defer last_space_down = space_down;
+        if space_down && !last_space_down {
             option := (cast(^type_info.Type_Info_Struct) type_info.get_type_info(active_menu.options)).members[active_menu.selected_option];
             if option.type == #type () -> void {
                 func := *(cast(^() -> void) option.default);
@@ -193,6 +213,7 @@ update :: (dt: f32) {
 
         if cell_contains_snake_body(the_snake.head) {
             state = .Lost;
+            active_menu = .{Lost_Options};
         }
     }
 }
@@ -242,6 +263,8 @@ draw :: () {
             hb.graphics.setFont(assets.title_font);
             hb.graphics.setColor(1, 1, 1);
             centered_text("You Lost!", 100);
+
+            draw_menu(200);
         }
     }
 }