title: Event Handling For Java Game Engine
body: <p>I'm writing a little game-engine in Java and want to know how to optimize my event-handling.</p>
  
  <p>For event-handling I have a class <code>EventManager</code>, here you can register a <code>EventHandler</code> (<code>EventHandler</code> is simply a flag-interface). The <code>EventManager</code> scans through all the methods of the <code>EventHandler</code> and when there is a method with the <code>@EventCallback</code> annotation and it has has exactly one Paramter with a type that extends <code>Event</code>, it adds this method to an Map : <code>Map&lt;Class&lt;? extends Event&gt;, Set&lt;Callback&gt;&gt;</code> (a <code>Callback</code> simply saves the <code>Method</code> and its <code>Object</code> so the <code>Method</code> can be invoked later).</p>
  
  <p>What i like about this kind of event-handling is, that you can name your handler-methods however you want and you can handle different events in the same class without having to implement many interfaces.</p>
  
  <p>A event-handling class would look like this:</p>
  
  <pre><code>class ExampleHandler implements EventHandler {
  
      ExampleHandler(Game game) {
         game.getEventManager().registerHandler(this);
      }
  
      @EventCallback
      public void exampleKeyCallback(KeyPressEvent evt) {
          //I only care about key-pressing, not key-releasing in this class
          System.out.println(evt.getKey() + " was pressed");  
      }
  
      @EventCallback
      public void i_can_name_this_method_however_i_want(PlayerDeathEvent evt) {
          System.out.println(evt.getPlayer().getName() + " died");
      }
  
  }
  </code></pre>
  
  <p>For example when my player dies it will send the <code>PlayerDeathEvent</code> to the <code>EventManager</code>:</p>
  
  <pre><code>//Inside the player-class
  public void update() {
     if (health &lt;= 0) {
        getGame().getEventHandler().fireEvent(new PlayerDeathEvent(this));
     }
  }
  </code></pre>
  
  <p>and the <code>EventHandler</code> will invoke all the callbacks for this <code>Event</code>:</p>
  
  <pre><code>//Inside the eventmanager-class
  
  private Map&lt;Class&lt;? extends Event&gt;, Set&lt;Callback&gt;&gt; eventCallbackMap = new HashMap&lt;&gt;();
  
  public void registerEventHandler(EventHandler evt) {
     // find all the @EventCallback-Methods and find the Class-Type of their Paramater, 
     // then add the Method to the Set of Methods for this Class of Events.
     // for example: exampleKeyCallback(KeyPressEvent)
     //    it will be added to the Set for KeyPressEvent.class
  }
  
  public void fireEvent(Event evt) {
     Set&lt;Callback&gt; callbacks = eventCallbackMap.get(evt.getClass());
  
     for (Callback callback : callbacks) {
        callback.invoke(evt);
     }
  }
  </code></pre>
  
  <p>Which invokes <code>i_can_name_this_method_however_i_want</code> with the <code>PlayerDeathEvent</code> and prints <code>"*Player-Name* died"</code> on the console.</p>
  
  <p>I want to know if this is a good way to handle events and if it will be fast enough for handling the Events in the Game or if i have to change it. And if I have to change it, how can i make it better?</p>