214 lines
5.0 KiB
C#
214 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Moonlit.FootstepPro;
|
|
using Moonlit.Utils;
|
|
using UnityEngine;
|
|
|
|
public class vp_FootstepManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class vp_SurfaceTypes
|
|
{
|
|
public Vector2 RandomPitch = new Vector2(1f, 1.5f);
|
|
|
|
public bool Foldout = true;
|
|
|
|
public bool SoundsFoldout = true;
|
|
|
|
public bool TexturesFoldout = true;
|
|
|
|
public string SurfaceName = string.Empty;
|
|
|
|
public List<AudioClip> Sounds = new List<AudioClip>();
|
|
|
|
public List<Texture> Textures = new List<Texture>();
|
|
}
|
|
|
|
private static vp_FootstepManager[] m_FootstepManagers;
|
|
|
|
public static bool mIsDirty = true;
|
|
|
|
public bool leftStep = true;
|
|
|
|
public List<vp_SurfaceTypes> SurfaceTypes = new List<vp_SurfaceTypes>();
|
|
|
|
public vp_SurfaceTypes waterSurface;
|
|
|
|
public vp_SurfaceTypes boatSurface;
|
|
|
|
protected vp_FPPlayerEventHandler m_Player;
|
|
|
|
protected vp_FPCamera m_Camera;
|
|
|
|
protected vp_FPController m_Controller;
|
|
|
|
protected FishingPlayer fishingPlayer;
|
|
|
|
protected AudioSource m_Audio;
|
|
|
|
protected AudioClip m_SoundToPlay;
|
|
|
|
protected AudioClip m_LastPlayedSound;
|
|
|
|
public static vp_FootstepManager[] FootstepManagers
|
|
{
|
|
get
|
|
{
|
|
if (mIsDirty)
|
|
{
|
|
mIsDirty = false;
|
|
m_FootstepManagers = UnityEngine.Object.FindObjectsOfType(typeof(vp_FootstepManager)) as vp_FootstepManager[];
|
|
if (m_FootstepManagers == null)
|
|
{
|
|
m_FootstepManagers = Resources.FindObjectsOfTypeAll(typeof(vp_FootstepManager)) as vp_FootstepManager[];
|
|
}
|
|
}
|
|
return m_FootstepManagers;
|
|
}
|
|
}
|
|
|
|
public bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
return mIsDirty;
|
|
}
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
m_Player = base.transform.root.GetComponentInChildren<vp_FPPlayerEventHandler>();
|
|
m_Camera = base.transform.root.GetComponentInChildren<vp_FPCamera>();
|
|
m_Controller = base.transform.root.GetComponentInChildren<vp_FPController>();
|
|
m_Audio = base.gameObject.AddComponent<AudioSource>();
|
|
fishingPlayer = m_Controller.GetComponent<FishingPlayer>();
|
|
foreach (vp_SurfaceTypes surfaceType in SurfaceTypes)
|
|
{
|
|
if (surfaceType.SurfaceName == "Footsteps_Water_01")
|
|
{
|
|
waterSurface = surfaceType;
|
|
}
|
|
if (surfaceType.SurfaceName == "Footsteps_Boat_01")
|
|
{
|
|
boatSurface = surfaceType;
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void SetDirty(bool dirty)
|
|
{
|
|
mIsDirty = dirty;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (m_Camera.BobStepCallback == null)
|
|
{
|
|
vp_FPCamera camera = m_Camera;
|
|
camera.BobStepCallback = (vp_FPCamera.BobStepDelegate)Delegate.Combine(camera.BobStepCallback, new vp_FPCamera.BobStepDelegate(Footstep));
|
|
}
|
|
}
|
|
|
|
protected virtual void OnEnable()
|
|
{
|
|
vp_FPCamera camera = m_Camera;
|
|
camera.BobStepCallback = (vp_FPCamera.BobStepDelegate)Delegate.Combine(camera.BobStepCallback, new vp_FPCamera.BobStepDelegate(Footstep));
|
|
}
|
|
|
|
protected virtual void OnDisable()
|
|
{
|
|
vp_FPCamera camera = m_Camera;
|
|
camera.BobStepCallback = (vp_FPCamera.BobStepDelegate)Delegate.Remove(camera.BobStepCallback, new vp_FPCamera.BobStepDelegate(Footstep));
|
|
}
|
|
|
|
public void MakeFootstep()
|
|
{
|
|
Footstep();
|
|
}
|
|
|
|
protected virtual void Footstep()
|
|
{
|
|
if (m_Player.Dead.Active || !m_Controller.Grounded)
|
|
{
|
|
return;
|
|
}
|
|
if ((bool)fishingPlayer && fishingPlayer.isInWater)
|
|
{
|
|
PlaySound(waterSurface);
|
|
}
|
|
else if ((bool)fishingPlayer && (bool)fishingPlayer.boatSimulator)
|
|
{
|
|
PlaySound(boatSurface);
|
|
}
|
|
else
|
|
{
|
|
if (m_Player.GroundTexture.Get() == null && m_Player.SurfaceType.Get() == null)
|
|
{
|
|
return;
|
|
}
|
|
if (!(m_Player.SurfaceType.Get() != null))
|
|
{
|
|
foreach (vp_SurfaceTypes surfaceType in SurfaceTypes)
|
|
{
|
|
foreach (Texture texture in surfaceType.Textures)
|
|
{
|
|
if (texture == m_Player.GroundTexture.Get())
|
|
{
|
|
PlaySound(surfaceType);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
PlaySound(SurfaceTypes[m_Player.SurfaceType.Get().SurfaceID]);
|
|
}
|
|
}
|
|
|
|
public virtual void PlaySound(vp_SurfaceTypes st)
|
|
{
|
|
if (Singleton<FootprintSystem>.IsInitialized && (st.SurfaceName == "Footsteps_Snow_01" || st.SurfaceName == "Footsteps_Sand_01"))
|
|
{
|
|
if (leftStep)
|
|
{
|
|
fishingPlayer.leftFoot.Place();
|
|
}
|
|
else
|
|
{
|
|
fishingPlayer.rightFoot.Place();
|
|
}
|
|
leftStep = !leftStep;
|
|
}
|
|
AudioObject audioObject = AudioController.Play(st.SurfaceName, m_Audio.transform);
|
|
if ((bool)audioObject)
|
|
{
|
|
audioObject.pitch = UnityEngine.Random.Range(st.RandomPitch.x, st.RandomPitch.y) * Time.timeScale;
|
|
}
|
|
}
|
|
|
|
public static int GetMainTerrainTexture(Vector3 worldPos, Terrain terrain)
|
|
{
|
|
TerrainData terrainData = terrain.terrainData;
|
|
Vector3 position = terrain.transform.position;
|
|
int x = (int)((worldPos.x - position.x) / terrainData.size.x * (float)terrainData.alphamapWidth);
|
|
int y = (int)((worldPos.z - position.z) / terrainData.size.z * (float)terrainData.alphamapHeight);
|
|
float[,,] alphamaps = terrainData.GetAlphamaps(x, y, 1, 1);
|
|
float[] array = new float[alphamaps.GetUpperBound(2) + 1];
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
array[i] = alphamaps[0, 0, i];
|
|
}
|
|
float num = 0f;
|
|
int result = 0;
|
|
for (int j = 0; j < array.Length; j++)
|
|
{
|
|
if (array[j] > num)
|
|
{
|
|
result = j;
|
|
num = array[j];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|