back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pac.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-04-02 17:43:35 +0300
committerscratko <m@scratko.xyz>2024-04-02 17:43:35 +0300
commitbdee2852c13f6b02ec5207ded584839a3118233e (patch)
tree39f1c14be91fb848ce0f94a64532e48a7667e5de /pac.c
downloadpacman-bdee2852c13f6b02ec5207ded584839a3118233e.tar.gz
pacman-bdee2852c13f6b02ec5207ded584839a3118233e.tar.bz2
pacman-bdee2852c13f6b02ec5207ded584839a3118233e.zip
Initial commit
Diffstat (limited to 'pac.c')
-rw-r--r--pac.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/pac.c b/pac.c
new file mode 100644
index 0000000..50256d8
--- /dev/null
+++ b/pac.c
@@ -0,0 +1,113 @@
+#include "pac.h"
+#include "ncurses.h"
+
+void initialize_pac(struct pacman *pac)
+{
+ pac->lives = max_live;
+ pac->points_eaten = 0;
+ pac->position.y = pac_y;
+ pac->position.x = pac_x;
+ pac->direction = none;
+}
+
+static enum movement_direction get_correct_path(struct pacman *pac,
+ struct free_directions
+ free_path,
+ enum movement_direction
+ direction)
+{
+ switch(direction) {
+ case left:
+ return free_path.left ? left : none;
+ case right:
+ return free_path.right ? right : none;
+ case up:
+ return free_path.up ? up : none;
+ case down:
+ return free_path.down ? down : none;
+ default:
+ return none;
+ }
+
+}
+
+void check_remaining_direction(game_space field, struct pacman *pac,
+ enum movement_direction *stored_direction)
+{
+ enum movement_direction temp_direction;
+ struct free_directions free_path =
+ find_free_directions(field, pac->position.y, pac->position.x);
+ enum movement_direction current_direction =
+ stored_direction ? *stored_direction : pac->direction;
+ temp_direction = get_correct_path(pac, free_path, current_direction);
+ if(temp_direction != none)
+ pac->direction = temp_direction;
+ else {
+ current_direction = pac->direction;
+ pac->direction = get_correct_path(pac, free_path, current_direction);
+ }
+ if(stored_direction)
+ *stored_direction = none;
+}
+
+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.y,
+ pac->position.x);
+ switch(key) {
+ case KEY_LEFT:
+ if(path.left) {
+ pac->direction = left;
+ is_changed_direction = yes;
+ } else
+ *stored_direction = left;
+ break;
+ case KEY_RIGHT:
+ if(key == KEY_RIGHT && path.right) {
+ pac->direction = right;
+ is_changed_direction = yes;
+ } else
+ *stored_direction = right;
+ break;
+ case KEY_UP:
+ if(key == KEY_UP && path.up) {
+ pac->direction = up;
+ is_changed_direction = yes;
+ } else
+ *stored_direction = up;
+ break;
+ case KEY_DOWN:
+ if(key == KEY_DOWN && path.down) {
+ pac->direction = down;
+ is_changed_direction = yes;
+ } else
+ *stored_direction = down;
+ break;
+ }
+ if(is_changed_direction == no)
+ check_remaining_direction(field, pac, NULL);
+}
+
+void make_pac_move(game_space field, struct pacman *pac)
+{
+ clear_symbol(field, pac->position.y, pac->position.x, pac_char);
+ switch(pac->direction) {
+ case left:
+ --pac->position.x;
+ return;
+ case right:
+ ++pac->position.x;
+ return;
+ case up:
+ --pac->position.y;
+ return;
+ case down:
+ ++pac->position.y;
+ return;
+ default:
+ return;
+ }
+}