Game Development Reference
In-Depth Information
Figure 3-2.
NotificationsManager workflow
1.
An event happens during gameplay and is detected immediately by a class,
such as when the
Player
class adjusts player health through a C# property.
The object that
causes
or
detects
an event is known as the
Poster
. Once
the event occurs, the Poster proceeds to
post a notification
about it to the
NotificationsManager
object. The notification sent to the Manager by the
Poster is essentially a data structure containing event-specific information,
such as the
event type
. Event types may include
OnPlayerHealthChange
,
OnLevelCompleted
,
OnEnemyDestroyed
, and others. The types themselves will
vary from game to game, depending on the events you need.
2.
Any objects that must be notified about the occurrence of specific event
types, such as
OnPlayerHealthChange
, should register themselves as
Listeners with the
NotificationsManager
class. By registering itself as
a Listener for a specific event type, the class is effectively asking the
NotificationsManager to tell it about all matching event occurrences, as and
when they happen.
3.
Based on this, the NotificationsManager can perform its work effectively.
Specifically, on receiving an event notification from a Poster, the
NotificationsManager immediately notifies all registered Listeners for that
event. In this way, every Listener is always immediately notified about every
relevant event, and can respond as appropriate.