HTML and CSS Reference
In-Depth Information
You will need to create a new class, called input.ts , in your rogue folder. As before, add to it the following code:
module rogue.input {
This time, though, instead of an interface, you are going to create something called an enum (enumerator).
Enums allow you to store name-value pairs in easy-to-reference lookup tables. Here is your first enum:
export enum Keyboard {
If you have ever worked with keyboard events, you know that you must reference their values. As you can see, you
store each key value in this enum so that you can reference, for instance, the left key as Keyboard.LEFT . This is a lot
better than referencing it directly, by its value of 37. Then, you will need a second lookup table of points that represent
the directions in which the player can move. Unfortunately, enums can only contain numbers, and you will want
your reference object to have points. Luckily, you can still create something very similar to an enum, but as a generic
object, like so:
export var Directions = {
UP: new geom.Point(0, -1),
DOWN: new geom.Point(0, 1),
RIGHT: new geom.Point(1, 0),
LEFT: new geom.Point(-1, 0)
Here, you are setting up points to represent directions in which the player will move. Figure 19-16 provides a
handy reference to what this actually looks like on a grid:
Figure 19-16. As you can see, you simply do math on the x, y position in order to move the player up, down, left,
or right
Search WWH ::

Custom Search