/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo
{
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Character.Abilities;
using Opsive.UltimateCharacterController.Demo.Objects;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;
///
/// Disables or removes objects when the ride ability is active.
///
public class RideDisabler : MonoBehaviour
{
[Tooltip("A reference to Blitz.")]
[SerializeField] protected GameObject m_Blitz;
[Tooltip("The doors that should be locked when the ride ability is active.")]
[SerializeField] protected Door[] m_Doors;
[Tooltip("The objects that should be deactivated when the ride ability is active.")]
[SerializeField] protected GameObject[] m_Objects;
private GameObject m_Nolan;
private bool m_RideActive;
private bool m_BlitzInTrigger;
///
/// Initialize the default values.
///
private void Awake()
{
var demoManager = FindObjectOfType();
m_Nolan = demoManager.Character;
EventHandler.RegisterEvent(m_Nolan, "OnCharacterAbilityActive", OnAbilityActive);
EventHandler.RegisterEvent(m_Blitz, "OnCharacterChangeMovingPlatforms", OnCharacterChangeMovingPlatforms);
}
///
/// The character's ability has been started or stopped.
///
/// The ability which was started or stopped.
/// True if the ability was started, false if it was stopped.
private void OnAbilityActive(Ability ability, bool active)
{
if (!(ability is Ride)) {
return;
}
// Lock the doors and disable the objects if the ride ability starts to restrict the locations that the rideable object can move.
for (int i = 0; i < m_Doors.Length; ++i) {
m_Doors[i].Locked = active;
}
for (int i = 0; i < m_Objects.Length; ++i) {
m_Objects[i].SetActive(!active && !m_BlitzInTrigger);
}
m_RideActive = active;
}
///
/// The character's moving platform object has changed.
///
/// The moving platform to set. Can be null.
private void OnCharacterChangeMovingPlatforms(Transform movingPlatform)
{
for (int i = 0; i < m_Objects.Length; ++i) {
m_Objects[i].SetActive(movingPlatform != null);
}
}
///
/// An object has entered the trigger.
///
/// The object that entered the trigger.
private void OnTriggerEnter(Collider other)
{
// A main character collider is required.
if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) {
return;
}
var characterLocomotion = other.gameObject.GetComponentInParent();
if (characterLocomotion == null) {
return;
}
if (characterLocomotion.gameObject != m_Blitz) {
return;
}
// The objects should not be activated when Blitz is in the trigger.
m_BlitzInTrigger = true;
}
///
/// An object has exited the trigger.
///
/// The collider that exited the trigger.
private void OnTriggerExit(Collider other)
{
// No actions are required if Blitz isn't in the trigger.
if (!m_BlitzInTrigger) {
return;
}
// A main character collider is required.
if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) {
return;
}
var characterLocomotion = other.gameObject.GetComponentInParent();
if (characterLocomotion == null) {
return;
}
if (characterLocomotion.gameObject != m_Blitz) {
return;
}
// Blitz is no longer in the trigger - reactivate the objects.
m_BlitzInTrigger = false;
for (int i = 0; i < m_Objects.Length; ++i) {
m_Objects[i].SetActive(!m_RideActive && !m_BlitzInTrigger);
}
}
///
/// The object has been destroyed.
///
private void OnDestroy()
{
EventHandler.UnregisterEvent(m_Nolan, "OnCharacterAbilityActive", OnAbilityActive);
EventHandler.UnregisterEvent(m_Blitz, "OnCharacterChangeMovingPlatforms", OnCharacterChangeMovingPlatforms);
}
}
}