For the input we use the mechanism from the prototype, it's perfectly fine. As mentioned earlier we use a bitset to know if the user keeps a key being pressed.
I modified the code a bit so that we can add and remove the listener. The listener will have to be removed once the game is over.
A dedicated class for the input handler allows us to create another instance of it. This way it should be easy to assign another instance to a second player which gives us the freedom of creating a multiplayer game with only a few additional lines of code.
I'll leave the key codes hardcoded for now. In the end we'll use custom configurations per player.
Here's the keyboard handler:
public class Input {
/**
* Bitset which registers if any {@link KeyCode} keeps being pressed or if it is released.
*
* Note: ordinal may be not appropriate, but we don't have a method in {@link KeyCode} to get the int code, so it'll have to do
*/
private BitSet keyboardBitSet = new BitSet();
// -------------------------------------------------
// default key codes
// we will vary them later when we let the user customize the key codes or when we add support for a 2nd player
// -------------------------------------------------
private KeyCode upKey = KeyCode.UP;
private KeyCode downKey = KeyCode.DOWN;
private KeyCode leftKey = KeyCode.LEFT;
private KeyCode rightKey = KeyCode.RIGHT;
private KeyCode primaryWeaponKey = KeyCode.SPACE;
private KeyCode secondaryWeaponKey = KeyCode.CONTROL;
Scene scene;
public Input( Scene scene) {
this.scene = scene;
}
public void addListeners() {
scene.addEventFilter(KeyEvent.KEY_PRESSED, keyPressedEventHandler);
scene.addEventFilter(KeyEvent.KEY_RELEASED, keyReleasedEventHandler);
}
public void removeListeners() {
scene.removeEventFilter(KeyEvent.KEY_PRESSED, keyPressedEventHandler);
scene.removeEventFilter(KeyEvent.KEY_RELEASED, keyReleasedEventHandler);
}
/**
* "Key Pressed" handler for all input events: register pressed key in the bitset
*/
private EventHandler keyPressedEventHandler = new EventHandler() {
@Override
public void handle(KeyEvent event) {
// register key down
keyboardBitSet.set(event.getCode().ordinal(), true);
}
};
/**
* "Key Released" handler for all input events: unregister released key in the bitset
*/
private EventHandler keyReleasedEventHandler = new EventHandler() {
@Override
public void handle(KeyEvent event) {
// register key up
keyboardBitSet.set(event.getCode().ordinal(), false);
}
};
// -------------------------------------------------
// Evaluate bitset of pressed keys and return the player input.
// If direction and its opposite direction are pressed simultaneously, then the direction isn't handled.
// -------------------------------------------------
public boolean isMoveUp() {
return keyboardBitSet.get( upKey.ordinal()) && !keyboardBitSet.get( downKey.ordinal());
}
public boolean isMoveDown() {
return keyboardBitSet.get( downKey.ordinal()) && !keyboardBitSet.get( upKey.ordinal());
}
public boolean isMoveLeft() {
return keyboardBitSet.get( leftKey.ordinal()) && !keyboardBitSet.get( rightKey.ordinal());
}
public boolean isMoveRight() {
return keyboardBitSet.get( rightKey.ordinal()) && !keyboardBitSet.get( leftKey.ordinal());
}
public boolean isFirePrimaryWeapon() {
return keyboardBitSet.get( primaryWeaponKey.ordinal());
}
public boolean isFireSecondaryWeapon() {
return keyboardBitSet.get( secondaryWeaponKey.ordinal());
}
// -------------------------------------------------
// getters & setters for key code modification (more players, user key settings change)
// -------------------------------------------------
}
ps: blogspot seems to modify the code, so please ignore the </keyevent> text and replace keyevent with KeyEvent; I'll put up the code elsewhere later.
No comments:
Post a Comment