This is the most complicated part of Pacman. Ghosts need to find a path to the player and also the four ghosts have different movement patterns.
Create a new Scene with CharacterBody2D as root
Add an AnimatedSprite2D rename to BodySprite
Add a Sprite2D rename to EyeSprite
Add an Area2D rename to Hitbox
For the default animation
Use Ghost_Body_01 and 02.png
Turn on Autoplay and Looping
Add a new Animation called scared
Use the blue ghost images
Set the texture to Ghost_Eyes_Right.png
For Hitbox CollisionShape2D
Choose a new RectangleShape2D
Set the size to 42px x 42px
Create a new Group called ghosts
Add this node to the group
Add a Script to the Ghost node
Setup and enum for the ghost types
Create export variables for:
type of ghost
corner position to run to
ghost colour
ghost speed
target to chase
Add references to the:
body sprite
eye sprite
Setup your state variables for:
the current path
if they are scared
their last direction
if they were eaten
their home position
the tunnel slowdown
Set the colour of the ghost
Start the walking animation
Store the starting position
Create a function to make the eyes
Look in the direction of travel
Both vertically and horizontally
Inky ghost works with Blinky to
Corner the player
Create a function to get access to
Create a function to find the position
This function returns Pacmans position
Set the default position and direction
Because target is a Node2D we must
Check if it has a current_direction variable
If it does we can store it in player_dir
Now set the player position
Based on the type of ghost
Blinky just chases the player directly
Pinky chases 4 tiles ahead of the player
Clyde runs away if player is too close
Inky works with Blinky to trap player
Otherwise default to player position
Create a function to update the ghosts path
Depending on what state they are in
First get the level script (main.gd)
Set a default destination
Change destination based on ghost state
Create a function to reset the game
Simply reload the current scene
In Godot, call_deferred is a safety mechanism. It tells the engine: "Don't run this function right now. Wait until you are completely finished with the current frame's work (physics, drawing, etc.), and then run it."
Think of it like a restaurant kitchen:
Without deferred: You (the waiter) run into the kitchen during the dinner rush and try to dismantle the stove while the chef is cooking. Chaos/Crash.
With deferred: You leave a note saying "Dismantle stove." The chef finishes cooking (the frame), serves the food, and then reads the note and dismantles the stove during the clean-up break. Safe.
Create a function to revive
Reset all the state variables
Include the same screen wrap function
That the player uses to pass through
Create a function for being eaten
Update eaten and scared states
We will return to this function to increase the score too...
Create a function to return to normal mode
Create a function for the scared state
Update the is_scared variable
Start a timer for 10 seconds
After 10 seconds return to normal
Calculate the current speed
Ghosts move slower through the tunnel
Ghosts move faster when eaten
Ghosts move slower when scared
When the ghosts move off-screen
Through the tunnel we have to move manually
If we are almost at the destination and
We are not eaten - get a new path
Connect the body entered signal
If the body is the Player and
If the ghost is not scared and not eaten yet
Add a new Node as child of Main
Place the ghosts in the starting position