Checkpoint
This commit is contained in:
@@ -1,16 +1,35 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class Checkpoint : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
public static Checkpoint Active { get; private set; }
|
||||
|
||||
[SerializeField] private bool _isDefault;
|
||||
[SerializeField] private UnityEvent _onActivated;
|
||||
[SerializeField] private UnityEvent _onDisable;
|
||||
|
||||
public void Activate()
|
||||
{
|
||||
|
||||
if (Active)
|
||||
Active.Disable();
|
||||
|
||||
Active = this;
|
||||
_onActivated?.Invoke();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
public void Disable()
|
||||
{
|
||||
|
||||
if (Active == this)
|
||||
{
|
||||
Active = null;
|
||||
_onDisable?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (_isDefault)
|
||||
Activate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System;
|
||||
using UnityEngine.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Player controller.
|
||||
@@ -95,8 +96,8 @@ public class Player : MonoBehaviour
|
||||
[Tooltip("Current health points")]
|
||||
public int CurrentHealth;
|
||||
|
||||
[Tooltip("Checkpoint for respawn")]
|
||||
public Checkpoint Checkpoint;
|
||||
[Tooltip("Event on definitive die")]
|
||||
public UnityEvent OnDie;
|
||||
}
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
@@ -203,6 +204,8 @@ public class Player : MonoBehaviour
|
||||
_groundCheckRayOffset = cc.center + Vector3.up * (-cc.height * .5f - cc.skinWidth + _settings.GroundTolerance);
|
||||
_groundCheckSphereOffset = cc.center + Vector3.up * (-cc.height * .5f + cc.radius - cc.skinWidth - _settings.GroundTolerance);
|
||||
_groundCheckRadius = cc.radius;
|
||||
|
||||
_health.CurrentHealth = _health.MaxHealth;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
@@ -297,16 +300,7 @@ public class Player : MonoBehaviour
|
||||
_state.CurrentState = PlayerState.Stunned;
|
||||
|
||||
if (duration > 0)
|
||||
Invoke(nameof(RecoverFromStun), duration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recover from stun.
|
||||
/// </summary>
|
||||
public void RecoverFromStun()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Stunned)
|
||||
_state.CurrentState = PlayerState.Idle;
|
||||
Invoke(nameof(Recover), duration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -319,6 +313,43 @@ public class Player : MonoBehaviour
|
||||
_state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
|
||||
_state.Ground = null;
|
||||
_state.CurrentState = PlayerState.Eliminated;
|
||||
|
||||
Invoke(nameof(GoToCheckpoint), 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recover from stun.
|
||||
/// </summary>
|
||||
public void GoToCheckpoint()
|
||||
{
|
||||
if (Checkpoint.Active && _health.CurrentHealth > 0)
|
||||
{
|
||||
_health.CurrentHealth--;
|
||||
|
||||
transform.position = Checkpoint.Active.transform.position;
|
||||
|
||||
Debug.Log("Go to last checkpoint");
|
||||
|
||||
Recover();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Player is dead");
|
||||
|
||||
_health.OnDie.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recover from stun.
|
||||
/// </summary>
|
||||
public void Recover()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Stunned ||
|
||||
_state.CurrentState == PlayerState.Eliminated)
|
||||
_state.CurrentState = PlayerState.Idle;
|
||||
|
||||
_references.Controller.enabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
24
Assets/Content/Scripts/PlayerEvent.cs
Normal file
24
Assets/Content/Scripts/PlayerEvent.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class PlayerEvent : MonoBehaviour
|
||||
{
|
||||
public UnityEvent OnPlayerEnter;
|
||||
public UnityEvent OnPlayerExit;
|
||||
|
||||
void OnTriggerEnter(Collider col)
|
||||
{
|
||||
if (Player.Instance && col.gameObject == Player.Instance.gameObject)
|
||||
{
|
||||
OnPlayerEnter?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerExit(Collider col)
|
||||
{
|
||||
if (Player.Instance && col.gameObject == Player.Instance.gameObject)
|
||||
{
|
||||
OnPlayerExit?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Content/Scripts/PlayerEvent.cs.meta
Normal file
2
Assets/Content/Scripts/PlayerEvent.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e20e5bd115065c42b0cd4c112c16203
|
||||
Reference in New Issue
Block a user