Pauser Added + Fixed Bug
TODO: Stop Animations when Paused
This commit is contained in:
@@ -94,7 +94,7 @@ namespace Level
|
||||
UIManager.Instance.CloseAllCanvases();
|
||||
}
|
||||
|
||||
private void DisableAllLevels()
|
||||
public void DisableAllLevels()
|
||||
{
|
||||
Player.SetActive(false);
|
||||
foreach (Level level in Levels.Values)
|
||||
|
40
Assets/Scripts/Pausable/Pauser.cs
Normal file
40
Assets/Scripts/Pausable/Pauser.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Level;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Pausable
|
||||
{
|
||||
public class Pauser : MonoBehaviour
|
||||
{
|
||||
protected Input.PlayerInput playerInput = null;
|
||||
protected bool isPaused = false;
|
||||
|
||||
protected void Start()
|
||||
{
|
||||
playerInput = new Input.PlayerInput();
|
||||
playerInput.Enable();
|
||||
playerInput.PlayerControl.Pause.performed += (context) => TogglePause();
|
||||
}
|
||||
|
||||
public void TogglePause()
|
||||
{
|
||||
if (LevelManager.Instance.CurrentLevel == null)
|
||||
return;
|
||||
|
||||
isPaused = !isPaused;
|
||||
|
||||
if (isPaused)
|
||||
UIManager.Instance.SwitchToCanvas("Pause Menu");
|
||||
else
|
||||
UIManager.Instance.CloseAllCanvases();
|
||||
|
||||
foreach (IPausable pausable in FindObjectsOfType<MonoBehaviour>().OfType<IPausable>())
|
||||
if (isPaused)
|
||||
pausable.Pause();
|
||||
else
|
||||
pausable.Resume();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Pausable/Pauser.cs.meta
Normal file
11
Assets/Scripts/Pausable/Pauser.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1705d06bcf3cabe4d9e2614e86cd0561
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -102,6 +102,9 @@ namespace Player
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (IsPaused)
|
||||
return;
|
||||
|
||||
//Took from tutorial : https://www.youtube.com/watch?v=7KiK0Aqtmzc
|
||||
switch (_playerRigidbody2D.velocity.y)
|
||||
{
|
||||
@@ -136,6 +139,9 @@ namespace Player
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (IsPaused)
|
||||
return;
|
||||
|
||||
_isOnAir = !_playerGroundTrigger.IsCollided;
|
||||
RespawnCheck();
|
||||
|
||||
@@ -156,13 +162,13 @@ namespace Player
|
||||
public void Pause()
|
||||
{
|
||||
IsPaused = true;
|
||||
_playerRigidbody2D.simulated = IsPaused;
|
||||
_playerRigidbody2D.simulated = !IsPaused;
|
||||
}
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
IsPaused = false;
|
||||
_playerRigidbody2D.simulated = IsPaused;
|
||||
_playerRigidbody2D.simulated = !IsPaused;
|
||||
}
|
||||
|
||||
// MOVE METHODS
|
||||
@@ -292,5 +298,9 @@ namespace Player
|
||||
}
|
||||
|
||||
public void Clop() => clop.PlayClop();
|
||||
|
||||
public void OnPause(InputAction.CallbackContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
Assets/Scripts/UI/PauseMenu.cs
Normal file
17
Assets/Scripts/UI/PauseMenu.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Level;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PauseMenu : MonoBehaviour
|
||||
{
|
||||
public void Resume() => UIManager.Instance.Pauser.TogglePause();
|
||||
public void Restart() => LevelManager.Instance.CurrentLevel.Restart();
|
||||
public void MainMenu()
|
||||
{
|
||||
LevelManager.Instance.DisableAllLevels();
|
||||
UIManager.Instance.SwitchToCanvas("Main Menu");
|
||||
}
|
||||
public void Exit() => Application.Quit();
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/PauseMenu.cs.meta
Normal file
11
Assets/Scripts/UI/PauseMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c8db5f2b097c0f4eaa1098c35e1bc0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Pausable;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
@@ -8,6 +9,7 @@ namespace UI
|
||||
private static UIManager _instance = null;
|
||||
public static UIManager Instance => _instance;
|
||||
private Dictionary<string, Canvas> canvases = null;
|
||||
public Pauser Pauser { get; private set; } = null;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -18,6 +20,8 @@ namespace UI
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
private void Start() => Pauser = gameObject.AddComponent<Pauser>();
|
||||
|
||||
public void SwitchToCanvas(string canvasName)
|
||||
{
|
||||
Initialize();
|
||||
|
Reference in New Issue
Block a user