Package com.csse3200.game.events
Class EventHandler
java.lang.Object
com.csse3200.game.events.EventHandler
Send and receive events between objects. EventHandler provides an implementation of the Observer
pattern, also known as an event system or publish/subscribe. When an event is triggered with
trigger(), all listeners are notified of the event.
Currently supports up to 3 arguments for an event. More can be added, but consider instead passing a class with required fields.
If you get a ClassCastException from an event, trigger is being called with different arguments than the listeners expect.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(String eventName, EventListener0 listener) Add a listener to an event with zero arguments<T> void
addListener
(String eventName, EventListener1<T> listener) Add a listener to an event with one argument<T0,
T1> void addListener
(String eventName, EventListener2<T0, T1> listener) Add a listener to an event with two arguments<T0,
T1, T2> void addListener
(String eventName, EventListener3<T0, T1, T2> listener) Add a listener to an event with three argumentsvoid
Trigger an event with no arguments<T> void
Trigger an event with one argument<T0,
T1> void Trigger an event with one argument<T0,
T1, T2> void Trigger an event with one argument
-
Constructor Details
-
EventHandler
public EventHandler()
-
-
Method Details
-
addListener
Add a listener to an event with zero arguments- Parameters:
eventName
- name of the eventlistener
- function to call when event fires
-
addListener
Add a listener to an event with one argument- Type Parameters:
T
- argument type- Parameters:
eventName
- name of the eventlistener
- function to call when event fires
-
addListener
Add a listener to an event with two arguments- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1- Parameters:
eventName
- name of the eventlistener
- function to call when event fires
-
addListener
Add a listener to an event with three arguments- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1T2
- Type of arg 2- Parameters:
eventName
- name of the eventlistener
- function to call when event fires
-
trigger
Trigger an event with no arguments- Parameters:
eventName
- name of the event
-
trigger
Trigger an event with one argument- Type Parameters:
T
- argument type- Parameters:
eventName
- name of the eventarg0
- arg to pass to event
-
trigger
Trigger an event with one argument- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1- Parameters:
eventName
- name of the eventarg0
- arg 0 to pass to eventarg1
- arg 1 to pass to event
-
trigger
Trigger an event with one argument- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1T2
- Type of arg 2- Parameters:
eventName
- name of the eventarg0
- arg 0 to pass to eventarg1
- arg 1 to pass to eventarg2
- arg 2 to pass to event
-