group1.control
Class Controller

java.lang.Object
  extended by group1.control.Controller
All Implemented Interfaces:
java.awt.event.KeyListener, java.util.EventListener
Direct Known Subclasses:
JeffController, PauseController

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

A class for a generic game controller. Think of this as an unplugged NES controller. It accepts inputs, but those inputs don't mean anything by themselves. (Admittedly, not all keys will be used by all classes. But, using this setup makes it easier to make new classes. It also ensures that the game only uses a small set of keys, which can be helpful to the player.) (If we wanted to make a second controller, an easy method would be to extend this class and allow the changing of the selected key values, or to simply make another class almost exactly like this class: ControllerTwo, or something.)

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 the extending 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
protected Controller()
          Default constructor.
 
Method Summary
protected  boolean isActionPressed()
          Whether or not the action key is being pressed.
protected  boolean isDownPressed()
          Whether or not the down key is being pressed.
protected  boolean isJumpPressed()
          Whether or not the jump key is being pressed.
protected  boolean isLeftPressed()
          Whether or not the left key is being pressed.
protected  boolean isRightPressed()
          Whether or not the right key is being pressed.
protected  boolean isSelectPressed()
          Whether or not the select key is being pressed.
protected  boolean isStartPressed()
          Whether or not the start key is being pressed.
protected  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.
protected  boolean wasActionHit()
          Whether or not the action key was hit.
protected  boolean wasDownHit()
          Whether or not the down key was hit.
protected  boolean wasJumpHit()
          Whether or not the jump key was hit.
protected  boolean wasLeftHit()
          Whether or not the left key was hit.
protected  boolean wasRightHit()
          Whether or not the right key was hit.
protected  boolean wasSelectHit()
          Whether or not the select key was hit.
protected  boolean wasStartHit()
          Whether or not the start key was hit.
protected  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

protected Controller()
Default constructor.

Method Detail

isJumpPressed

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

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

isActionPressed

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

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

isStartPressed

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

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

isSelectPressed

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

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

isUpPressed

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

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

isDownPressed

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

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

isLeftPressed

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

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

isRightPressed

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

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

wasJumpHit

protected 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

protected 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

protected 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

protected 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

protected 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

protected 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

protected 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

protected 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.