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
Constructors -
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
Cancels all scheduled events for an entity.void
cancelEvent
(ScheduledEvent event) Cancels the given scheduled eventscheduleEvent
(float delay, String eventName) Schedule an event with no arguments<T> ScheduledEvent
scheduleEvent
(float delay, String eventName, T arg0) Schedule an event with one argument<T0,
T1> ScheduledEvent scheduleEvent
(float delay, String eventName, T0 arg0, T1 arg1) Schedule an event with two arguments<T0,
T1, T2> ScheduledEvent scheduleEvent
(float delay, String eventName, T0 arg0, T1 arg1, T2 arg2) Schedule 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 argumentvoid
update()
Update the event handler, processing and triggering scheduled events that have reached their scheduled execution time.
-
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
-
scheduleEvent
Schedule an event with no arguments- Parameters:
delay
- delay before triggering event in secondseventName
- name of the event- Returns:
- the scheduled event
-
scheduleEvent
Schedule an event with one argument- Type Parameters:
T
- argument type- Parameters:
delay
- delay before triggering event in secondseventName
- name of the eventarg0
- arg to pass to event- Returns:
- the scheduled event
-
scheduleEvent
Schedule an event with two arguments- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1- Parameters:
delay
- delay before triggering event in secondseventName
- name of the eventarg0
- arg 0 to pass to eventarg1
- arg 1 to pass to event- Returns:
- the scheduled event
-
scheduleEvent
public <T0,T1, ScheduledEvent scheduleEventT2> (float delay, String eventName, T0 arg0, T1 arg1, T2 arg2) Schedule an event with three arguments- Type Parameters:
T0
- Type of arg 0T1
- Type of arg 1T2
- Type of arg 2- Parameters:
delay
- delay before triggering event in secondseventName
- name of the eventarg0
- arg 0 to pass to eventarg1
- arg 1 to pass to eventarg2
- arg 2 to pass to event- Returns:
- the scheduled event
-
update
public void update()Update the event handler, processing and triggering scheduled events that have reached their scheduled execution time.If there is no instance of
GameTime
available, this method does nothing. -
cancelEvent
Cancels the given scheduled event- Parameters:
event
- event to cancel
-
cancelAllEvents
public void cancelAllEvents()Cancels all scheduled events for an entity. -
getScheduledEventsSize
-