2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.2 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. namespace Unity.FPS.Game
  4. {
  5. public class GameFlowManager : MonoBehaviour
  6. {
  7. [Header("Parameters")] [Tooltip("Duration of the fade-to-black at the end of the game")]
  8. public float EndSceneLoadDelay = 3f;
  9. [Tooltip("The canvas group of the fade-to-black screen")]
  10. public CanvasGroup EndGameFadeCanvasGroup;
  11. [Header("Win")] [Tooltip("This string has to be the name of the scene you want to load when winning")]
  12. public string WinSceneName = "WinScene";
  13. [Tooltip("Duration of delay before the fade-to-black, if winning")]
  14. public float DelayBeforeFadeToBlack = 4f;
  15. [Tooltip("Win game message")]
  16. public string WinGameMessage;
  17. [Tooltip("Duration of delay before the win message")]
  18. public float DelayBeforeWinMessage = 2f;
  19. [Tooltip("Sound played on win")] public AudioClip VictorySound;
  20. [Header("Lose")] [Tooltip("This string has to be the name of the scene you want to load when losing")]
  21. public string LoseSceneName = "LoseScene";
  22. public bool GameIsEnding { get; private set; }
  23. float m_TimeLoadEndGameScene;
  24. string m_SceneToLoad;
  25. void Awake()
  26. {
  27. EventManager.AddListener<AllObjectivesCompletedEvent>(OnAllObjectivesCompleted);
  28. EventManager.AddListener<PlayerDeathEvent>(OnPlayerDeath);
  29. }
  30. void Start()
  31. {
  32. AudioUtility.SetMasterVolume(1);
  33. }
  34. void Update()
  35. {
  36. if (GameIsEnding)
  37. {
  38. float timeRatio = 1 - (m_TimeLoadEndGameScene - Time.time) / EndSceneLoadDelay;
  39. EndGameFadeCanvasGroup.alpha = timeRatio;
  40. AudioUtility.SetMasterVolume(1 - timeRatio);
  41. // See if it's time to load the end scene (after the delay)
  42. if (Time.time >= m_TimeLoadEndGameScene)
  43. {
  44. SceneManager.LoadScene(m_SceneToLoad);
  45. GameIsEnding = false;
  46. }
  47. }
  48. }
  49. void OnAllObjectivesCompleted(AllObjectivesCompletedEvent evt) => EndGame(true);
  50. void OnPlayerDeath(PlayerDeathEvent evt) => EndGame(false);
  51. void EndGame(bool win)
  52. {
  53. // unlocks the cursor before leaving the scene, to be able to click buttons
  54. Cursor.lockState = CursorLockMode.None;
  55. Cursor.visible = true;
  56. // Remember that we need to load the appropriate end scene after a delay
  57. GameIsEnding = true;
  58. EndGameFadeCanvasGroup.gameObject.SetActive(true);
  59. if (win)
  60. {
  61. m_SceneToLoad = WinSceneName;
  62. m_TimeLoadEndGameScene = Time.time + EndSceneLoadDelay + DelayBeforeFadeToBlack;
  63. // play a sound on win
  64. var audioSource = gameObject.AddComponent<AudioSource>();
  65. audioSource.clip = VictorySound;
  66. audioSource.playOnAwake = false;
  67. audioSource.outputAudioMixerGroup = AudioUtility.GetAudioGroup(AudioUtility.AudioGroups.HUDVictory);
  68. audioSource.PlayScheduled(AudioSettings.dspTime + DelayBeforeWinMessage);
  69. // create a game message
  70. //var message = Instantiate(WinGameMessagePrefab).GetComponent<DisplayMessage>();
  71. //if (message)
  72. //{
  73. // message.delayBeforeShowing = delayBeforeWinMessage;
  74. // message.GetComponent<Transform>().SetAsLastSibling();
  75. //}
  76. DisplayMessageEvent displayMessage = Events.DisplayMessageEvent;
  77. displayMessage.Message = WinGameMessage;
  78. displayMessage.DelayBeforeDisplay = DelayBeforeWinMessage;
  79. EventManager.Broadcast(displayMessage);
  80. }
  81. else
  82. {
  83. m_SceneToLoad = LoseSceneName;
  84. m_TimeLoadEndGameScene = Time.time + EndSceneLoadDelay;
  85. }
  86. }
  87. void OnDestroy()
  88. {
  89. EventManager.RemoveListener<AllObjectivesCompletedEvent>(OnAllObjectivesCompleted);
  90. EventManager.RemoveListener<PlayerDeathEvent>(OnPlayerDeath);
  91. }
  92. }
  93. }