demo/snake/snake.c

changeset 273
966bfca56b9d
parent 272
6ed9fb9662c0
equal deleted inserted replaced
272:6ed9fb9662c0 273:966bfca56b9d
28 #include <ascension/core.h> 28 #include <ascension/core.h>
29 #include <ascension/ui.h> 29 #include <ascension/ui.h>
30 #include <ascension/sprite.h> 30 #include <ascension/sprite.h>
31 #include <ascension/2d.h> 31 #include <ascension/2d.h>
32 #include <ascension/shader.h> 32 #include <ascension/shader.h>
33 #include <ascension/util.h>
33 34
34 #include <cx/printf.h> 35 #include <cx/printf.h>
35 #include <cx/linked_list.h> 36 #include <cx/linked_list.h>
36 37
37 #define TEXTURE_2D_COUNT 3 38 #define TEXTURE_2D_COUNT 3
366 367
367 // update the trace, if necessary. 368 // update the trace, if necessary.
368 // remark: some calculations are repeated here, but they are cheap enough 369 // remark: some calculations are repeated here, but they are cheap enough
369 { 370 {
370 const asc_vec2u tile_coords = game_field_tile_at_position(node->position); 371 const asc_vec2u tile_coords = game_field_tile_at_position(node->position);
371 // TODO: player should have been destroyed before leaving the field
372 if (tile_coords.x > GAME_FIELD_SIZE || tile_coords.y > GAME_FIELD_SIZE) return; 372 if (tile_coords.x > GAME_FIELD_SIZE || tile_coords.y > GAME_FIELD_SIZE) return;
373 if (game_field_tile_chown(tile_coords, player)) { 373 if (game_field_tile_chown(tile_coords, player)) {
374 // new owner of the tile! 374 // new owner of the tile!
375 asc_vec2u p = tile_coords; 375 asc_vec2u p = tile_coords;
376 cxListAdd(player->trace, &p); 376 cxListAdd(player->trace, &p);
414 pl->new_position.x = x; 414 pl->new_position.x = x;
415 pl->new_position.y = y; 415 pl->new_position.y = y;
416 pl->reset_position = true; 416 pl->reset_position = true;
417 } 417 }
418 418
419 static void player_position_random(Player *pl) {
420 // TODO: check if the spawn location is viable when there is more action on the board
421 player_position(
422 pl,
423 4 + asc_util_rand(GAME_FIELD_SIZE - 8),
424 4 + asc_util_rand(GAME_FIELD_SIZE - 8)
425 );
426 }
427
419 static void player_destroy(CxAllocator *allocator, Player *player) { 428 static void player_destroy(CxAllocator *allocator, Player *player) {
420 cxListFree(player->trace); 429 cxListFree(player->trace);
421 cxFree(allocator, player); 430 cxFree(allocator, player);
422 } 431 }
423 432
433 .origin_x = GAME_FIELD_TILE_SIZE / 2, 442 .origin_x = GAME_FIELD_TILE_SIZE / 2,
434 .origin_y = GAME_FIELD_TILE_SIZE / 2, 443 .origin_y = GAME_FIELD_TILE_SIZE / 2,
435 ); 444 );
436 asc_scene_add_node(MAIN_SCENE, node); 445 asc_scene_add_node(MAIN_SCENE, node);
437 Player *player = asc_scene_node_allocate_data(node, sizeof(Player)); 446 Player *player = asc_scene_node_allocate_data(node, sizeof(Player));
438 player_position(player, 12, 8); 447 player_position_random(player);
439 player->speed = 3.f; // start with 3 tiles/sec 448 player->speed = 3.f; // start with 3 tiles/sec
440 player->number = 1; 449 player->number = 1;
441 player->health = 100; 450 player->health = 100;
442 player->color = ASC_RGB(1, 0, 0); 451 player->color = ASC_RGB(1, 0, 0);
443 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2u)); 452 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2u));
570 // TODO: move this into a behavior - probably add a state machine that enables/disables behaviors 579 // TODO: move this into a behavior - probably add a state machine that enables/disables behaviors
571 if (game.state == GAME_STATE_GAME_OVER) { 580 if (game.state == GAME_STATE_GAME_OVER) {
572 asc_scene_node_show(text_game_over); 581 asc_scene_node_show(text_game_over);
573 if (asc_key_pressed(ASC_KEY(R))) { 582 if (asc_key_pressed(ASC_KEY(R))) {
574 // TODO: re-load the "level" 583 // TODO: re-load the "level"
575 player_position(game.players[0], 12, 8); 584 player_position_random(game.players[0]);
576 player_set_health(game.players[0], 100); 585 player_set_health(game.players[0], 100);
577 game.state = GAME_STATE_PLAYING; 586 game.state = GAME_STATE_PLAYING;
578 asc_scene_node_hide(text_game_over); 587 asc_scene_node_hide(text_game_over);
579 } 588 }
580 } 589 }

mercurial