3.4) Maze-Data: Layout and Directions
Since this script was designed in the early days and JavaScript as well as computers were rather slow, it was decissivly important to save as much runtime as possible. Since this is not a bad habit, we are going a bit into details:
First, it would be really bad, if any character moving would have to calculate a 'wish'-direction, then look up the maze map, recalculate again, if there was a wall and not a passage, and so on. So it would be nice to have some map encoding any possible direction for the exact position in a handy way.
So we're coming up with two types of maze data to store:
one for the layout of the display
and another encoding the direction information
In fact we could calculate the second from the first one, but this would use some extra time for the game to come up. So we have two sets of arrays for every level:
the border layout (+ the placing of pills, food, and empty spaces)
the directions map
The layout map is quite trivial: We use any encoding (here alphabetic characters) referring to our border-tiles. Further a simple blank will refer to a normal passages, a 'p' to pills, and a 'x' to empty spaces (mainly the teleport passages and the pacman's start position). Any normal passage will be inhabited by a food (we'll have to count this at level set up).
For the directions it might be wise to invest some more consideration.
We'll find that, if a character is passing through a straight passage, it just has to move further (or possibly reverse its direction). So if a character is on its way, we don't have to consider any directions to be encoded in the map. We just have to know that there is no crossing.
So all information to be handled are the crossings' possible connections.
On a 2D surface (as our maze obviously is) there are 4 possible directions. So we could encode them using binary numbers as a 4-bit vector (one bit for every possible direction).
Here we define (as powers of 2):