vooga.engine.control
Class Control

java.lang.Object
  extended by vooga.engine.control.Control
Direct Known Subclasses:
ControlExample, KeyboardControl, MouseControl

public class Control
extends java.lang.Object

Control class which can be extended to create control schemes. Keyboard and mouse control are already packaged subclasses, but this class can also be extended to control GameEntitySprites with an AI, joystick, network, etc.

Control takes one or more GameEntitySprites and registers actions that those sprites will perform if an event occurs from some predetermined input device. If the programmer is just trying to use the built in keyboard or mouse listening capabilties, they can do the following:

Control shipControl = new Control(this); //this is inside of a GameEntitySprite subclass. shipControl.addInput(KeyEvent.VK_LEFT, "left", "rotateLeft", "Ship", new Class[]{int.class}); //Registers the left key to trigger rotateLeft with an int parameter. shipControl.setParams(KeyEvent.VK_LEFT, 10); //Sets parameter of rotateLeft to 10. shipControl.act(); //Within Player's act method. Tells Control to check the registered events and see if any have occurred.


Field Summary
protected  java.util.List<java.lang.Object> entities
           
protected  int key
           
protected  java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.reflect.Method>> methodMap
           
protected  java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.reflect.Method[]>> methodParamMap
           
protected  Game myGame
           
protected  java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.Object[]>> paramMap
           
protected  java.lang.Class<?>[] paramTypes
           
 
Constructor Summary
Control()
          Default Control Constructor
Control(Game game)
          Control Constructor with only a Game declared
Control(java.util.List<java.lang.Object> players, Game game)
          Constructor that can add multiple players initially
Control(java.lang.Object initialPlayer, Game game)
          Constructor which can add an initial player to the scheme
 
Method Summary
 void addInput(int listen, java.lang.String method, java.lang.String classname, java.lang.Class<?>... paramTypes)
          Create keyset to map input to method.
 void changeKey(int previousKey, int newKey)
          Change the input corresponding to a set of actions to a different input.
 void initializeMappings()
          Initialize the mappings of inputs to methods and inputs to the methods parameters.
 void setParams(int listen, java.lang.Class<?> paramClass, java.lang.reflect.Method... paramValues)
          Sets the parameter values that need to be used for a method
 void setParams(int listen, java.lang.Object... paramValues)
          Sets the parameter values that need to be used for a method
 void update()
           
 void update(int key)
          Invoke methods here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

entities

protected java.util.List<java.lang.Object> entities

paramTypes

protected java.lang.Class<?>[] paramTypes

myGame

protected Game myGame

methodMap

protected java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.reflect.Method>> methodMap

paramMap

protected java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.Object[]>> paramMap

methodParamMap

protected java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.reflect.Method[]>> methodParamMap

key

protected int key
Constructor Detail

Control

public Control()
Default Control Constructor


Control

public Control(Game game)
Control Constructor with only a Game declared

Parameters:
game - The game which this Control object is a part of

Control

public Control(java.lang.Object initialPlayer,
               Game game)
Constructor which can add an initial player to the scheme

Parameters:
initialPlayer - First player to add to use this control scheme
game - The game which this Control object is a part of

Control

public Control(java.util.List<java.lang.Object> players,
               Game game)
Constructor that can add multiple players initially

Parameters:
players - Initial players to use this scheme
game - The game which this Control object is a part of
Method Detail

setParams

public void setParams(int listen,
                      java.lang.Object... paramValues)
Sets the parameter values that need to be used for a method

Parameters:
listen - The key which the method listens for
paramValues - The value of the parameters (eg. "Shoot", 10, etc.)

setParams

public void setParams(int listen,
                      java.lang.Class<?> paramClass,
                      java.lang.reflect.Method... paramValues)
Sets the parameter values that need to be used for a method

Parameters:
listen - The key which the method listens for
paramClass - The class which the method belongs to
paramValues - The method which can be used as the value of the parameter. The method MUST return an object needed for the method's parameters. (eg. getMouseX(), getCurrentLevel(), etc.)

update

public void update(int key)
Invoke methods here. Call method each time through game loop


update

public void update()

addInput

public void addInput(int listen,
                     java.lang.String method,
                     java.lang.String classname,
                     java.lang.Class<?>... paramTypes)
Create keyset to map input to method. Can be overwritten to create new control scheme

Parameters:
listen - Use an Integer version of what to listen to (eg. Java.awt.event.KeyEvent constants for "KEYBOARD" or Java.awt.event.MouseEvent constants for "MOUSE")
method - Name of method to map to (do not include brackets)
classname - Name of class that wants to use this (eg. "Player" or "Game") NOTE: You must put the class's fully qualified name (including its package) For example: if a class is called Test and is in the package cps108.games.example then the String here must be "cps108.games.example.Test"
paramTypes - Class type that the parameters must be for the method (previously set with setParams() method. For example: String.class, int.class, etc.

initializeMappings

public void initializeMappings()
Initialize the mappings of inputs to methods and inputs to the methods parameters.


changeKey

public void changeKey(int previousKey,
                      int newKey)
Change the input corresponding to a set of actions to a different input. Use this method to change moving left from the left arrow key to the 'A' key for instance.

Parameters:
previousKey - the previous input
newKey - the new input