GameManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. public class GameManager : MonoBehaviour
  3. {
  4. private GameData data;
  5. private SaveManager saveManager;
  6. private TimeManager timeManager;
  7. private GameObject player;
  8. // Start is called once before the first execution of Update after the MonoBehaviour is created
  9. void Start()
  10. {
  11. //Get the player
  12. player = GameObject.Find("Player");
  13. if (player == null) return;
  14. //Get components
  15. saveManager = GetComponent<SaveManager>();
  16. timeManager = GetComponent<TimeManager>();
  17. data = saveManager.LoadGame();
  18. if (data != null)
  19. {
  20. SetData();
  21. }
  22. else
  23. {
  24. data = new GameData();
  25. }
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (Input.GetKeyDown(KeyCode.Space))
  31. {
  32. GetData();
  33. saveManager.SaveGame(data);
  34. }
  35. }
  36. void GetData()
  37. {
  38. data.playerPosition = player.transform.position;
  39. #region DaylightCycle
  40. data.minutes = timeManager.Minutes;
  41. data.hours = timeManager.Hours;
  42. data.skybox = RenderSettings.skybox;
  43. data.globalLightColor = timeManager.globalLight.color;
  44. data.globalLightRotation = timeManager.globalLight.transform.rotation;
  45. #endregion
  46. #region Stamina
  47. data.currentStamina = player.GetComponent<Stamina>().GetCurrentStamina();
  48. data.maxStamina = player.GetComponent<Stamina>().maxStamina;
  49. data.canSprint = player.GetComponent<Stamina>().GetCanSprint();
  50. #endregion
  51. }
  52. void SetData()
  53. {
  54. player.transform.position = data.playerPosition;
  55. #region DaylightCycle
  56. timeManager.Minutes = data.minutes;
  57. timeManager.Hours = data.hours;
  58. RenderSettings.skybox = data.skybox;
  59. timeManager.globalLight.color = data.globalLightColor;
  60. RenderSettings.fogColor = data.globalLightColor;
  61. timeManager.globalLight.transform.rotation = data.globalLightRotation;
  62. #endregion
  63. #region Stamina
  64. player.GetComponent<Stamina>().Reinit(data.currentStamina, data.maxStamina, data.canSprint);
  65. #endregion
  66. }
  67. }