1118OPSSNew/Assets/Zion/Scripts/GameScene/Driver/CarPartsDriver.cs

159 lines
4.4 KiB
C#

using UnityEngine;
/// <summary>
/// 二维指令动作
/// </summary>
public class CarPartsDriver : MonoBehaviour
{
public static CarPartsDriver instance;
private float driveTime = 0;
/// <summary>
/// 当前播放声音
/// </summary>
private FunctionSync_Audio currentAudio;
/// <summary>
/// 当前声音时长
/// </summary>
private float currentTime;
private MechanismsDriver currentMsg;
private bool isPlay = false;
public int orderCode;
public int _state;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isPlay)
{
if (currentMsg.name == "支腿")
{
PlayAudio(currentAudio, currentTime);
Debug.Log("zhengche");
}
else
{
currentAudio.SetAudio(AudioControlEnum.Stop);
Debug.Log("qita");
}
}
if (Input.GetKeyDown(KeyCode.P))
{
DriveMechanism(orderCode, _state);
}
}
/// <summary>
/// 接收动作
/// </summary>
/// <param name="orderCode"></param>
/// <param name="_state"></param>
public void DriveMechanism(int orderCode, int _state)
{
foreach (var item in GetComponentsInChildren<MechanismsDriver>())
{
if (item.reciveOrderType == (ReciveOrderType)orderCode)
{
item.driverState.myint = _state;
item.driverState.SendSync();
if (item.sync_Audio != null)
{
switch (_state)
{
case 1:
currentAudio = item.sync_Audio;
currentMsg = item;
currentTime = item.openTime;
item.sync_Audio.SetAudio(AudioControlEnum.Play);
isPlay = true;
break;
case -1:
currentAudio = item.sync_Audio;
currentMsg = item;
currentTime = item.closeTime;
item.sync_Audio.SetAudio(AudioControlEnum.Play);
isPlay = true;
break;
default:
break;
}
}
}
}
}
private void PlayAudio(FunctionSync_Audio sync_Audio, float time)
{
if (driveTime < time)
{
driveTime += Time.deltaTime;
}
if (driveTime >= time)
{
driveTime = 0;
sync_Audio.SetAudio(AudioControlEnum.Stop);
isPlay = false;
}
}
private void PlayAudio(MechanismsDriver mechanismsDriver, FunctionSync_Audio sync_Audio, float time)
{
if (mechanismsDriver.limitCrank != null)
{
if (mechanismsDriver.limitCrank.Angle == mechanismsDriver.limitCrank.range.min || mechanismsDriver.limitCrank.Angle == mechanismsDriver.limitCrank.range.max)
{
sync_Audio.SetAudio(AudioControlEnum.Stop);
isPlay = false;
}
}
else
{
if (driveTime < time)
{
driveTime += Time.deltaTime;
}
if (driveTime >= time)
{
driveTime = 0;
sync_Audio.SetAudio(AudioControlEnum.Stop);
isPlay = false;
}
}
}
public void DriveMechanism(ReciveOrderType reciveOrderType, int _state)
{
foreach (var item in GetComponentsInChildren<MechanismsDriver>())
{
if (item.reciveOrderType == reciveOrderType)
{
item.InitMechanisms(_state);
}
}
}
/// <summary>
/// 停止
/// </summary>
public void StopMechanism()
{
foreach (var item in GetComponentsInChildren<MechanismsDriver>())
{
item.driverState.myint = 0;
item.driverState.SendSync();
if (item.sync_Audio != null)
{
isPlay = false;
item.sync_Audio.SetAudio(AudioControlEnum.Stop);
}
}
}
}