PacMan/Assets/Scripts/PauseManager.cs

103 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour
{
public PacManInput pacManInputControl;//输入系统
public GameObject PauseMenu;//右上角的暂停菜单
public GameObject PauseButton;//esc的打开菜单
public GameObject DieMenu;//死亡菜单
public GameObject StartMenu;//开始菜单
public GameObject WinMenu;//胜利菜单
public Sprite[] pauseSprite;//右上角的暂停继续的图片切换
private bool isPaused = false;
public PacMan pacManScript;//拖拽赋值
private void Awake()
{
pacManInputControl = new PacManInput();//new一个新的输入系统
if (!GameManager.instance.GameStart)
{
StartTogglePause();
}
if(GameManager.instance.DotNum <= 0)
{
WinMenu.SetActive(true);
Time.timeScale = 0;
pacManInputControl.Disable();
}
}
private void OnEnable()
{
pacManInputControl.Enable();
}
private void OnDisable()
{
pacManInputControl.Disable();
}
void Update()
{
// 可选增加键盘快捷键如ESC键
if(GameManager.instance.GameStart)
{
pacManInputControl.PacManInputSystem.Menu.performed += ctx => TogglePause();
}
if (pacManScript.die)
{
DieTogglePause();
pacManScript.die = false;
}
}
// 按钮点击事件绑定方法
public void TogglePause()
{
isPaused = !isPaused;
PauseMenu.SetActive(isPaused);
PauseButton.gameObject.GetComponent<Image>().sprite = isPaused ? pauseSprite[0] : pauseSprite[1];
Time.timeScale = isPaused ? 0 : 1;
}
public void DieTogglePause()
{
isPaused = !isPaused;
Time.timeScale = 0;
DieMenu.SetActive(isPaused);
pacManInputControl.Disable();
// pacManInputControl.PacManInputSystem.Menu.Disable();
}
public void StartTogglePause()
{
StartMenu.SetActive(true);
Time.timeScale = 0;
pacManInputControl.Disable();
// pacManInputControl.PacManInputSystem.Menu.Disable();
}
public void RestartGame()
{
Time.timeScale = 1;
GameManager.instance.GameStart = true;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void QuitGame()
{
Application.Quit();
}
}