Menu
This commit is contained in:
@@ -43,6 +43,9 @@ public class Player : MonoBehaviour
|
||||
[Tooltip("Layers considered as ground")]
|
||||
public LayerMask GroundLayer = 1;
|
||||
|
||||
[Tooltip("Layers considered as death zone")]
|
||||
public LayerMask DeathLayer = 0;
|
||||
|
||||
[Header("Forces")]
|
||||
|
||||
[Tooltip("Decay rate of extra forces (m/s²)")]
|
||||
@@ -83,11 +86,26 @@ public class Player : MonoBehaviour
|
||||
public Vector3 HorizontalVelocity => new Vector3(Velocity.x, 0, Velocity.z);
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class HealthContainer
|
||||
{
|
||||
[Tooltip("Maximum health points")]
|
||||
public int MaxHealth = 3;
|
||||
|
||||
[Tooltip("Current health points")]
|
||||
public int CurrentHealth;
|
||||
|
||||
[Tooltip("Checkpoint for respawn")]
|
||||
public Checkpoint Checkpoint;
|
||||
}
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private StateContainer _state;
|
||||
[SerializeField] private HealthContainer _health;
|
||||
|
||||
public StateContainer State => _state;
|
||||
public HealthContainer Health => _health;
|
||||
|
||||
#region Private Fields
|
||||
private InputAction _moveAction;
|
||||
@@ -152,6 +170,14 @@ public class Player : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
if ((_settings.DeathLayer & (1 << hit.gameObject.layer)) != 0)
|
||||
{
|
||||
Eliminate();
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
@@ -194,6 +220,7 @@ public class Player : MonoBehaviour
|
||||
void Update()
|
||||
{
|
||||
if (_state.IsPaused) return;
|
||||
if (!_references.Controller.enabled) return;
|
||||
|
||||
float deltaTime = Time.deltaTime;
|
||||
|
||||
@@ -232,6 +259,29 @@ public class Player : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
/// <summary>
|
||||
/// Teleport player to position.
|
||||
/// </summary>
|
||||
public void Teleport(Vector3 position)
|
||||
{
|
||||
_references.Controller.enabled = false;
|
||||
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
|
||||
transform.position = position;
|
||||
|
||||
_references.Controller.enabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the pause state of the player.
|
||||
/// </summary>
|
||||
public void Pause(bool paused)
|
||||
{
|
||||
_state.IsPaused = paused;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stuns for a specified duration (0 = infinite).
|
||||
/// </summary>
|
||||
@@ -240,8 +290,11 @@ public class Player : MonoBehaviour
|
||||
if (_state.CurrentState == PlayerState.Eliminated)
|
||||
return;
|
||||
|
||||
_references.Controller.enabled = false;
|
||||
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
_state.CurrentState = PlayerState.Stunned;
|
||||
_state.Velocity = Vector3.zero;
|
||||
|
||||
if (duration > 0)
|
||||
Invoke(nameof(RecoverFromStun), duration);
|
||||
@@ -261,17 +314,35 @@ public class Player : MonoBehaviour
|
||||
/// </summary>
|
||||
public void Eliminate()
|
||||
{
|
||||
_state.CurrentState = PlayerState.Eliminated;
|
||||
_state.Velocity = Vector3.zero;
|
||||
_references.Controller.enabled = false;
|
||||
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
_state.CurrentState = PlayerState.Eliminated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the pause state of the player.
|
||||
/// Set the player loser.
|
||||
/// </summary>
|
||||
public void Pause(bool paused)
|
||||
public void Loose()
|
||||
{
|
||||
_state.IsPaused = paused;
|
||||
_references.Controller.enabled = false;
|
||||
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
_state.CurrentState = PlayerState.Loser;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the player winner.
|
||||
/// </summary>
|
||||
public void Win()
|
||||
{
|
||||
_references.Controller.enabled = false;
|
||||
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
_state.CurrentState = PlayerState.Winner;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -432,7 +503,7 @@ public class Player : MonoBehaviour
|
||||
impulseForce.y = _impulseForce.y;
|
||||
_impulseForce = impulseForce;
|
||||
|
||||
// Decay inherited platform velocity — reset when back on ground
|
||||
// Decay inherited platform velocity: reset when back on ground
|
||||
if (_state.IsGrounded)
|
||||
{
|
||||
_platformVelocity = Vector3.zero;
|
||||
|
||||
Reference in New Issue
Block a user