Retry UI
This commit is contained in:
8
Assets/_Content/Scripts/Player.meta
Normal file
8
Assets/_Content/Scripts/Player.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7632d4d556d06e14387adebb7d498a40
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,6 +2,8 @@ using TMPro.EditorUtilities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UI;
|
||||
using static UnityEditor.Experimental.GraphView.GraphView;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
@@ -14,7 +16,7 @@ public class Player : MonoBehaviour
|
||||
Jumping,
|
||||
Falling,
|
||||
Stunned,
|
||||
Eliminated,
|
||||
Dead,
|
||||
Loser,
|
||||
Winner,
|
||||
}
|
||||
@@ -81,8 +83,16 @@ public class Player : MonoBehaviour
|
||||
{
|
||||
public CharacterController Controller;
|
||||
public InputActionAsset InputActions;
|
||||
public GameObject DiePrefab;
|
||||
}
|
||||
|
||||
/*[System.Serializable]
|
||||
public class Events
|
||||
{
|
||||
public UnityEvent OnLose;
|
||||
public UnityEvent OnDie;
|
||||
}*/
|
||||
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
@@ -123,6 +133,7 @@ public class Player : MonoBehaviour
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
//[SerializeField] private Events _events;
|
||||
[SerializeField, ReadOnly] private StateContainer _state;
|
||||
|
||||
public StateContainer State => _state;
|
||||
@@ -134,6 +145,41 @@ public class Player : MonoBehaviour
|
||||
private const float MAX_GRAVITY = -50f;
|
||||
#endregion
|
||||
|
||||
#region Public Fields
|
||||
|
||||
public void Pause(bool pause = true)
|
||||
{
|
||||
_state.IsPaused = pause;
|
||||
}
|
||||
|
||||
public void SetState(PlayerState state)
|
||||
{
|
||||
_state.CurrentState = state;
|
||||
|
||||
//TriggerStateEvents();
|
||||
}
|
||||
|
||||
public void Lose()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Loser)
|
||||
return;
|
||||
|
||||
SetState(PlayerState.Loser);
|
||||
Pause();
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Dead)
|
||||
return;
|
||||
|
||||
SetState(PlayerState.Dead);
|
||||
Instantiate(_references.DiePrefab);
|
||||
Debug.Log("Player is dead");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Fields
|
||||
// Inputs
|
||||
private InputAction _moveAction;
|
||||
@@ -209,6 +255,14 @@ public class Player : MonoBehaviour
|
||||
SetMovement(t);
|
||||
SetState();
|
||||
}
|
||||
|
||||
void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
if ((_settings.DeathLayer.value & (1 << hit.gameObject.layer)) != 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Player Logic
|
||||
@@ -387,7 +441,16 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void SetVelocity(float deltaTime)
|
||||
{
|
||||
Vector2 input = _moveAction.ReadValue<Vector2>();
|
||||
bool allowInput = !_state.IsPaused;
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
case PlayerState.Dead:
|
||||
allowInput = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Vector2 input = allowInput ? _moveAction.ReadValue<Vector2>() : Vector2.zero;
|
||||
|
||||
bool run = _runAction.IsPressed();
|
||||
_state.Running = Mathf.Clamp01(_state.Running + deltaTime * 4 * (run ? 1 : -1));
|
||||
@@ -473,6 +536,13 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void SetState()
|
||||
{
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
case PlayerState.Dead:
|
||||
return;
|
||||
}
|
||||
|
||||
if (State.IsGrounded || State.IsStickedToWall)
|
||||
{
|
||||
if (State.HorizontalVelocity.sqrMagnitude > .1f)
|
||||
@@ -495,6 +565,22 @@ public class Player : MonoBehaviour
|
||||
State.CurrentState = PlayerState.Falling;
|
||||
}
|
||||
}
|
||||
|
||||
//TriggerStateEvents();
|
||||
}
|
||||
|
||||
/*private void TriggerStateEvents()
|
||||
{
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
_events.OnLose?.Invoke();
|
||||
break;
|
||||
|
||||
case PlayerState.Dead:
|
||||
_events.OnDie?.Invoke();
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
#endregion
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public class PlayerAnimation : MonoBehaviour
|
||||
|
||||
_stateMapper[5] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Eliminated,
|
||||
PlayerState = Player.PlayerState.Dead,
|
||||
AnimatorState = "eliminate",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_eliminate",
|
||||
@@ -112,6 +112,15 @@ public class PlayerCamera : MonoBehaviour
|
||||
|
||||
private void SetPosition(float deltaTime)
|
||||
{
|
||||
if (PlayerDie.Instance && PlayerDie.Instance.Respawned)
|
||||
{
|
||||
Vector3 pos;
|
||||
Quaternion rot;
|
||||
PlayerDie.Instance.GetCameraTransformation(out pos, out rot);
|
||||
transform.SetPositionAndRotation(pos, rot);
|
||||
return;
|
||||
}
|
||||
|
||||
float t = (1.1f - _settings.FollowSmoothness) * 20 * deltaTime;
|
||||
_playerPosition = Vector3.Lerp(_playerPosition, _references.Target.position, t);
|
||||
|
||||
94
Assets/_Content/Scripts/Player/PlayerDie.cs
Normal file
94
Assets/_Content/Scripts/Player/PlayerDie.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PlayerDie : MonoBehaviour
|
||||
{
|
||||
public static PlayerDie Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public GameObject UI;
|
||||
public Camera Camera;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float DelayBeforeRespawn = 3;
|
||||
}
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private Settings _settings;
|
||||
|
||||
private bool _respawned;
|
||||
public bool Respawned => _respawned;
|
||||
|
||||
private bool _waitForPlayer;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
DontDestroyOnLoad(this);
|
||||
|
||||
_references.UI.gameObject.SetActive(false);
|
||||
_references.Camera.gameObject.SetActive(false);
|
||||
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
Invoke("ReloadScene", _settings.DelayBeforeRespawn);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
Instance = null;
|
||||
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_waitForPlayer)
|
||||
{
|
||||
if (Player.Instance)
|
||||
{
|
||||
_waitForPlayer = false;
|
||||
ShowUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Continue()
|
||||
{
|
||||
Player.Instance.SetState(Player.PlayerState.Idle);
|
||||
Player.Instance.Pause(false);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
public void GetCameraTransformation(out Vector3 position, out Quaternion rotation)
|
||||
{
|
||||
position = _references.Camera.transform.position;
|
||||
rotation = _references.Camera.transform.rotation;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
_waitForPlayer = true;
|
||||
}
|
||||
|
||||
private void ReloadScene()
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
private void ShowUI()
|
||||
{
|
||||
Player.Instance.Pause();
|
||||
Player.Instance.SetState(Player.PlayerState.Loser);
|
||||
transform.position = Player.Instance.transform.position;
|
||||
_references.UI.gameObject.SetActive(true);
|
||||
_respawned = true;
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/PlayerDie.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/PlayerDie.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afe227544adc2cb4eb8fc89a8b115f7e
|
||||
Reference in New Issue
Block a user