Skip to content

Commit

Permalink
ADD - added mapping for controls in options menu
Browse files Browse the repository at this point in the history
  • Loading branch information
giglio committed Mar 17, 2024
1 parent 2db9e19 commit 3b2abe8
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 35 deletions.
21 changes: 21 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,21 @@
package com.gdx.game.common;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class UtilityClass {


public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
Set<T> keys = new HashSet<T>();
for (Map.Entry<T, E> entry : map.entrySet()) {
if (Objects.equals(value, entry.getValue())) {
keys.add(entry.getKey());
}
}
return keys;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.gdx.game.component;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.utils.Json;
import com.gdx.game.entities.Entity;
import com.gdx.game.quest.QuestGraph;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -13,7 +16,7 @@ public abstract class InputComponent extends ComponentSubject implements Compone
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 +54,6 @@ public boolean scrolled(float amountX, float amountY) {
return false;
}

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

}
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;

public class BaseScreen implements Screen, AudioSubject {
protected final GdxGame gdxGame;
protected ResourceManager resourceManager;
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/com/gdx/game/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.gdx.game.camera.CameraStyles;
import com.gdx.game.component.Component;
import com.gdx.game.component.ComponentObserver;
import com.gdx.game.component.InputComponent;
import com.gdx.game.entities.Entity;
import com.gdx.game.entities.EntityFactory;
import com.gdx.game.entities.player.PlayerHUD;
Expand All @@ -28,6 +29,9 @@
import org.slf4j.LoggerFactory;

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

import static com.gdx.game.component.InputComponent.playerControls;

public class GameScreen extends BaseScreen implements ComponentObserver {

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

//initialize controls
Json jsonObject = new Json();
HashMap<String, String> jsonMap =
jsonObject.fromJson(HashMap.class, Gdx.files.local("settings/keys.json"));

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

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

Expand Down
Loading

0 comments on commit 3b2abe8

Please sign in to comment.