Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adding controls options #59

Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ lib/
*.hprof
.DS_Store
*.sav
/**/settings/keys.json

### STS ###
.apt_generated
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/gdx/game/common/Constats.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public class Constats {

public static final String COURTESY_PHRASES_PATH = "conversations/conversation_courtesy.json";

public static final String PARTIAL_CONTROLS_SETTINGS_PATH = "settings/keys.json";
public static final String FULL_CONTROLS_SETTINGS_PATH = "core/src/main/resources/settings/keys.json";

}
21 changes: 21 additions & 0 deletions core/src/main/java/com/gdx/game/common/DefaultControlsMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gdx.game.common;

import com.gdx.game.component.InputComponent;

import java.util.HashMap;

public class DefaultControlsMap {

public static final HashMap<String, String> DEFAULT_CONTROLS = new HashMap<>();

static {
DEFAULT_CONTROLS.put("33", InputComponent.Keys.INTERACT.name());
DEFAULT_CONTROLS.put("43", InputComponent.Keys.OPTION.name());
DEFAULT_CONTROLS.put("32", InputComponent.Keys.RIGHT.name());
DEFAULT_CONTROLS.put("111", InputComponent.Keys.QUIT.name());
DEFAULT_CONTROLS.put("51", InputComponent.Keys.UP.name());
DEFAULT_CONTROLS.put("47", InputComponent.Keys.DOWN.name());
DEFAULT_CONTROLS.put("29", InputComponent.Keys.LEFT.name());
}

}
30 changes: 30 additions & 0 deletions core/src/main/java/com/gdx/game/common/UtilityClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gdx.game.common;

import java.util.HashMap;
import java.util.Map;

public class UtilityClass {

public static <T, E> HashMap<E, T> mapInverter(HashMap<T, E> hashMap){
hdescottes marked this conversation as resolved.
Show resolved Hide resolved

HashMap<E, T> newHashMap = new HashMap<>();

for(Map.Entry<T, E> entry : hashMap.entrySet()){
newHashMap.put(entry.getValue(), entry.getKey());
}

return newHashMap;
}

public static <T, E> Map<E, T> mapInverter(Map<T, E> hashMap){

Map<E, T> newMap = new HashMap<>();

for(Map.Entry<T, E> entry : hashMap.entrySet()){
newMap.put(entry.getValue(), entry.getKey());
}

return newMap;
}

}
39 changes: 38 additions & 1 deletion core/src/main/java/com/gdx/game/component/InputComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import java.util.HashMap;
import java.util.Map;

import static com.gdx.game.common.UtilityClass.mapInverter;

public abstract class InputComponent extends ComponentSubject implements Component, InputProcessor {

protected Entity.Direction currentDirection = null;
protected Entity.State currentState = null;
protected Json json;

protected enum Keys {
public enum Keys {
LEFT, RIGHT, UP, DOWN, QUIT, INTERACT, OPTION
}

Expand Down Expand Up @@ -51,4 +53,39 @@ public boolean scrolled(float amountX, float amountY) {
return false;
}

public static HashMap<Integer, Keys> playerControls = new HashMap<>();

public static void setPlayerControlMapFromJsonControlsMap(HashMap<String, String> jsonMap){

HashMap<Integer, Keys> newPlayerControls = new HashMap<>();

for (var entry : jsonMap.entrySet()) {
newPlayerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue()));
}

playerControls = newPlayerControls;

}

public static HashMap<String, String> mapJsonControlsToPlayerControl(HashMap<Integer, Keys> playerControls){

HashMap<String, String> result = new HashMap<>();

for (var entry : playerControls.entrySet()) {
result.put(entry.getKey().toString(), entry.getValue().toString());
}

return result;
}

