demo/snake/snake.c

changeset 275
2256af1440db
parent 274
ba7f043f9fdf
equal deleted inserted replaced
274:ba7f043f9fdf 275:2256af1440db
189 asc_behavior_pause_all_while_hidden(node); 189 asc_behavior_pause_all_while_hidden(node);
190 asc_ui_add_node(node); 190 asc_ui_add_node(node);
191 return node; 191 return node;
192 } 192 }
193 193
194 static void game_over_text_keep_centered(AscBehavior *behavior) {
195 if (!asc_active_window->resized) return;
196
197 // TODO: if we could react on some sort of "unhidden" event, we could pause this behavior while hidden
198
199 AscSceneNode *node = behavior->node;
200 // center the "game over" text in the game field viewport
201 const asc_rect main_scene_viewport = MAIN_SCENE->camera.viewport;
202 asc_scene_node_set_position2f(node, ASC_VEC2F(
203 main_scene_viewport.pos.x + main_scene_viewport.size.x / 2.f,
204 main_scene_viewport.pos.y + main_scene_viewport.size.y / 2.f - 36
205 ));
206 }
207
208 static AscSceneNode *game_over_create_text(void) {
209 AscSceneNode *node = asc_text(
210 .name = "game_over_text",
211 .text = "Game Over\nPress R to Restart",
212 .color = ASC_RGB(1, 1, 1),
213 .font = asc_font(ASC_FONT_REGULAR, 36),
214 .alignment = ASC_TEXT_ALIGN_CENTER,
215 .centered = true,
216 );
217
218 asc_scene_node_hide(node);
219 asc_behavior_add(node, game_over_text_keep_centered);
220 asc_ui_add_node(node);
221
222 return node;
223 }
224
194 static bool game_field_tile_chown(asc_vec2u coords, Player *player) { 225 static bool game_field_tile_chown(asc_vec2u coords, Player *player) {
195 unsigned x = coords.x, y = coords.y; 226 unsigned x = coords.x, y = coords.y;
196 asc_ptr_cast(AscRectangle, tile, game.field->nodes[x][y]); 227 asc_ptr_cast(AscRectangle, tile, game.field->nodes[x][y]);
197 int old_owner = game.field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK; 228 int old_owner = game.field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK;
198 if (player == NULL) { 229 if (player == NULL) {
544 // create the fps counter 575 // create the fps counter
545 AscSceneNode *fps_counter = fps_counter_create(); 576 AscSceneNode *fps_counter = fps_counter_create();
546 asc_scene_node_hide(fps_counter); 577 asc_scene_node_hide(fps_counter);
547 578
548 // create game over text 579 // create game over text
549 AscSceneNode *text_game_over = asc_text( 580 AscSceneNode *text_game_over = game_over_create_text();
550 .name = "game_over_text",
551 .text = "Game Over\nPress R to Restart",
552 .color = ASC_RGB(1, 1, 1),
553 .font = asc_font(ASC_FONT_REGULAR, 36),
554 .alignment = ASC_TEXT_ALIGN_CENTER,
555 .centered = true,
556 .x = (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE/2,
557 .y = (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE/2 - 60,
558 );
559 asc_scene_node_hide(text_game_over);
560 // TODO: add as a UI node and add a behavior which centers the node in the main scenes viewport
561 // otherwise we won't be able to implement a moving camera in the future
562 asc_scene_add_node(MAIN_SCENE, text_game_over);
563 581
564 // initialize the game state 582 // initialize the game state
565 // TODO: add a main menu and start with the menu 583 // TODO: add a main menu and start with the menu
566 game.state = GAME_STATE_PLAYING; 584 game.state = GAME_STATE_PLAYING;
567 game.players[0] = player_create(); 585 game.players[0] = player_create();

mercurial