I think that we are making some good progress. Now that we have a place for the ghost to respawn we can make Pacman eat the ghost and have the ghost retreat to the ghost home and respawn back into the game. What we need to do is to get Pacman to collide with the ghost, which will be easy because we've already figured out how to do that with the pellets, so it'll pretty much be the same. Then, after we detect a collision with the ghost, we need to check to make sure the ghost was in FREIGHT mode or not. If he was in FREIGHT mode, then we need to change his mode to SPAWN mode. In SPAWN mode we give him the home we just created as his goal, and set his speed to be faster than his maximum speed, maybe 1.5 or 2 times. Then when he reaches his spawn point, he just returns back to CHASE or SCATTER mode. After a ghost has respawned, we don't return him to FREIGHT mode because a ghost can only be eaten once per power pellet. Also, a ghost can only enter SPAWN mode if he was first in FREIGHT mode.
We first need to define where the spawn node is located. We'll have it in the middle of the ghost home. This is the location the ghosts have to get to in order to return back to normal. To do that we'll first construct a spawn key. If you look at the home nodes that we added earlier then you'll see that the location I'm talking about is at location (2, 3) or column 2, row 3. So when we're constructing the key I want to give that coordinate added to the offset that we had to give the home nodes earlier. I know what you're thinking, I don't like all of these numbers here either, but we'll take care of that in a bit. We'll just put up with it for now.
Then once we have the key, which will give us the key in the nodesLUT dictionary for the correct node, we pass that node into the ghost's setSpawnNode method which we'll create next.
The setSpawnNode method just defines the spawn node that we want to use.
The spawn method sets our goal to be the location of that node.
The startSpawn method is similar to the startFreight method in that it checks to make sure we can start the SPAWN mode, and if so we increase the ghost's speed, set the goal for the spawn location and set the directionMethod since before the ghost was in FREIGHT mode and moving around randomly.
We'll create a new method here that will set the current mode to SPAWN only if the ghost is in FREIGHT mode.
In the update method we'll need to check to see when the ghost reaches the home so we can change his mode back.
In the Pacman class we'll create a new method that will check if Pacman is colliding with the ghost. This is the same logic we used when colliding with the pellets. In fact they are so similar that I created a new method called collideCheck that will take some entity (really anything with a position and a radius). We'll need to make the changes below to eatPellets and create a collideGhost which really just calls the collideCheck.
So similar to the pellets, we'll create a method that checks certain ghost events. Here we'll see if Pacman has collided with the ghost, and if so we'll check to see if the ghosts is in FREIGHT mode. If he is, then we start his spawn mode.
When running, eat a power pellet then collide with the ghost and you'll see that he'll quickly rush off to the middle of the screen. When he gets there he will go back to normal.