demo/snake/snake.c

changeset 272
6ed9fb9662c0
parent 271
ddcf637587ce
equal deleted inserted replaced
271:ddcf637587ce 272:6ed9fb9662c0
380 } 380 }
381 } 381 }
382 } 382 }
383 } 383 }
384 384
385 static void player_controls(Player *player) { 385 static void player_controls(AscBehavior *behavior) {
386 // TODO: instead of checking the game state, disable the behavior when the player is dead
387 if (game.state != GAME_STATE_PLAYING) return;
388
389 Player *player = behavior->node->user_data;
390 // TODO: different key bindings for different player number in hot seat mode
386 if (asc_key_pressed(ASC_KEY(LEFT))) { 391 if (asc_key_pressed(ASC_KEY(LEFT))) {
387 if (player->direction != MOVE_RIGHT) { 392 if (player->direction != MOVE_RIGHT) {
388 player->target_direction = MOVE_LEFT; 393 player->target_direction = MOVE_LEFT;
389 } 394 }
390 } 395 }
437 player->color = ASC_RGB(1, 0, 0); 442 player->color = ASC_RGB(1, 0, 0);
438 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2u)); 443 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2u));
439 cxDefineDestructor(player->trace, player_trace_release_tile); 444 cxDefineDestructor(player->trace, player_trace_release_tile);
440 node->draw_func = player_draw; 445 node->draw_func = player_draw;
441 node->user_data_free_func = (cx_destructor_func2)player_destroy; 446 node->user_data_free_func = (cx_destructor_func2)player_destroy;
447 // add behaviors (the order is important!)
448 asc_behavior_add(node, player_controls);
442 asc_behavior_add(node, player_move); 449 asc_behavior_add(node, player_move);
443 return player; 450 return player;
444 } 451 }
445 452
446 static asc_rect main_scene_viewport_update(asc_vec2u window_size) { 453 static asc_rect main_scene_viewport_update(asc_vec2u window_size) {
558 asc_clear_error(); 565 asc_clear_error();
559 asc_context_quit(); 566 asc_context_quit();
560 } 567 }
561 568
562 // game states 569 // game states
563 // TODO: move all this into behaviors 570 // TODO: move this into a behavior - probably add a state machine that enables/disables behaviors
564 if (game.state == GAME_STATE_PLAYING) { 571 if (game.state == GAME_STATE_GAME_OVER) {
565 // TODO: implement hot seat 1on1 multiplayer
566 player_controls(game.players[0]);
567 } else if (game.state == GAME_STATE_GAME_OVER) {
568 asc_scene_node_show(text_game_over); 572 asc_scene_node_show(text_game_over);
569 if (asc_key_pressed(ASC_KEY(R))) { 573 if (asc_key_pressed(ASC_KEY(R))) {
570 // TODO: re-load the "level" 574 // TODO: re-load the "level"
571 player_position(game.players[0], 12, 8); 575 player_position(game.players[0], 12, 8);
572 player_set_health(game.players[0], 100); 576 player_set_health(game.players[0], 100);

mercurial