diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java new file mode 100644 index 00000000..a713c537 --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -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 Set getKeysByValue(Map map, E value) { + Set keys = new HashSet(); + for (Map.Entry entry : map.entrySet()) { + if (Objects.equals(value, entry.getValue())) { + keys.add(entry.getKey()); + } + } + return keys; + } + +} diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 399e1b9c..12a1f9c1 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -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; @@ -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 } @@ -51,4 +54,6 @@ public boolean scrolled(float amountX, float amountY) { return false; } + public static Map playerControls = new HashMap<>(); + } diff --git a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java index 9721af06..4a9d7429 100644 --- a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java +++ b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java @@ -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: @@ -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; } diff --git a/core/src/main/java/com/gdx/game/screen/BaseScreen.java b/core/src/main/java/com/gdx/game/screen/BaseScreen.java index b7e672d1..1219c09b 100644 --- a/core/src/main/java/com/gdx/game/screen/BaseScreen.java +++ b/core/src/main/java/com/gdx/game/screen/BaseScreen.java @@ -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; @@ -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; diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 40088950..b37e388a 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -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; @@ -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 { @@ -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 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); diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1f859480..1c6eb23b 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -1,21 +1,40 @@ package com.gdx.game.screen; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; +import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Json; import com.crashinvaders.vfx.VfxManager; import com.crashinvaders.vfx.effects.GaussianBlurEffect; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; +import com.gdx.game.component.InputComponent; +import com.gdx.game.entities.player.PlayerInputComponent; import com.gdx.game.manager.ResourceManager; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static com.gdx.game.common.UtilityClass.getKeysByValue; +import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { @@ -35,6 +54,8 @@ public class OptionScreen extends BaseScreen { private VfxManager vfxManager; private GaussianBlurEffect vfxEffect; + private Map playerControlsNew = new HashMap<>(); + public OptionScreen(GdxGame gdxGame, BaseScreen previousScreen, ResourceManager resourceManager) { super(gdxGame, resourceManager); this.previousScreen = previousScreen; @@ -69,23 +90,213 @@ private void handleControlButton() { createButton("Control",0, optionTable.getHeight()/10, optionTable); Actor controlButton = optionTable.getCells().get(0).getActor(); + controlButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { controlClickListener = true; + controlTable = createTable(); + handleControlSettings(); handleControlBackButton(); } }); } + private void handleControlSettings() { + + Json jsonObject = new Json(); + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal("settings/keys.json")); + + Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); + TextField downText = new TextField("S", skin); + Label upLabel = new Label(InputComponent.Keys.UP.name(), skin); + TextField upText = new TextField("W", skin); + Label leftLabel = new Label(InputComponent.Keys.LEFT.name(), skin); + TextField leftText = new TextField("A", skin); + Label rightLabel = new Label(InputComponent.Keys.RIGHT.name(), skin); + TextField rightText = new TextField("D", skin); + Label interactLabel = new Label(InputComponent.Keys.INTERACT.name(), skin); + TextField interactText = new TextField("E", skin); + Label optionLabel = new Label(InputComponent.Keys.OPTION.name(), skin); + TextField optionText = new TextField("O", skin); + Label quitLabel = new Label(InputComponent.Keys.QUIT.name(), skin); + TextField quitText = new TextField("ENTER", skin); + + downText.setMaxLength(1); + upText.setMaxLength(1); + leftText.setMaxLength(1); + rightText.setMaxLength(1); + interactText.setMaxLength(1); + optionText.setMaxLength(1); + quitText.setMaxLength(1); + + downText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.DOWN.name()); + downText.setMaxLength(Input.Keys.toString(keycode).length()); + downText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + upText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.UP.name()); + upText.setMaxLength(Input.Keys.toString(keycode).length()); + upText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + leftText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.LEFT.name()); + leftText.setMaxLength(Input.Keys.toString(keycode).length()); + leftText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + rightText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.RIGHT.name()); + rightText.setMaxLength(Input.Keys.toString(keycode).length()); + rightText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + interactText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.INTERACT.name()); + interactText.setMaxLength(Input.Keys.toString(keycode).length()); + interactText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + optionText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.OPTION.name()); + optionText.setMaxLength(Input.Keys.toString(keycode).length()); + optionText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + quitText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.QUIT.name()); + quitText.setMaxLength(Input.Keys.toString(keycode).length()); + quitText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + controlTable.add(downLabel); + controlTable.add(downText); + controlTable.row(); + controlTable.add(upLabel); + controlTable.add(upText); + controlTable.row(); + controlTable.add(leftLabel); + controlTable.add(leftText); + controlTable.row(); + controlTable.add(rightLabel); + controlTable.add(rightText); + controlTable.row(); + controlTable.add(interactLabel); + controlTable.add(interactText); + controlTable.row(); + controlTable.add(optionLabel); + controlTable.add(optionText); + controlTable.row(); + controlTable.add(quitLabel); + controlTable.add(quitText); + controlTable.row(); + + } + private void handleControlBackButton() { createButton("Back",0, controlTable.getHeight()/5, controlTable); - Actor backButton = controlTable.getCells().get(0).getActor(); + Actor backButton = controlTable.getCells().get(controlTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { + + Json json = new Json(); + + File f = Gdx.files.local("settings/keys.json").file(); + try { + f.createNewFile(); + + FileWriter fw = new FileWriter(f); + + fw.write(json.prettyPrint(playerControlsNew)); + fw.close(); + + } catch (IOException e) { + throw new RuntimeException(e); + } + controlClickListener = false; } }); @@ -107,16 +318,16 @@ public void clicked(InputEvent even, float x, float y) { } private void handleMusicSettings() { - Label musicLabel = new Label("MUSIC", resourceManager.skin); + Label musicLabel = new Label("MUSIC", skin); musicLabel.setAlignment(Align.left); - Slider musicSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider musicSlider = new Slider(0, 1, 0.01f, false, skin); musicSlider.setValue(gdxGame.getPreferenceManager().getMusicVolume()); musicSlider.addListener(event -> { gdxGame.getPreferenceManager().setMusicVolume(musicSlider.getValue()); AudioManager.getInstance().getCurrentMusic().setVolume(gdxGame.getPreferenceManager().getMusicVolume()); return false; }); - CheckBox musicCheckbox = new CheckBox("Enable Music", resourceManager.skin); + CheckBox musicCheckbox = new CheckBox("Enable Music", skin); musicCheckbox.setChecked(gdxGame.getPreferenceManager().isMusicEnabled()); musicCheckbox.addListener(event -> { gdxGame.getPreferenceManager().setMusicEnabled(musicCheckbox.isChecked()); @@ -124,15 +335,15 @@ private void handleMusicSettings() { return false; }); - Label soundLabel = new Label("SOUND", resourceManager.skin); + Label soundLabel = new Label("SOUND", skin); soundLabel.setAlignment(Align.left); - Slider soundSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider soundSlider = new Slider(0, 1, 0.01f, false, skin); soundSlider.setValue(gdxGame.getPreferenceManager().getSoundVolume()); soundSlider.addListener(event -> { gdxGame.getPreferenceManager().setSoundVolume(soundSlider.getValue()); return false; }); - CheckBox soundCheckbox = new CheckBox("Enable Sound", resourceManager.skin); + CheckBox soundCheckbox = new CheckBox("Enable Sound", skin); soundCheckbox.setChecked(gdxGame.getPreferenceManager().isSoundEffectsEnabled()); soundCheckbox.addListener(event -> { boolean enabled = soundCheckbox.isChecked(); diff --git a/core/src/main/resources/settings/keys.json b/core/src/main/resources/settings/keys.json new file mode 100644 index 00000000..72db42ac --- /dev/null +++ b/core/src/main/resources/settings/keys.json @@ -0,0 +1,30 @@ +{ + "33":{ + "class":"java.lang.String", + "value":"INTERACT" + }, + "44":{ + "class":"java.lang.String", + "value":"OPTION" + }, + "111":{ + "class":"java.lang.String", + "value":"QUIT" + }, + "47":{ + "class":"java.lang.String", + "value":"DOWN" + }, + "29":{ + "class":"java.lang.String", + "value":"LEFT" + }, + "51":{ + "class":"java.lang.String", + "value":"UP" + }, + "32":{ + "class":"java.lang.String", + "value":"RIGHT" + } +} \ No newline at end of file