public static HashMap<String, String> changeValueFromJsonControlsMap(HashMap<String, String> jsonMap,
Keys keyValue,
Integer keyCode){
jsonMap = mapInverter(jsonMap);
jsonMap.put(keyValue.name(), String.valueOf(keyCode));
jsonMap = mapInverter(jsonMap);

return jsonMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,31 @@ public void update(Entity entity, float delta) {

@Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.DOWN:
case Input.Keys.S:

if(!playerControls.containsKey(keycode)){
return false;
}

switch (playerControls.get(keycode)) {
case DOWN:
this.downPressed();
break;
case Input.Keys.UP:
case Input.Keys.W:
case UP:
this.upPressed();
break;
case Input.Keys.LEFT:
case Input.Keys.A:
case LEFT:
this.leftPressed();
break;
case Input.Keys.RIGHT:
case Input.Keys.D:
case RIGHT:
this.rightPressed();
break;
case Input.Keys.E:
case INTERACT:
this.interactPressed();
break;
case Input.Keys.O:
case OPTION:
this.optionPressed();
break;
case Input.Keys.ESCAPE:
case QUIT:
this.quitPressed();
break;
default:
Expand All @@ -126,35 +127,33 @@ public boolean keyDown(int keycode) {

@Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.DOWN:
case Input.Keys.S:

if(!playerControls.containsKey(keycode)){
return false;
}

switch (playerControls.get(keycode)) {
case DOWN:
this.downReleased();
break;
case Input.Keys.UP:
case Input.Keys.W:
case UP:
this.upReleased();
break;
case Input.Keys.LEFT:
case Input.Keys.A:
case LEFT:
this.leftReleased();
break;
case Input.Keys.RIGHT:
case Input.Keys.D:
case RIGHT:
this.rightReleased();
break;
case Input.Keys.E:
case INTERACT:
this.interactReleased();
break;
case Input.Keys.O:
case OPTION:
this.optionReleased();
break;
case Input.Keys.ESCAPE:
case QUIT:
this.quitReleased();
break;
case Input.Keys.BACKSPACE:
debug = !debug;
break;
default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/gdx/game/screen/BaseScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.Viewport;
Expand All @@ -22,6 +24,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.gdx.game.manager.ResourceManager.skin;
hdescottes marked this conversation as resolved.
Show resolved Hide resolved

public class BaseScreen implements Screen, AudioSubject {
protected final GdxGame gdxGame;
protected ResourceManager resourceManager;
Expand Down
44 changes: 36 additions & 8 deletions core/src/main/java/com/gdx/game/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.SerializationException;
import com.gdx.game.GdxGame;
import com.gdx.game.audio.AudioManager;
import com.gdx.game.audio.AudioObserver;
Expand All @@ -28,6 +29,13 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;

import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH;
import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH;
import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS;
import static com.gdx.game.component.InputComponent.playerControls;
import static com.gdx.game.component.InputComponent.setPlayerControlMapFromJsonControlsMap;

public class GameScreen extends BaseScreen implements ComponentObserver {

Expand Down Expand Up @@ -56,14 +64,13 @@ public enum GameState {
protected MapManager mapManager;
protected OrthographicCamera camera;
protected OrthographicCamera hudCamera;
private Stage gameStage = new Stage();

private Json json;
private GdxGame game;
private InputMultiplexer multiplexer;
private final Json json;
private final GdxGame game;
private final InputMultiplexer multiplexer;

private Entity player;
private PlayerHUD playerHUD;
private final Entity player;
private final PlayerHUD playerHUD;

private float startX;
private float startY;
Expand Down Expand Up @@ -92,6 +99,28 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) {
player = EntityFactory.getInstance().getEntity(ProfileManager.getInstance().getProperty("playerCharacter", EntityFactory.EntityType.class));
player.registerObserver(this);

//initialize controls
HashMap<String, String> controlMap;

if(playerControls.isEmpty()){
try {
controlMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH));

if (DEFAULT_CONTROLS.size() != controlMap.size()){
throw new SerializationException("Not valid control map");
}
} catch (SerializationException se) {LOGGER.error(se.getMessage());

// if I can not read the file , so use the default controls binding and save it
controlMap = DEFAULT_CONTROLS;

FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH);
commandsFile.writeString(json.prettyPrint(controlMap), false);
}

setPlayerControlMapFromJsonControlsMap(controlMap);
}

mapManager.setPlayer(player);
mapManager.setCamera(camera);

Expand Down Expand Up @@ -236,7 +265,6 @@ public static GameState getGameState() {

public static void setGameState(GameState state) {
switch (state) {
case RUNNING -> gameState = GameState.RUNNING;
case LOADING -> {
ProfileManager.getInstance().loadProfile();
gameState = GameState.RUNNING;
Expand Down
Loading