Refactor: added GameManager

This commit is contained in:
Vova
2023-12-11 11:53:00 +02:00
parent 05bf8484f5
commit ea59babf93
23 changed files with 162 additions and 643 deletions
+3 -3
View File
@@ -25,14 +25,14 @@ public class Bed : BaseInteractableObject
private void OnConfirm(TimeSpan time)
{
_player.SetPlayerActing(PlayerStates.Sleeping);
TimeManager.Instance.FastForward(time);
TimeManager.Instance.OnFastForwardEnd += OnFastForwardEnd;
GameManager.Instance.Time.FastForward(time);
GameManager.Instance.Time.OnFastForwardEnd += OnFastForwardEnd;
}
private void OnFastForwardEnd()
{
_player.SetPlayerActing(PlayerStates.Awake);
_player.SetPlayerAnimation(AnimationStates.Standing);
TimeManager.Instance.OnFastForwardEnd -= OnFastForwardEnd;
GameManager.Instance.Time.OnFastForwardEnd -= OnFastForwardEnd;
}
}
@@ -93,15 +93,15 @@ public class CashierDesk : BaseInteractableObject
{
_totalSalary = (float)(time.TotalHours * _playerJob.Salary);
_player.SetPlayerActing(PlayerStates.Working);
TimeManager.Instance.FastForward(time);
TimeManager.Instance.OnFastForwardEnd += OnFastForwardEnd;
GameManager.Instance.Time.FastForward(time);
GameManager.Instance.Time.OnFastForwardEnd += OnFastForwardEnd;
}
private void OnFastForwardEnd()
{
_player.AddMoney(_totalSalary);
_player.SetPlayerActing(PlayerStates.Awake);
TimeManager.Instance.OnFastForwardEnd -= OnFastForwardEnd;
GameManager.Instance.Time.OnFastForwardEnd -= OnFastForwardEnd;
}
}
+2 -1
View File
@@ -24,7 +24,8 @@ public class Door : BaseInteractableObject
{
PlayerPrefs.SetString("lastExitName", _exitName.ToLower());
}
SceneManager.Instance.ChangeScene(_scene);
GameManager.Instance.Scene.Change(_scene);
}
}
+4 -9
View File
@@ -1,5 +1,4 @@
using Cinemachine;
using System;
using UnityEngine;
using UnityEngine.InputSystem;
@@ -30,14 +29,10 @@ public class CameraSystem : MonoBehaviour
[SerializeField]
private float _rotateSpeed = 100f;
[SerializeField]
private float _mouseRotationSpeed = 3f;
[SerializeField]
private float _moveSpeed = 25f;
[SerializeField]
private int _edgeScrollSize = 20;
[SerializeField]
private float _dragPanSpeed = 2f;
[SerializeField]
private float _zoomSpeed = 2f;
[SerializeField]
private float _zoomAmount = 3f;
@@ -57,20 +52,20 @@ public class CameraSystem : MonoBehaviour
private void Update()
{
var cameraMove = InputManager.Instance.CameraAction.Move.ReadValue<Vector2>();
var cameraMove = GameManager.Instance.Input.CameraAction.Move.ReadValue<Vector2>();
if (cameraMove.sqrMagnitude > 0.1f)
{
HandleCameraMovement(cameraMove);
}
float rotateDir = InputManager.Instance.CameraAction.Rotate.ReadValue<Vector2>().x;
float rotateDir = GameManager.Instance.Input.CameraAction.Rotate.ReadValue<Vector2>().x;
if (rotateDir != 0)
{
HandleCameraRotation(rotateDir, _rotateSpeed);
}
float zoomAmount = InputManager.Instance.CameraAction.Zoom.ReadValue<Vector2>().y;
float zoomAmount = GameManager.Instance.Input.CameraAction.Zoom.ReadValue<Vector2>().y;
if (zoomAmount != 0)
{
HandleCameraZoom_MoveY(zoomAmount);
@@ -181,7 +176,7 @@ public class CameraSystem : MonoBehaviour
{
}
float rotateDir = InputManager.Instance.CameraAction.Rotate.ReadValue<Vector2>().x;
float rotateDir = GameManager.Instance.Input.CameraAction.Rotate.ReadValue<Vector2>().x;
//HandleCameraRotation(rotationValue, _mouseRotationSpeed);
transform.eulerAngles += new Vector3(0, rotateDir * _rotateSpeed * Time.deltaTime, 0);
}
-18
View File
@@ -1,18 +0,0 @@
using UnityEngine;
public class DontDestroy : MonoBehaviour
{
public static DontDestroy Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: e7675785cd5331f4bb7a2aa78bf300fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+57
View File
@@ -0,0 +1,57 @@
using UnityEngine;
using UnityEngine.InputSystem;
public class GameManager : MonoBehaviour
{
private bool _isPause;
private InputSystem _inputSystem;
private SceneManager _sceneManager;
private TimeSystem _timeSystem;
public InputSystem Input => _inputSystem;
public SceneManager Scene => _sceneManager;
public TimeSystem Time => _timeSystem;
public static GameManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
_inputSystem = new InputSystem();
_sceneManager = new SceneManager();
_timeSystem = new TimeSystem();
}
private void OnEnable()
{
_inputSystem.Enable();
}
private void OnDisable()
{
_inputSystem.Disable();
}
public void Pause()
{
_isPause = true;
}
internal void Resume()
{
_isPause = false;
}
private void Update()
{
if (!_isPause)
{
_timeSystem.UpdateTime();
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3bb5f426eac8ce54f9134f80c025083d
@@ -24,12 +24,12 @@ public class InGameMouseHandler : MonoBehaviour
private void Start()
{
InputManager.Instance.PlayerAction.PointClick.performed += ClickToMove;
GameManager.Instance.Input.PlayerAction.PointClick.performed += ClickToMove;
}
private void OnDestroy()
{
InputManager.Instance.PlayerAction.PointClick.performed += ClickToMove;
GameManager.Instance.Input.PlayerAction.PointClick.performed += ClickToMove;
}
private void ClickToMove(InputAction.CallbackContext context)
@@ -1,34 +1,25 @@
using UnityEngine;
using static InputActions;
public class InputManager : MonoBehaviour
public class InputSystem
{
private InputActions _inputActions;
public static InputManager Instance { get; private set; }
public PlayerActions PlayerAction { get; private set; }
public CameraActions CameraAction { get; private set; }
private void Awake()
public InputSystem()
{
if (Instance != null)
{
Debug.Log("There's more than one Input manager instance");
}
Instance = this;
_inputActions = new InputActions();
PlayerAction = _inputActions.Player;
CameraAction = _inputActions.Camera;
}
private void OnEnable()
public void Enable()
{
_inputActions.Enable();
}
private void OnDisable()
public void Disable()
{
_inputActions.Disable();
}
-9
View File
@@ -1,9 +0,0 @@
using UnityEngine;
public class LookAtCamera : MonoBehaviour
{
private void LateUpdate()
{
transform.LookAt(Camera.main.transform);
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 7a74888cae6f9f14ba0d76cfc5c0198a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+3 -32
View File
@@ -1,38 +1,9 @@
using UnityEngine;
public class SceneManager : MonoBehaviour
public class SceneManager
{
[SerializeField]
private LevelChanger _levelChanger;
public static SceneManager Instance { get; private set; }
private void Awake()
public void Change(string sceneName)
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
}
private void Start()
{
_levelChanger.gameObject.SetActive(true);
_levelChanger.FadeIn(OnSceneChanged);
}
public void ChangeScene(string sceneName)
{
_levelChanger.gameObject.SetActive(true);
_levelChanger.FadeToLevel(sceneName, OnSceneChanged);
}
private void OnSceneChanged()
{
_levelChanger.gameObject.SetActive(false);
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
}
}
@@ -1,10 +1,8 @@
using System;
using UnityEngine;
public class TimeManager : MonoBehaviour
public class TimeSystem
{
public static TimeManager Instance { get; private set; }
private const float MINUTE_TIME = 1f;
private const float FF_TIME = 0.003f;
@@ -14,9 +12,6 @@ public class TimeManager : MonoBehaviour
[SerializeField]
private TimeSpan _startTime = new TimeSpan(1, 08, 00, 00);
//[SerializeField]
//private Light _sunLight;
[SerializeField]
private float _sunriseHour;
[SerializeField]
@@ -26,32 +21,19 @@ public class TimeManager : MonoBehaviour
private TimeSpan _sunsetTime;
private float _timer;
private float _sunInitialIntensity;
private float _minuteToRealTime;
private static TimeSpan _currentTime;
public static TimeSpan CurrentTime => _currentTime;
private TimeSpan _currentTime;
public TimeSpan CurrentTime => _currentTime;
private TimeSpan _timeToStop;
private bool _isPause = false;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
}
// Start is called before the first frame update
void Start()
public TimeSystem()
{
// _sunInitialIntensity = _sunLight.intensity;
_timer = _minuteToRealTime;
@@ -62,16 +44,7 @@ public class TimeManager : MonoBehaviour
}
// Update is called once per frame
void Update()
{
if (!_isPause)
{
UpdateTime();
RotateSun();
}
}
private void UpdateTime()
public void UpdateTime()
{
_timer -= Time.deltaTime;
if (_timer <= 0)
@@ -91,7 +64,7 @@ public class TimeManager : MonoBehaviour
public void FastForward(TimeSpan timeToStop)
{
_isPause = false;
GameManager.Instance.Resume();
_timeToStop = _currentTime.Add(timeToStop);
_minuteToRealTime = FF_TIME;
}
@@ -112,7 +85,6 @@ public class TimeManager : MonoBehaviour
{
intensityMultiplier = 0;
}
// _sunLight.intensity = _sunInitialIntensity * intensityMultiplier;
}
private TimeSpan CalculateTimeDifference(TimeSpan from, TimeSpan to)
@@ -125,14 +97,4 @@ public class TimeManager : MonoBehaviour
return diff;
}
public void Pause()
{
_isPause = true;
}
internal void Resume()
{
_isPause = false;
}
}
+2 -2
View File
@@ -54,7 +54,7 @@ public class Player : MonoBehaviour
private void Start()
{
TimeManager.Instance.OnMinuteChanged += UpdateStatsByClock;
GameManager.Instance.Time.OnMinuteChanged += UpdateStatsByClock;
_animator.applyRootMotion = true;
_navAgent.updatePosition = false;
@@ -63,7 +63,7 @@ public class Player : MonoBehaviour
private void OnDestroy()
{
TimeManager.Instance.OnMinuteChanged -= UpdateStatsByClock;
GameManager.Instance.Time.OnMinuteChanged -= UpdateStatsByClock;
}
private void Update()
+3 -3
View File
@@ -20,8 +20,8 @@ public class TimeSliderUI : MonoBehaviour
public void ShowTimeSliderDialog(string title, string description, Action onCancel, Action<TimeSpan> onConfirm)
{
TimeManager.Instance.OnFastForwardEnd += CloseDialog;
TimeManager.Instance.Pause();
GameManager.Instance.Time.OnFastForwardEnd += CloseDialog;
GameManager.Instance.Pause();
UIManager.Instance.Freeze();
gameObject.SetActive(true);
@@ -63,7 +63,7 @@ public class TimeSliderUI : MonoBehaviour
{
_slider.onValueChanged.RemoveListener(x=>SliderValueChanger(x));
gameObject.SetActive(false);
TimeManager.Instance.Resume();
GameManager.Instance.Resume();
}
}
+3 -3
View File
@@ -24,12 +24,12 @@ public class TopBarUI : MonoBehaviour
// Start is called before the first frame update
private void Start()
{
TimeManager.Instance.OnMinuteChanged += UpdateTime;
GameManager.Instance.Time.OnMinuteChanged += UpdateTime;
}
private void OnDisable()
{
TimeManager.Instance.OnMinuteChanged -= UpdateTime;
GameManager.Instance.Time.OnMinuteChanged -= UpdateTime;
}
// Update is called once per frame
@@ -44,7 +44,7 @@ public class TopBarUI : MonoBehaviour
{
if (_timeText != null)
{
_timeText.text = $"{TimeManager.CurrentTime.GetDayName()} {TimeManager.CurrentTime.ToString(@"hh\:mm")} day ({TimeManager.CurrentTime.Days})";
_timeText.text = $"{GameManager.Instance.Time.CurrentTime.GetDayName()} {GameManager.Instance.Time.CurrentTime.ToString(@"hh\:mm")} day ({GameManager.Instance.Time.CurrentTime.Days})";
}
_energy.value = (Player.Instance.Stats[StatsId.Energy] as INumericStat).Value;