Renderer/src/main/java/electrosphere/controls/ControlHandler.java
2021-07-10 00:01:04 -04:00

455 lines
22 KiB
Java

package electrosphere.controls;
import electrosphere.entity.CameraEntityUtils;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.MovementTree;
import electrosphere.entity.state.MovementTree.MovementTreeState;
import electrosphere.main.Globals;
import electrosphere.menu.MenuTransition;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.util.Utilities;
import java.util.HashMap;
import java.util.List;
import org.joml.Vector2f;
import org.joml.Vector3f;
import static org.lwjgl.glfw.GLFW.GLFW_CURSOR;
import static org.lwjgl.glfw.GLFW.GLFW_CURSOR_DISABLED;
import static org.lwjgl.glfw.GLFW.GLFW_CURSOR_HIDDEN;
import static org.lwjgl.glfw.GLFW.GLFW_CURSOR_NORMAL;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_0;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_1;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_2;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_3;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_4;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_5;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_6;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_7;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_8;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_9;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_BACKSPACE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_D;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_DOWN;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ENTER;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_CONTROL;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_PERIOD;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_UP;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_W;
import static org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_LEFT;
import static org.lwjgl.glfw.GLFW.GLFW_PRESS;
import static org.lwjgl.glfw.GLFW.glfwGetCursorPos;
import static org.lwjgl.glfw.GLFW.glfwGetKey;
import static org.lwjgl.glfw.GLFW.glfwGetMouseButton;
import static org.lwjgl.glfw.GLFW.glfwSetInputMode;
/**
*
* @author amaterasu
*/
public class ControlHandler {
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD = "moveForward";
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD = "moveBackward";
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_LEFT = "moveLeft";
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT = "moveRight";
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_JUMP = "jump";
public static final String DATA_STRING_INPUT_CODE_MOVEMENT_FALL = "fall";
public static final String DATA_STRING_INPUT_CODE_ATTACK_PRIMARY = "attackPrimary";
public static final String DATA_STRING_INPUT_CODE_MENU_INCREMENT = "menuIncrement";
public static final String DATA_STRING_INPUT_CODE_MENU_DECREMENT = "menuDecrement";
public static final String DATA_STRING_INPUT_CODE_MENU_SELECT = "menuSelect";
public static final String DATA_STRING_INPUT_CODE_MENU_BACKOUT = "menuBackout";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_BACKSPACE = "menuTypeBackspace";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_0 = "menuType0";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_1 = "menuType1";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_2 = "menuType2";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_3 = "menuType3";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_4 = "menuType4";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_5 = "menuType5";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_6 = "menuType6";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_7 = "menuType7";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_8 = "menuType8";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_9 = "menuType9";
public static final String DATA_STRING_INPUT_CODE_MENU_TYPE_PERIOD = "menuType.";
public static enum ControlsState {
TITLE_PAGE,
TITLE_MENU,
MAIN_GAME,
NO_INPUT,
}
ControlsState state = ControlsState.TITLE_MENU;
boolean mouseIsVisible = true;
HashMap<String, Control> controls;
ControlHandler(){
controls = new HashMap();
}
public static ControlHandler generateExampleControlsMap(){
ControlHandler handler = new ControlHandler();
/*
Main Game controls
*/
/*
Map the controls
*/
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD, new Control(true,false,GLFW_KEY_W));
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD, new Control(true,false,GLFW_KEY_S));
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT, new Control(true,false,GLFW_KEY_A));
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT, new Control(true,false,GLFW_KEY_D));
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_JUMP, new Control(true,false,GLFW_KEY_SPACE));
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_FALL, new Control(true,false,GLFW_KEY_LEFT_CONTROL));
handler.addControl(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY, new Control(false,true,GLFW_MOUSE_BUTTON_LEFT));
/*
Map the menu navigation controls
*/
handler.addControl(DATA_STRING_INPUT_CODE_MENU_INCREMENT, new Control(true,false,GLFW_KEY_DOWN));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_DECREMENT, new Control(true,false,GLFW_KEY_UP));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_SELECT, new Control(true,false,GLFW_KEY_ENTER));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_BACKOUT, new Control(true,false,GLFW_KEY_ESCAPE));
/*
Map the typing controls
*/
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_BACKSPACE, new Control(true,false,GLFW_KEY_BACKSPACE));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_0, new Control(true,false,GLFW_KEY_0));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_1, new Control(true,false,GLFW_KEY_1));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_2, new Control(true,false,GLFW_KEY_2));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_3, new Control(true,false,GLFW_KEY_3));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_4, new Control(true,false,GLFW_KEY_4));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_5, new Control(true,false,GLFW_KEY_5));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_6, new Control(true,false,GLFW_KEY_6));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_7, new Control(true,false,GLFW_KEY_7));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_8, new Control(true,false,GLFW_KEY_8));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_9, new Control(true,false,GLFW_KEY_9));
handler.addControl(DATA_STRING_INPUT_CODE_MENU_TYPE_PERIOD, new Control(true,false,GLFW_KEY_PERIOD));
/*
set state
*/
handler.setHandlerState(ControlsState.TITLE_MENU);
/*
Save to file
*/
// Utilities.saveObjectToBakedJsonFile("/Config/keybinds.json", handler);
/*
return
*/
return handler;
}
public void pollControls(){
switch(state){
case MAIN_GAME:
pollMainGameControls();
break;
case TITLE_MENU:
pollMenuNavigationControls();
/*
Typing..
*/
pollTypingControls();
break;
case NO_INPUT:
break;
}
}
public void pollMainGameControls(){
if(Globals.playerCharacter != null){
MovementTree movementTree = CreatureUtils.getEntityMovementTree(Globals.playerCharacter);
AttackTree attackTree = CreatureUtils.getAttackTree(Globals.playerCharacter);
Vector3f cameraEyeVector = CameraEntityUtils.getCameraEye(Globals.playerCamera);
/*
Move forward
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD)){
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).getKeyValue()) == GLFW_PRESS){
CreatureUtils.setMovementVector(Globals.playerCharacter, new Vector3f(-cameraEyeVector.x,0,-cameraEyeVector.z).normalize());
if(movementTree.getState()==MovementTreeState.IDLE || movementTree.getState()==MovementTreeState.SLOWDOWN){
movementTree.start();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).setState(true);
//send to server
// Vector3f position = EntityUtils.getEntityPosition(Globals.playerCharacter);
// EntityMessage outgoingMessage = EntityMessage.constructMoveMessage(
// Globals.playerCharacter.getId(),
// System.currentTimeMillis(),
// position.x,
// position.y,
// position.z
// );
// Globals.clientConnection.queueOutgoingMessage(outgoingMessage);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).isState() == true){
movementTree.slowdown();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).setState(false);
}
}
/*
Move backward
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD)){
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD).getKeyValue()) == GLFW_PRESS){
CreatureUtils.setMovementVector(Globals.playerCharacter, new Vector3f(cameraEyeVector.x,0,cameraEyeVector.z).normalize());
if(movementTree.getState()==MovementTreeState.IDLE || movementTree.getState()==MovementTreeState.SLOWDOWN){
movementTree.start();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD).setState(true);
//send to server
// Vector3f position = EntityUtils.getEntityPosition(Globals.playerCharacter);
// EntityMessage outgoingMessage = EntityMessage.constructMoveMessage(
// Globals.playerCharacter.getId(),
// System.currentTimeMillis(),
// position.x,
// position.y,
// position.z
// );
// Globals.clientConnection.queueOutgoingMessage(outgoingMessage);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD).isState() == true){
movementTree.slowdown();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_BACKWARD).setState(false);
}
}
/*
Move left
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT)){
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT).getKeyValue()) == GLFW_PRESS){
CreatureUtils.setMovementVector(Globals.playerCharacter, new Vector3f(-cameraEyeVector.x,0,-cameraEyeVector.z).rotateY((float)(90 * Math.PI / 180)).normalize());
if(movementTree.getState()==MovementTreeState.IDLE || movementTree.getState()==MovementTreeState.SLOWDOWN){
movementTree.start();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT).setState(true);
//send to server
// Vector3f position = EntityUtils.getEntityPosition(Globals.playerCharacter);
// EntityMessage outgoingMessage = EntityMessage.constructMoveMessage(
// Globals.playerCharacter.getId(),
// System.currentTimeMillis(),
// position.x,
// position.y,
// position.z
// );
// Globals.clientConnection.queueOutgoingMessage(outgoingMessage);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT).isState() == true){
movementTree.slowdown();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_LEFT).setState(false);
}
}
/*
Move right
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT)){
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT).getKeyValue()) == GLFW_PRESS){
CreatureUtils.setMovementVector(Globals.playerCharacter, new Vector3f(-cameraEyeVector.x,0,-cameraEyeVector.z).rotateY((float)(-90 * Math.PI / 180)).normalize());
if(movementTree.getState()==MovementTreeState.IDLE || movementTree.getState()==MovementTreeState.SLOWDOWN){
movementTree.start();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT).setState(true);
//send to server
// Vector3f position = EntityUtils.getEntityPosition(Globals.playerCharacter);
// EntityMessage outgoingMessage = EntityMessage.constructMoveMessage(
// Globals.playerCharacter.getId(),
// System.currentTimeMillis(),
// position.x,
// position.y,
// position.z
// );
// Globals.clientConnection.queueOutgoingMessage(outgoingMessage);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT).isState() == true){
movementTree.slowdown();
}
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_RIGHT).setState(false);
}
}
/*
Move up
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_JUMP) && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_JUMP).getKeyValue()) == GLFW_PRESS){
EntityUtils.getPosition(Globals.playerCharacter).add(new Vector3f(0,0.6f,0).mul(1f));
}
/*
Move down
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_MOVEMENT_FALL) && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FALL).getKeyValue()) == GLFW_PRESS){
EntityUtils.getPosition(Globals.playerCharacter).add(new Vector3f(0,-0.6f,0).mul(1f));
}
/*
Attack
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY)){
if(controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).isIsMouse() && glfwGetMouseButton(Globals.window, controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).getKeyValue()) == GLFW_PRESS){
if(controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).isState() == false){
if(attackTree != null){
attackTree.start(EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND);
}
}
controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).isState() == true){
}
controls.get(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY).setState(false);
}
}
}
}
public void pollMenuNavigationControls(){
if(controls.containsKey(DATA_STRING_INPUT_CODE_MENU_INCREMENT)){
if(controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).getKeyValue()) == GLFW_PRESS){
controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).isState() == true){
Globals.currentMenu.incrementMenuOption();
}
controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).setState(false);
}
}
if(controls.containsKey(DATA_STRING_INPUT_CODE_MENU_DECREMENT)){
if(controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MENU_DECREMENT).getKeyValue()) == GLFW_PRESS){
controls.get(DATA_STRING_INPUT_CODE_MENU_DECREMENT).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MENU_DECREMENT).isState() == true){
Globals.currentMenu.decrementMenuOption();
}
controls.get(DATA_STRING_INPUT_CODE_MENU_DECREMENT).setState(false);
}
}
if(controls.containsKey(DATA_STRING_INPUT_CODE_MENU_SELECT)){
if(controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MENU_SELECT).getKeyValue()) == GLFW_PRESS){
controls.get(DATA_STRING_INPUT_CODE_MENU_SELECT).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MENU_SELECT).isState() == true){
MenuTransition.selectOption(Globals.currentMenu);
}
controls.get(DATA_STRING_INPUT_CODE_MENU_SELECT).setState(false);
}
}
if(controls.containsKey(DATA_STRING_INPUT_CODE_MENU_BACKOUT)){
if(controls.get(DATA_STRING_INPUT_CODE_MENU_INCREMENT).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MENU_BACKOUT).getKeyValue()) == GLFW_PRESS){
controls.get(DATA_STRING_INPUT_CODE_MENU_BACKOUT).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_MENU_BACKOUT).isState() == true){
MenuTransition.backout(Globals.currentMenu);
}
controls.get(DATA_STRING_INPUT_CODE_MENU_BACKOUT).setState(false);
}
}
}
public void pollTypingControls(){
String[] typeKeybinds = {
DATA_STRING_INPUT_CODE_MENU_TYPE_BACKSPACE,
DATA_STRING_INPUT_CODE_MENU_TYPE_0,
DATA_STRING_INPUT_CODE_MENU_TYPE_1,
DATA_STRING_INPUT_CODE_MENU_TYPE_2,
DATA_STRING_INPUT_CODE_MENU_TYPE_3,
DATA_STRING_INPUT_CODE_MENU_TYPE_4,
DATA_STRING_INPUT_CODE_MENU_TYPE_5,
DATA_STRING_INPUT_CODE_MENU_TYPE_6,
DATA_STRING_INPUT_CODE_MENU_TYPE_7,
DATA_STRING_INPUT_CODE_MENU_TYPE_8,
DATA_STRING_INPUT_CODE_MENU_TYPE_9,
DATA_STRING_INPUT_CODE_MENU_TYPE_PERIOD,
};
for(String currentKey : typeKeybinds){
if(controls.get(currentKey).isIsKey() && glfwGetKey(Globals.window, controls.get(currentKey).getKeyValue()) == GLFW_PRESS){
controls.get(currentKey).setState(true);
} else {
if(controls.get(currentKey).isState() == true){
MenuTransition.menuType(Globals.currentMenu,currentKey);
}
controls.get(currentKey).setState(false);
}
}
}
public Control getControl(String controlName){
return controls.get(controlName);
}
public boolean containsControl(String controlName){
return controls.containsKey(controlName);
}
public void removeControl(String controlName){
controls.remove(controlName);
}
public void addControl(String controlName, Control c){
controls.put(controlName, c);
}
public void setHandlerState(ControlsState state){
this.state = state;
}
public ControlsState getState(){
return state;
}
public void hideMouse(){
glfwSetInputMode(Globals.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
mouseIsVisible = false;
}
public void showMouse(){
glfwSetInputMode(Globals.window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
mouseIsVisible = true;
}
public boolean isMouseVisible(){
return mouseIsVisible;
}
public Vector2f getMousePosition(){
double posX[] = new double[1];
double posY[] = new double[1];
glfwGetCursorPos(Globals.window, posX, posY);
Vector2f rVal = new Vector2f((float)posX[0],(float)posY[0]);
return rVal;
}
}