Game Development Reference
In-Depth Information
(left mouse button) rather than an abstract action (such as fire weapon). What if
oneplayerwantstofirehisweaponwiththespacebarinsteadoftheleftmousebut-
ton? Any firing code that was registered directly to the left mouse button wouldn't
work properly in this case. So an ideal input system would allow for abstract ac-
tions that can be bound to specific buttons.
Another issue is that the event system described does not allow any one registered
party to prevent any other parties from receiving the event. Suppose you are mak-
ing a simple RTS (such as the tower defense game in Chapter 14 , Sample Game:
Tower Defense for PC/Mac ”). If such a game were on a PC, it would make sense
that a mouse click could be directed either at the user interface (the menus) or at
the game world itself (selecting units). Because the UI is displayed on top of the
game world, the UI needs to have first priority on mouse clicks. This means when
a mouse click occurs, the UI must first inspect it and determine whether the click
is directed at the UI. If it is, then that click event should not be dispatched to the
game world. But in our current input system, both the game world and UI would
receive the mouse click, which could potentially cause unintended behavior.
A More Complex System
The system covered in this section does not allow direct registration of a function
to a specific event. It instead creates a list of active input actions that can then be
queried by interested parties. So while perhaps it is not an event-based system in
the strict definition, it is fairly similar. It also happens to be that the system out-
lined here is very similar to the one used in Chapter 14 ' s tower defense game.
The basic premise of this system is that there is a map of key bindings. Recall
that a map is a set of (key, value) pairs, which, depending on the type of map,
may or may not be sorted. In this system, the key is the name of the binding (such
as “Fire”), and the value is information about that binding such as what key it is
mapped to and when it gets triggered (just pressed, still pressed, and so on). Be-
cause we want to support multiple bindings for actions (so “Fire” can be the left
mouse button or the spacebar), we'd ideally select a type of map that does allow
duplicate entries with the same key.
Every frame, the input system goes through the map of key bindings and determ-
ines which bindings (if any) are active. The active bindings for that specific frame
are then stored in an “active” bindings map. This map of active bindings is first
provided to the UI system, which will go through and determine whether there are
any actions that affect the UI. If there are such actions, they will be removed from
the active bindings map and processed. Any remaining bindings are then passed
Search WWH ::




Custom Search