|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object group1.control.Controller
public abstract class Controller
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 |
---|
protected Controller()
Method Detail |
---|
protected boolean isJumpPressed()
protected boolean isActionPressed()
protected boolean isStartPressed()
protected boolean isSelectPressed()
protected boolean isUpPressed()
protected boolean isDownPressed()
protected boolean isLeftPressed()
protected boolean isRightPressed()
protected boolean wasJumpHit()
protected boolean wasActionHit()
protected boolean wasStartHit()
protected boolean wasSelectHit()
protected boolean wasUpHit()
protected boolean wasDownHit()
protected boolean wasRightHit()
protected boolean wasLeftHit()
public void keyPressed(java.awt.event.KeyEvent e)
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.
keyPressed
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.public void keyReleased(java.awt.event.KeyEvent e)
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...
keyReleased
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |