Skip to content

Commit

Permalink
Custom Keybinding added
Browse files Browse the repository at this point in the history
  • Loading branch information
theMr17 committed Apr 9, 2023
1 parent d1a85c6 commit a0a15c8
Show file tree
Hide file tree
Showing 10 changed files with 6,131 additions and 1,162 deletions.
7,011 changes: 5,853 additions & 1,158 deletions Assets/Scenes/GameScene.unity

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Assets/Scenes/MainMenuScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 998620165}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1142.8285, y: 520.56396, z: 0.8565953}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Counters/BaseCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class BaseCounter : MonoBehaviour, IKitchenObjectParent {
public static event EventHandler OnAnyObjectPlacedHere;

new public static void ResetStaticData() {
public static void ResetStaticData() {
OnAnyObjectPlacedHere = null;
}

Expand Down
89 changes: 89 additions & 0 deletions Assets/Scripts/GameInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class GameInput : MonoBehaviour {
private const string PLAYER_PREFS_BINDINGS = "InputBindings";

public static GameInput Instance { get; private set; }

public event EventHandler OnInteractAction;
public event EventHandler OnInteractAlternateAction;
public event EventHandler OnPauseAction;

public enum Binding {
Move_Up,
Move_Down,
Move_Left,
Move_Right,
Interact,
InteractAlternate,
Pause,
}

private PlayerInputActions playerInputActions;

private void Awake() {
Instance = this;

playerInputActions = new PlayerInputActions();

if(PlayerPrefs.HasKey(PLAYER_PREFS_BINDINGS)) {
playerInputActions.LoadBindingOverridesFromJson(PlayerPrefs.GetString(PLAYER_PREFS_BINDINGS));
}

playerInputActions.Player.Enable();

playerInputActions.Player.Interact.performed += Interact_performed;
Expand Down Expand Up @@ -48,4 +67,74 @@ public Vector2 GetMovementVectorNormalized() {
inputVector = inputVector.normalized;
return inputVector;
}

public string GetBindingText(Binding binding) {
switch(binding) {
default:
case Binding.Move_Up:
return playerInputActions.Player.Move.bindings[1].ToDisplayString();
case Binding.Move_Down:
return playerInputActions.Player.Move.bindings[2].ToDisplayString();
case Binding.Move_Left:
return playerInputActions.Player.Move.bindings[3].ToDisplayString();
case Binding.Move_Right:
return playerInputActions.Player.Move.bindings[4].ToDisplayString();
case Binding.Interact:
return playerInputActions.Player.Interact.bindings[0].ToDisplayString();
case Binding.InteractAlternate:
return playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString();
case Binding.Pause:
return playerInputActions.Player.Pause.bindings[0].ToDisplayString();
}
}

public void RebindBinding(Binding binding, Action onActionRebound) {
playerInputActions.Player.Disable();

InputAction inputAction;
int bindingIndex;

switch(binding) {
default:
case Binding.Move_Up:
inputAction = playerInputActions.Player.Move;
bindingIndex = 1;
break;
case Binding.Move_Down:
inputAction = playerInputActions.Player.Move;
bindingIndex = 2;
break;
case Binding.Move_Left:
inputAction = playerInputActions.Player.Move;
bindingIndex = 3;
break;
case Binding.Move_Right:
inputAction = playerInputActions.Player.Move;
bindingIndex = 4;
break;
case Binding.Interact:
inputAction = playerInputActions.Player.Interact;
bindingIndex = 0;
break;
case Binding.InteractAlternate:
inputAction = playerInputActions.Player.InteractAlternate;
bindingIndex = 0;
break;
case Binding.Pause:
inputAction = playerInputActions.Player.Pause;
bindingIndex = 0;
break;
}

inputAction.PerformInteractiveRebinding(bindingIndex)
.OnComplete(callback => {
callback.Dispose();
playerInputActions.Player.Enable();
onActionRebound();

PlayerPrefs.SetString(PLAYER_PREFS_BINDINGS, playerInputActions.SaveBindingOverridesAsJson());
PlayerPrefs.Save();
})
.Start();
}
}
36 changes: 36 additions & 0 deletions Assets/Scripts/MusicManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MusicManager : MonoBehaviour {
private const string PLAYER_PREFS_MUSIC_EFFECTS_VOLUME = "MusicVolume";

public static MusicManager Instance { get; private set; }

private AudioSource audioSource;
private float volume = 0.3f;

private void Awake() {
Instance = this;

audioSource = GetComponent<AudioSource>();

volume = PlayerPrefs.GetFloat(PLAYER_PREFS_MUSIC_EFFECTS_VOLUME, 0.3f);
audioSource.volume = volume;
}

public void ChangeVolume() {
volume += 0.1f;
if(volume > 1f) {
volume = 0f;
}
audioSource.volume = volume;

PlayerPrefs.SetFloat(PLAYER_PREFS_MUSIC_EFFECTS_VOLUME, volume);
PlayerPrefs.Save();
}

public float GetVolume() {
return volume;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/MusicManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions Assets/Scripts/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
using UnityEngine;

public class SoundManager : MonoBehaviour {
private const string PLAYER_PREFS_SOUND_EFFECTS_VOLUME = "SoundEffectsVolume";

public static SoundManager Instance { get; private set; }

[SerializeField] private AudioClipRefsSO audioClipRefsSO;

private float volume = 1f;

private void Awake() {
Instance = this;

volume = PlayerPrefs.GetFloat(PLAYER_PREFS_SOUND_EFFECTS_VOLUME, 1f);
}

private void Start() {
Expand Down Expand Up @@ -58,7 +64,21 @@ private void PlaySound(AudioClip audioClip, Vector3 position, float volume = 1f)
AudioSource.PlayClipAtPoint(audioClip, position, volume);
}

public void PlayFootstepsSound(Vector3 position, float volume = 1f) {
PlaySound(audioClipRefsSO.footstep, position, volume);
public void PlayFootstepsSound(Vector3 position, float volumeMultiplier = 1f) {
PlaySound(audioClipRefsSO.footstep, position, volumeMultiplier * volume);
}

public void ChangeVolume() {
volume += 0.1f;
if(volume > 1f) {
volume = 0f;
}

PlayerPrefs.SetFloat(PLAYER_PREFS_SOUND_EFFECTS_VOLUME, volume);
PlayerPrefs.Save();
}

public float GetVolume() {
return volume;
}
}
4 changes: 4 additions & 0 deletions Assets/Scripts/UI/GamePauseUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

public class GamePauseUI : MonoBehaviour {
[SerializeField] private Button resumeButton;
[SerializeField] private Button optionsButton;
[SerializeField] private Button mainMenuButton;

private void Awake() {
resumeButton.onClick.AddListener(() => {
KitchenGameManager.Instance.TogglePauseGame();
});
optionsButton.onClick.AddListener(() => {
OptionsUI.Instance.Show();
});
mainMenuButton.onClick.AddListener(() => {
Loader.Load(Loader.Scene.MainMenuScene);
});
Expand Down
103 changes: 103 additions & 0 deletions Assets/Scripts/UI/OptionsUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class OptionsUI : MonoBehaviour {
public static OptionsUI Instance { get; private set; }

[SerializeField] private Button soundEffectsButton;
[SerializeField] private Button musicButton;
[SerializeField] private Button closeButton;
[SerializeField] private Button moveUpButton;
[SerializeField] private Button moveDownButton;
[SerializeField] private Button moveLeftButton;
[SerializeField] private Button moveRightButton;
[SerializeField] private Button interactButton;
[SerializeField] private Button interactAltButton;
[SerializeField] private Button pauseButton;
[SerializeField] private TextMeshProUGUI soundEffectsText;
[SerializeField] private TextMeshProUGUI musicText;
[SerializeField] private TextMeshProUGUI moveUpText;
[SerializeField] private TextMeshProUGUI moveDownText;
[SerializeField] private TextMeshProUGUI moveLeftText;
[SerializeField] private TextMeshProUGUI moveRightText;
[SerializeField] private TextMeshProUGUI interactText;
[SerializeField] private TextMeshProUGUI interactAltText;
[SerializeField] private TextMeshProUGUI pauseText;
[SerializeField] private Transform pressToRebindKeyTransform;

private void Awake() {
Instance = this;

soundEffectsButton.onClick.AddListener(() => {
SoundManager.Instance.ChangeVolume();
UpdateVisual();
});
musicButton.onClick.AddListener(() => {
MusicManager.Instance.ChangeVolume();
UpdateVisual();
});
closeButton.onClick.AddListener(() => {
Hide();
});

moveUpButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Move_Up); });
moveDownButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Move_Down); });
moveLeftButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Move_Left); });
moveRightButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Move_Right); });
interactButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Interact); });
interactAltButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.InteractAlternate); });
pauseButton.onClick.AddListener(() => { RebindBinding(GameInput.Binding.Pause); });
}

private void Start() {
KitchenGameManager.Instance.OnGameUnpaused += KitchenGameManager_OnGameUnpaused;

UpdateVisual();
Hide();
HidePressToRebindKey();
}

private void KitchenGameManager_OnGameUnpaused(object sender, System.EventArgs e) {
Hide();
}

private void UpdateVisual() {
soundEffectsText.text = "Sound Effects: " + Mathf.Round(SoundManager.Instance.GetVolume() * 10f);
musicText.text = "Music Effects: " + Mathf.Round(MusicManager.Instance.GetVolume() * 10f);

moveUpText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Move_Up);
moveDownText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Move_Down);
moveLeftText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Move_Left);
moveRightText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Move_Right);
interactText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Interact);
interactAltText.text = GameInput.Instance.GetBindingText(GameInput.Binding.InteractAlternate);
pauseText.text = GameInput.Instance.GetBindingText(GameInput.Binding.Pause);
}

public void Show() {
gameObject.SetActive(true);
}

private void Hide() {
gameObject.SetActive(false);
}

public void ShowPressToRebindKey() {
pressToRebindKeyTransform.gameObject.SetActive(true);
}

private void HidePressToRebindKey() {
pressToRebindKeyTransform.gameObject.SetActive(false);
}

private void RebindBinding(GameInput.Binding binding) {
ShowPressToRebindKey();
GameInput.Instance.RebindBinding(binding, () => {
HidePressToRebindKey();
UpdateVisual();
});
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/UI/OptionsUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0a15c8

Please sign in to comment.