#include "pac.h" #include "ncurses.h" #include "queue.h" void initialize_pac(struct pacman *pac) { pac->lives = max_live; pac->score = 0; pac->position.y = pac_y; pac->position.x = pac_x; pac->direction = none; pac->is_energizer_eaten = 0; } static enum movement_direction get_matching_for_directions(game_space field, struct pacman *pac, enum movement_direction direction) { struct free_directions free_paths = find_free_directions(field, pac->position.x, pac->position.y); switch(direction) { case left: return free_paths.left ? left : none; case right: return free_paths.right ? right : none; case up: return free_paths.up ? up : none; case down: return free_paths.down ? down : none; default: return none; } } void check_remaining_direction(game_space field, struct pacman *pac, enum movement_direction *stored_direction) { enum movement_direction matching_result; if(*stored_direction == none) pac->direction = get_matching_for_directions(field, pac, pac->direction); else { matching_result = get_matching_for_directions(field, pac, *stored_direction); if(matching_result != none) pac->direction = matching_result; else pac->direction = get_matching_for_directions(field, pac, pac->direction); *stored_direction = none; } } static void check_remaining_direction_after_pressed_key(game_space field, struct pacman *pac) { pac->direction = get_matching_for_directions(field, pac, pac->direction); } void change_pac_direction(game_space field, struct pacman *pac, int key, enum movement_direction *stored_direction) { enum state { no, yes } is_changed_direction; is_changed_direction = no; struct free_directions path = find_free_directions(field, pac->position.x, pac->position.y); switch(key) { case KEY_LEFT: if(path.left) { pac->direction = left; is_changed_direction = yes; } else *stored_direction = left; break; case KEY_RIGHT: if(path.right) { pac->direction = right; is_changed_direction = yes; } else *stored_direction = right; break; case KEY_UP: if(path.up) { pac->direction = up; is_changed_direction = yes; } else *stored_direction = up; break; case KEY_DOWN: if(path.down) { pac->direction = down; is_changed_direction = yes; } else *stored_direction = down; break; } if(is_changed_direction == no) check_remaining_direction_after_pressed_key(field, pac); } static void eat_energizer(game_space field, struct pacman *pac) { if(field_has_energizer(field, pac->position.x, pac->position.y)) { pac->is_energizer_eaten = 1; clear_energizer(field, pac->position); } } void make_pac_move(game_space field, struct pacman *pac, struct queue *eaten_coins) { clear_or_revert_symbol(field, pac->position, pac_char, eaten_coins); switch(pac->direction) { case left: --pac->position.x; break; case right: ++pac->position.x; break; case up: --pac->position.y; break; case down: ++pac->position.y; break; default: return; } if(check_coin_for_pac(field, pac->position, eaten_coins)) { queue_push(eaten_coins, &pac->position); ++pac->score; } change_point_if_outside_tunnel(&pac->position); eat_energizer(field, pac); } void catch_pac(struct pacman *pac) { --pac->lives; erase_life(pac->lives); if(pac->lives >= 1) { pac->position.y = pac_y; pac->position.x = pac_x; pac->direction = none; } }