Sat, 23 Aug 2025 21:01:41 +0200
move game over text to the UI scene
demo/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/demo/snake/snake.c Fri Aug 22 23:27:36 2025 +0200 +++ b/demo/snake/snake.c Sat Aug 23 21:01:41 2025 +0200 @@ -191,6 +191,37 @@ return node; } +static void game_over_text_keep_centered(AscBehavior *behavior) { + if (!asc_active_window->resized) return; + + // TODO: if we could react on some sort of "unhidden" event, we could pause this behavior while hidden + + AscSceneNode *node = behavior->node; + // center the "game over" text in the game field viewport + const asc_rect main_scene_viewport = MAIN_SCENE->camera.viewport; + asc_scene_node_set_position2f(node, ASC_VEC2F( + main_scene_viewport.pos.x + main_scene_viewport.size.x / 2.f, + main_scene_viewport.pos.y + main_scene_viewport.size.y / 2.f - 36 + )); +} + +static AscSceneNode *game_over_create_text(void) { + AscSceneNode *node = asc_text( + .name = "game_over_text", + .text = "Game Over\nPress R to Restart", + .color = ASC_RGB(1, 1, 1), + .font = asc_font(ASC_FONT_REGULAR, 36), + .alignment = ASC_TEXT_ALIGN_CENTER, + .centered = true, + ); + + asc_scene_node_hide(node); + asc_behavior_add(node, game_over_text_keep_centered); + asc_ui_add_node(node); + + return node; +} + static bool game_field_tile_chown(asc_vec2u coords, Player *player) { unsigned x = coords.x, y = coords.y; asc_ptr_cast(AscRectangle, tile, game.field->nodes[x][y]); @@ -546,20 +577,7 @@ asc_scene_node_hide(fps_counter); // create game over text - AscSceneNode *text_game_over = asc_text( - .name = "game_over_text", - .text = "Game Over\nPress R to Restart", - .color = ASC_RGB(1, 1, 1), - .font = asc_font(ASC_FONT_REGULAR, 36), - .alignment = ASC_TEXT_ALIGN_CENTER, - .centered = true, - .x = (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE/2, - .y = (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE/2 - 60, - ); - asc_scene_node_hide(text_game_over); - // TODO: add as a UI node and add a behavior which centers the node in the main scenes viewport - // otherwise we won't be able to implement a moving camera in the future - asc_scene_add_node(MAIN_SCENE, text_game_over); + AscSceneNode *text_game_over = game_over_create_text(); // initialize the game state // TODO: add a main menu and start with the menu