/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Traits
{
using Opsive.Shared.Events;
using UnityEngine;
///
/// Extends the Respawner by listening/executing character related events.
///
public class CharacterRespawner : Respawner
{
private bool m_Active;
///
/// Initialize the default values.
///
protected override void Awake()
{
base.Awake();
EventHandler.RegisterEvent(m_GameObject, "OnCharacterActivate", OnActivate);
m_Active = true;
}
///
/// Does the respawn by setting the position and rotation to the specified values.
/// Enable the GameObject and let all of the listening objects know that the object has been respawned.
///
/// The respawn position.
/// The respawn rotation.
/// Was the position or rotation changed?
public override void Respawn(Vector3 position, Quaternion rotation, bool transformChange)
{
base.Respawn(position, rotation, transformChange);
// Execute OnCharacterImmediateTransformChange after OnRespawn to ensure all of the interested components are using the new position/rotation.
if (transformChange) {
EventHandler.ExecuteEvent(m_GameObject, "OnCharacterImmediateTransformChange", true);
}
}
///
/// The object has been disabled.
///
protected override void OnDisable()
{
// If the GameObject was deactivated then the respawner shouldn't respawn.
if (m_Active) {
base.OnDisable();
}
}
///
/// The character has been activated or deactivated.
///
/// Was the character activated?
private void OnActivate(bool activate)
{
m_Active = activate;
}
///
/// The GameObject has been destroyed.
///
protected override void OnDestroy()
{
base.OnDestroy();
EventHandler.UnregisterEvent(m_GameObject, "OnCharacterActivate", OnActivate);
}
}
}