Class EventHandler

java.lang.Object
com.csse3200.game.events.EventHandler

public class EventHandler extends Object
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 Details

    • EventHandler

      public EventHandler()
  • Method Details

    • addListener

      public void addListener(String eventName, EventListener0 listener)
      Add a listener to an event with zero arguments
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T> void addListener(String eventName, EventListener1<T> listener)
      Add a listener to an event with one argument
      Type Parameters:
      T - argument type
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T0, T1> void addListener(String eventName, EventListener2<T0,T1> listener)
      Add a listener to an event with two arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T0, T1, T2> void addListener(String eventName, EventListener3<T0,T1,T2> listener)
      Add a listener to an event with three arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      T2 - Type of arg 2
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • trigger

      public void trigger(String eventName)
      Trigger an event with no arguments
      Parameters:
      eventName - name of the event
    • trigger

      public <T> void trigger(String eventName, T arg0)
      Trigger an event with one argument
      Type Parameters:
      T - argument type
      Parameters:
      eventName - name of the event
      arg0 - arg to pass to event
    • trigger

      public <T0, T1> void trigger(String eventName, T0 arg0, T1 arg1)
      Trigger an event with one argument
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      Parameters:
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
    • trigger

      public <T0, T1, T2> void trigger(String eventName, T0 arg0, T1 arg1, T2 arg2)
      Trigger an event with one argument
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      T2 - Type of arg 2
      Parameters:
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
      arg2 - arg 2 to pass to event
    • scheduleEvent

      public ScheduledEvent scheduleEvent(float delay, String eventName)
      Schedule an event with no arguments
      Parameters:
      delay - delay before triggering event in seconds
      eventName - name of the event
      Returns:
      the scheduled event
    • scheduleEvent

      public <T> ScheduledEvent scheduleEvent(float delay, String eventName, T arg0)
      Schedule an event with one argument
      Type Parameters:
      T - argument type
      Parameters:
      delay - delay before triggering event in seconds
      eventName - name of the event
      arg0 - arg to pass to event
      Returns:
      the scheduled event
    • scheduleEvent

      public <T0, T1> ScheduledEvent scheduleEvent(float delay, String eventName, T0 arg0, T1 arg1)
      Schedule an event with two arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      Parameters:
      delay - delay before triggering event in seconds
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
      Returns:
      the scheduled event
    • scheduleEvent

      public <T0, T1, T2> ScheduledEvent scheduleEvent(float delay, String eventName, T0 arg0, T1 arg1, T2 arg2)
      Schedule an event with three arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      T2 - Type of arg 2
      Parameters:
      delay - delay before triggering event in seconds
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
      arg2 - 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

      public void cancelEvent(ScheduledEvent event)
      Cancels the given scheduled event
      Parameters:
      event - event to cancel
    • cancelAllEvents

      public void cancelAllEvents()
      Cancels all scheduled events for an entity.
    • getScheduledEventsSize

      public Integer getScheduledEventsSize()