group1.control
Class Controller

java.lang.Object
  extended by group1.control.Controller
All Implemented Interfaces:
java.awt.event.KeyListener, java.util.EventListener

public class Controller
extends java.lang.Object
implements java.awt.event.KeyListener

A class for a game controller.

TIP: When making the GUI, in one of the larger components you'll need to use:

addKeyListener(control);

setFocusable(true);

...for this class to work. That actually makes the GUI accept keyboard input, don'cha know.

This is written in a way to take better control of the keyboard events. It remembers whether certain keys are being pressed or have been hit.

Note that, although this class suggests purposes for each key, it it up to other classes to determine what each key will actually do.

Also note, it became necessary to determine whether the key is being held down or was hit. For instance, the character should only jump if the user hits "jump," not if "jump" is being held down.

--"is pressed" means that the key is being held down.

--"was hit" means that the key was newly pressed. This is only changed to true if the key was not being pressed before, and changes to false as soon as the key is released, or when another class asks this class if the key was hit.


Constructor Summary
Controller()
          Default constructor.
 
Method Summary
 boolean isActionPressed()
          Whether or not the action key is being pressed.
 boolean isDownPressed()
          Whether or not the down key is being pressed.
 boolean isJumpPressed()
          Whether or not the jump key is being pressed.
 boolean isLeftPressed()
          Whether or not the left key is being pressed.
 boolean isRightPressed()
          Whether or not the right key is being pressed.
 boolean isSelectPressed()
          Whether or not the select key is being pressed.
 boolean isStartPressed()
          Whether or not the start key is being pressed.
 boolean isUpPressed()
          Whether or not the up key is being pressed.
 void keyPressed(java.awt.event.KeyEvent e)
          keyPressed method to implement KeyListener.
 void keyReleased(java.awt.event.KeyEvent e)
          keyReleased method to implement KeyListener.
 void keyTyped(java.awt.event.KeyEvent e)
          keyTyped method to implement KeyListener.
 boolean wasActionHit()
          Whether or not the action key was hit.
 boolean wasDownHit()
          Whether or not the down key was hit.
 boolean wasJumpHit()
          Whether or not the jump key was hit.
 boolean wasLeftHit()
          Whether or not the left key was hit.
 boolean wasRightHit()
          Whether or not the right key was hit.
 boolean wasSelectHit()
          Whether or not the select key was hit.
 boolean wasStartHit()
          Whether or not the start key was hit.
 boolean wasUpHit()
          Whether or not the up key was hit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Controller

public Controller()
Default constructor.

Method Detail

isJumpPressed

public boolean isJumpPressed()
Whether or not the jump key is being pressed.

Returns:
Whether or not the action key is being pressed.

isActionPressed

public boolean isActionPressed()
Whether or not the action key is being pressed.

Returns:
Whether or not the action key is being pressed.

isStartPressed

public boolean isStartPressed()
Whether or not the start key is being pressed.

Returns:
Whether or not the start key is being pressed.

isSelectPressed

public boolean isSelectPressed()
Whether or not the select key is being pressed.

Returns:
Whether or not the select key is being pressed.

isUpPressed

public boolean isUpPressed()
Whether or not the up key is being pressed.

Returns:
Whether or not the up key is being pressed.

isDownPressed

public boolean isDownPressed()
Whether or not the down key is being pressed.

Returns:
Whether or not the down key is being pressed.

isLeftPressed

public boolean isLeftPressed()
Whether or not the left key is being pressed.

Returns:
Whether or not the left key is being pressed.

isRightPressed

public boolean isRightPressed()
Whether or not the right key is being pressed.

Returns:
Whether or not the right key is being pressed.

wasJumpHit

public boolean wasJumpHit()
Whether or not the jump key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the jump key was hit.

wasActionHit

public boolean wasActionHit()
Whether or not the action key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the action key was hit.

wasStartHit

public boolean wasStartHit()
Whether or not the start key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the start key was hit.

wasSelectHit

public boolean wasSelectHit()
Whether or not the select key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the select key was hit.

wasUpHit

public boolean wasUpHit()
Whether or not the up key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the up key was hit.

wasDownHit

public boolean wasDownHit()
Whether or not the down key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the down key was hit.

wasRightHit

public boolean wasRightHit()
Whether or not the right key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the right key was hit.

wasLeftHit

public boolean wasLeftHit()
Whether or not the left key was hit. (If true, this value is set to false for future calls.)

Returns:
Whether or not the left key was hit.

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
keyPressed method to implement KeyListener. Called whenever a key is pressed.

Note that keyPressed events are generated over and over. This is... bad for a game with frames and time clicks. Very bad. So, this method takes those events and simplifies them to more controlled boolean states.

Specified by:
keyPressed in interface java.awt.event.KeyListener
Parameters:
e - The KeyEvent caught.

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
keyReleased method to implement KeyListener. Called whenever a key is released.

Note that keyReleased events are generated only once. We use this to our advantage to keep control over the states of the keys.

Note also, that this method resets "was hit" values to false. Otherwise, it might cause problems if we don't call for a button hit, then call for it later. If a player actually taps the button so fast that a time click can't pick it up, then I don't think they could be human, anyway...

Specified by:
keyReleased in interface java.awt.event.KeyListener
Parameters:
e - The KeyEvent caught.

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
keyTyped method to implement KeyListener. Called whenever a key is typed. Not used, though.

Specified by:
keyTyped in interface java.awt.event.KeyListener
Parameters:
e - The KeyEvent caught.