/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo.Car
{
using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Demo.UnityStandardAssets.Vehicles.Car;
using Opsive.UltimateCharacterController.Objects.CharacterAssist;
using UnityEngine;
///
/// Provides a sample implementation of the IDriveSource.
///
public class SkyCar : MonoBehaviour, IDriveSource
{
[Tooltip("A reference to the headlights that should turn on when the character enters the car.")]
[SerializeField] protected GameObject[] m_Headlights;
[Tooltip("A reference to the colliders that should be disabled when the character enters the car.")]
[SerializeField] protected GameObject[] m_Colliders;
[Tooltip("The location that the character drives from.")]
[SerializeField] protected Transform m_DriverLocation;
private static int s_OpenCloseDoorParameter = Animator.StringToHash("OpenCloseDoor");
private GameObject m_GameObject;
private Transform m_Transform;
private Animator m_Animator;
private Rigidbody m_Rigidbody;
private CarUserControl m_UserControl;
private CarAudio m_Audio;
private AnimatorMonitor m_CharacterAnimatorMonitor;
private int m_HorizontalInputID;
private bool m_OpenedDoor;
public GameObject GameObject { get => m_GameObject; }
public Transform Transform { get => m_Transform; }
public Transform DriverLocation { get => m_DriverLocation; }
public int AnimatorID { get => 0; }
///
/// Initialize the default values.
///
private void Awake()
{
m_GameObject = gameObject;
m_Transform = transform;
m_Animator = GetComponent();
m_Rigidbody = GetComponent();
m_UserControl = GetComponent();
m_Audio = GetComponent();
m_HorizontalInputID = Animator.StringToHash("HorizontalInput");
EnableDisableCar(false);
}
///
/// Enables or disables the car components.
///
/// Should the car be enabled?
private void EnableDisableCar(bool enable)
{
enabled = m_UserControl.enabled = m_Audio.enabled = enable;
m_Rigidbody.isKinematic = !enable;
for (int i = 0; i < m_Headlights.Length; ++i) {
m_Headlights[i].SetActive(enable);
}
for (int i = 0; i < m_Colliders.Length; ++i) {
m_Colliders[i].SetActive(!enable);
}
}
///
/// The character has started to enter the vehicle.
///
/// The character that is entering the vehicle.
public void EnterVehicle(GameObject character)
{
EventHandler.RegisterEvent(character, "OnAnimatorOpenCloseDoor", OpenCloseDoor);
}
///
/// Triggers the OpenCloseDoor parameter.
///
private void OpenCloseDoor()
{
m_OpenedDoor = !m_OpenedDoor;
m_Animator.SetTrigger(s_OpenCloseDoorParameter);
}
///
/// The character has entered the vehicle.
///
/// The character that entered the vehicle.
public void EnteredVehicle(GameObject character)
{
m_CharacterAnimatorMonitor = character.GetCachedComponent();
EnableDisableCar(true);
}
///
/// Updates the animator.
///
public void Update()
{
m_Animator.SetFloat(m_HorizontalInputID, m_CharacterAnimatorMonitor.AbilityFloatData, 0, 0);
}
///
/// The character has started to exit the vehicle.
///
/// The character that is exiting the vehicle.
public void ExitVehicle(GameObject character)
{
EnableDisableCar(false);
}
///
/// The character has exited the vehicle.
///
/// The character that exited the vehicle.
public void ExitedVehicle(GameObject character)
{
EventHandler.UnregisterEvent(character, "OnAnimatorOpenCloseDoor", OpenCloseDoor);
if (m_OpenedDoor) {
OpenCloseDoor();
}
}
}
}