/*
* 程序功能:
* 举臂移动 MoveTo(移动目标点,速度)
* 举臂复位 ActionReset()
*/
using System;
using System.Collections.Generic;
using System.Linq;
using AX.InputSystem;
using AX.MessageSystem;
using UnityEngine;
///
/// 手臂动作
///
public class ArmAction : MonoBehaviour
{
///
/// 可伸缩的起始点
///
public Transform StartTrans;
///
/// 可伸缩的结束点
///
public Transform EndTrans;
private List list;
Queue actions = new Queue();
[HideInInspector]
public Vector3 targetPosition;
public bool IsArm;
private void Awake()
{
list = GetComponentsInChildren().ToList();
}
private void OnEnable()
{
if (list == null)
{
list = GetComponentsInChildren().ToList();
}
}
private void Start()
{
MessageDispatcher.AddListener("AUTO_LIFT_TRUCK_ARM_COMMAND", AutoArm);
}
private void AutoArm(IMessage message)
{
var gameObjID = (long)message.Sender;
if (gameObjID == GetComponent().GameObjID)
{
//var targetPosition = ((TargetCmdArgs)message.Data).targetPosition;
//MoveTo(targetPosition, 20f);
IsArm = true;
}
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("AUTO_LIFT_TRUCK_ARM_COMMAND", AutoArm);
}
///
/// 举臂移动
///
///
///
public void MoveTo(Vector3 position, float speed)
{
targetPosition = position;
//根据帧索引升序排列
actions.Clear();
list = list.OrderBy(o => o.FrameIndex).ToList();
//向队列中添加动作
foreach (var item in list)
{
item.Speed = speed;
item.OnFinished = MoveNext;
actions.Enqueue(item);
}
//移动前停止
Stop();
//下一次移动
MoveNext();
}
///
/// 停止所有帧动画
///
private void Stop()
{
//先停止所有帧动作
foreach (var item in actions)
{
item.Stop();
}
}
///
/// 复位
///
public void ActionReset()
{
IsArm = false;
//根据帧索引倒序排列
actions.Clear();
list = list.OrderByDescending(o => o.FrameIndex).ToList();
//向队列中添加动作
foreach (var item in list)
{
item.OnFinished = ResetNext;
actions.Enqueue(item);
}
//移动前停止
Stop();
//下一次移动
ResetNext();
}
///
/// 下一次移动
///
private void MoveNext()
{
//如果动画队列中还有动作,继续播放
if (actions.Count > 0)
{
//从队列中取出动作
ArmActionFrame action = actions.Dequeue();
action.Play();
//action.OnFinished = MoveNext;
}
//else
//{
// //检查高度是否合适
// float temp = Vector3.Distance(targetPosition,EndTrans.position);
// //如果temp<16抬高手臂
// if (temp<16)
// {
// ArmActionFrame action = list[1];
// action.MoveUp();
// }
// else
// {
// ArmActionFrame action = actions.Dequeue();
// action.Play();
// }
//}
}
///
/// 下一次复位
///
private void ResetNext()
{
//如果动画队列中还有动作,继续播放
if (actions.Count > 0)
{
//从队列中取出动作
ArmActionFrame action = actions.Dequeue();
action.ResetArm();
//action.OnFinished = ResetNext;
}
}
}