Package org.filsa.nikujaga.pong

Source Code of org.filsa.nikujaga.pong.InputHandler

package org.filsa.nikujaga.pong;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Map;

import org.filsa.nikujaga.pong.Keys.Key;

public class InputHandler implements KeyListener {
  private Map<Integer, Key> mappings = new HashMap<Integer, Key>();

  public InputHandler(Keys keys) {
    mappings.put(KeyEvent.VK_UP, keys.p2_up);
    mappings.put(KeyEvent.VK_DOWN, keys.p2_down);

    mappings.put(KeyEvent.VK_NUMPAD8, keys.p2_up);
    mappings.put(KeyEvent.VK_NUMPAD5, keys.p2_down);
    mappings.put(KeyEvent.VK_NUMPAD9, keys.p2_up);
    mappings.put(KeyEvent.VK_NUMPAD6, keys.p2_down);

    mappings.put(KeyEvent.VK_W, keys.p1_up);
    mappings.put(KeyEvent.VK_S, keys.p1_down);
    mappings.put(KeyEvent.VK_Q, keys.p1_up);
    mappings.put(KeyEvent.VK_A, keys.p1_down);

    mappings.put(KeyEvent.VK_SPACE, keys.pause);
    mappings.put(KeyEvent.VK_ESCAPE, keys.pause);
  }

  public void keyPressed(KeyEvent ke) {
    toggle(ke, true);
  }

  public void keyReleased(KeyEvent ke) {
    toggle(ke, false);
  }

  public void keyTyped(KeyEvent ke) {
  }

  private void toggle(KeyEvent ke, boolean state) {
    Key key = mappings.get(ke.getKeyCode());
    if (key != null) {
      key.nextState = state;
    }
  }
}
TOP

Related Classes of org.filsa.nikujaga.pong.InputHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.