Browse Source

KB controls

master
Mathieu Sérandour 2 years ago
parent
commit
5e5c87ce56
  1. 6
      controls.properties
  2. 19
      src/client/events/Events.java
  3. 138
      src/client/events/KBControls.java

6
controls.properties

@ -0,0 +1,6 @@
#keyboard controls file
#Sat Feb 27 11:58:30 CET 2021
up=87
right=68
left=65
down=83

19
src/client/events/Events.java

@ -8,6 +8,7 @@ import client.graphics.hud.HUDMain;
import game.Game;
import game.entities.MoveableEntity;
import math.vec.Vec2;
import static client.events.KBControls.*;
/**
*
@ -17,12 +18,12 @@ import math.vec.Vec2;
* handle every events that come from the player (keyboard, mouse etc.)
*/
public class Events extends EventHandler {
private MoveableEntity player;
private final Window window;
private MoveableEntity player;
private final Window window;
private Vec2 playerDirection = new Vec2();
private Vec2 playerDirection = new Vec2();
public static final float SPEED = .005f;
public static final float SPEED = .005f;
/**
*
@ -61,19 +62,19 @@ public class Events extends EventHandler {
private void checkKeyboard() {
if (keyboard[GLFW.GLFW_KEY_ESCAPE])
window.shouldClose();
//System.out.println()
// System.out.println()
if (player == null)
return;
playerDirection.reset();
if (keyboard[kbcontrols.getUPKey()])
if (keyboard[kbcontrols.getKey(UP)])
playerDirection.vals[1] += SPEED;
if (keyboard[kbcontrols.getDOWNKey()])
if (keyboard[kbcontrols.getKey(DOWN)])
playerDirection.vals[1] -= SPEED;
if (keyboard[kbcontrols.getRIGHTKey()])
if (keyboard[kbcontrols.getKey(RIGHT)])
playerDirection.vals[0] += SPEED;
if (keyboard[kbcontrols.getLEFTKey()])
if (keyboard[kbcontrols.getKey(LEFT)])
playerDirection.vals[0] -= SPEED;
if (playerDirection.isZero()) {

138
src/client/events/KBControls.java

@ -1,21 +1,141 @@
package client.events;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import org.lwjgl.glfw.GLFW;
public class KBControls {
public int getUPKey() {
return GLFW.GLFW_KEY_W;
import game.Game;
/**
*
* @author Mathieu
*
*
* class to load, acces, modify and save keyboard controls
*/
public final class KBControls {
public static final String UP = "up";
public static final String DOWN = "down";
public static final String LEFT = "left";
public static final String RIGHT = "right";
private final TreeMap<String, Integer> keys;
private static final String PROPERTIES_FILENAME = "controls.properties";
/**
* create controls by loading the PROPERTIES_FILENAME file
*/
public KBControls() {
keys = loadKeysFromPropertiesFile(PROPERTIES_FILENAME);
}
/**
* save keys to the disk
*/
public void save() {
saveProperties();
}
/**
*
* @param action
* one of the actions that are declared public final on the class
* head
* @return the string name of the key associated with the given action
*/
public String getKeyNameForAction(String action) {
return GLFW.glfwGetKeyName(getKey(action), 0);
}
/**
*
* @param action
* one of the actions that are declared public final on the class
* head
* @return the key associated with the given action
*/
public int getKey(String action) {
return keys.get(action);
}
public int getDOWNKey() {
return GLFW.GLFW_KEY_S;
/**
* sets a key for an action
*
* @param action
* the given action
* @param key
* the key to be associated with the action
*/
public void setKey(String action, int key) {
keys.put(action, key);
}
public int getLEFTKey() {
return GLFW.GLFW_KEY_A;
/**
* loads default values for all keys
*/
public void loadDefaultValues() {
setKey(UP, GLFW.GLFW_KEY_W);
setKey(DOWN, GLFW.GLFW_KEY_S);
setKey(LEFT, GLFW.GLFW_KEY_A);
setKey(RIGHT, GLFW.GLFW_KEY_D);
}
public int getRIGHTKey() {
return GLFW.GLFW_KEY_D;
/// private methods
private TreeMap<String, Integer> loadKeysFromPropertiesFile(String filename) {
Properties prop = new Properties();
try (InputStream is = new FileInputStream(filename)) {
prop.load(is);
return fillKeys(prop);
} catch (FileNotFoundException e) {
Game.error("missing file: " + filename);
return new TreeMap<>();
} catch (IOException e) {
Game.error(filename + ": corrupted file");
return new TreeMap<>();
}
}
private void saveProperties() {
Properties prop = fillPropertiesWithKeys();
try (FileOutputStream os = new FileOutputStream(PROPERTIES_FILENAME)) {
prop.store(os, "keyboard controls file");
} catch (IOException e) {
Game.error("cannot save keyboard controls");
}
}
private TreeMap<String, Integer> fillKeys(Properties prop) {
TreeMap<String, Integer> ret = new TreeMap<>();
for (Object o : Collections.list(prop.propertyNames())) {
String name = (String) o;
ret.put(name, Integer.parseInt(prop.getProperty(name)));
}
return ret;
}
private Properties fillPropertiesWithKeys() {
Properties prop = new Properties();
for (Map.Entry<String, Integer> couple : keys.entrySet())
prop.setProperty(couple.getKey(), Integer.toString(couple.getValue()));
return prop;
}
}
Loading…
Cancel
Save