The Controller now takes in key presses.
- Each key is now registered in an Abstract Factory which takes in an action to perform. - Comments are added behind each function in to give some indication of how it could be extended/modified in the future. #story[1006]
This commit is contained in:
@@ -2,6 +2,8 @@ package seng302.gameController;
|
|||||||
|
|
||||||
import javafx.animation.AnimationTimer;
|
import javafx.animation.AnimationTimer;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
import seng302.gameController.Keys.ControlKey;
|
||||||
|
import seng302.gameController.Keys.KeyFactory;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@@ -11,6 +13,10 @@ import java.util.HashMap;
|
|||||||
public class InputChecker {
|
public class InputChecker {
|
||||||
private HashMap<String, Boolean> currentlyActiveKeys = new HashMap<>();
|
private HashMap<String, Boolean> currentlyActiveKeys = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller loop that detects key presses that runs parallel to the main scene.
|
||||||
|
* @param scene Scene the controller is to run in parallel with.
|
||||||
|
*/
|
||||||
public void runWithScene(Scene scene){
|
public void runWithScene(Scene scene){
|
||||||
scene.setOnKeyPressed(event -> {
|
scene.setOnKeyPressed(event -> {
|
||||||
String codeString = event.getCode().toString();
|
String codeString = event.getCode().toString();
|
||||||
@@ -26,16 +32,27 @@ public class InputChecker {
|
|||||||
new AnimationTimer() {
|
new AnimationTimer() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(long now) {
|
public void handle(long now) {
|
||||||
for (String key : InputKeys.listOfKeys){
|
for (String key: currentlyActiveKeys.keySet()){
|
||||||
if (removeActiveKey(key)) {
|
ControlKey controlKey = KeyFactory.getKey(key);
|
||||||
System.out.println(key);
|
if (controlKey != null){
|
||||||
|
controlKey.onAction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// for (String key : InputKeys.stringKeysMap.keySet()){
|
||||||
|
// if (removeActiveKey(key)) {
|
||||||
|
// System.out.println(key);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
}.start();
|
}.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removes a key from the active dictionary
|
||||||
|
* @param codeString string of the key press to remove
|
||||||
|
* @return whether or not the key has been removed or not.
|
||||||
|
*/
|
||||||
private boolean removeActiveKey(String codeString) {
|
private boolean removeActiveKey(String codeString) {
|
||||||
Boolean isActive = currentlyActiveKeys.get(codeString);
|
Boolean isActive = currentlyActiveKeys.get(codeString);
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package seng302.gameController;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class to store the basic keys used by this controller
|
|
||||||
*/
|
|
||||||
public class InputKeys {
|
|
||||||
protected static String zoomIn = "Z";
|
|
||||||
protected static String zoomOut = "X";
|
|
||||||
protected static String vmg = "SPACE";
|
|
||||||
protected static String sailsToggle = "SHIFT";
|
|
||||||
protected static String tackGybe = "Enter";
|
|
||||||
protected static String upWind = "PAGE_UP";
|
|
||||||
protected static String downWind = "Page_Down";
|
|
||||||
|
|
||||||
protected static List<String> listOfKeys = Arrays.asList(zoomIn, zoomOut, vmg,
|
|
||||||
sailsToggle, tackGybe, upWind, downWind);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key for the controller, part of the abstract factory KeyFactory
|
||||||
|
*/
|
||||||
|
public abstract class ControlKey {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private KeyCode keyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public ControlKey(String name, KeyCode keyCode){
|
||||||
|
this.name = name;
|
||||||
|
this.keyCode = keyCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What this key should do when the command is issued for it to do its job.
|
||||||
|
*/
|
||||||
|
public abstract void onAction();//may want to make it take in a visualiser and stuff in the future.
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to send downwind packet to server
|
||||||
|
*/
|
||||||
|
public class DownWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public DownWindKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for creating Keys, these could be predefined in the future.
|
||||||
|
*/
|
||||||
|
public class KeyFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Control Key incharge of a key press
|
||||||
|
* @param key key pressed (String value of KeyCode)
|
||||||
|
* @return the Control Key behaviour of the key pressed.
|
||||||
|
*/
|
||||||
|
public static ControlKey getKey(String key){
|
||||||
|
switch(key){
|
||||||
|
case "Z":
|
||||||
|
return new ZoomInKey("Z", KeyCode.Z);
|
||||||
|
case "X":
|
||||||
|
return new ZoomOutKey("X", KeyCode.X);
|
||||||
|
case "SPACE":
|
||||||
|
return new VMGKey("SPACE", KeyCode.SPACE);
|
||||||
|
case "SHIFT":
|
||||||
|
return new SailsToggleKey("SHIFT", KeyCode.SHIFT);
|
||||||
|
case "ENTER":
|
||||||
|
return new TackGybeKey("ENTER", KeyCode.ENTER);
|
||||||
|
case "PAGE_UP":
|
||||||
|
return new UpWindKey("PAGE_UP", KeyCode.PAGE_UP);
|
||||||
|
case "PAGE_DOWN":
|
||||||
|
return new DownWindKey("PAGE_DOWN", KeyCode.PAGE_DOWN);
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to toggle the sails
|
||||||
|
*/
|
||||||
|
public class SailsToggleKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public SailsToggleKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to toggle between tacking and gybing
|
||||||
|
*/
|
||||||
|
public class TackGybeKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public TackGybeKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to go upwind
|
||||||
|
*/
|
||||||
|
public class UpWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public UpWindKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to trigger auto VMG
|
||||||
|
*/
|
||||||
|
public class VMGKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public VMGKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to zoom into the game
|
||||||
|
*/
|
||||||
|
public class ZoomInKey extends ControlKey {
|
||||||
|
|
||||||
|
public ZoomInKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package seng302.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to zoom out of the game.
|
||||||
|
*/
|
||||||
|
public class ZoomOutKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
* @param keyCode key code for the key
|
||||||
|
*/
|
||||||
|
public ZoomOutKey(String name, KeyCode keyCode) {
|
||||||
|
super(name, keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user