You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.1 KiB
160 lines
4.1 KiB
4 years ago
|
/*
|
||
|
* 程序功能:
|
||
|
* 举臂移动 MoveTo(移动目标点,速度)
|
||
|
* 举臂复位 ActionReset()
|
||
|
*/
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using AX.InputSystem;
|
||
|
using AX.MessageSystem;
|
||
|
using UnityEngine;
|
||
|
/// <summary>
|
||
|
/// 手臂动作
|
||
|
/// </summary>
|
||
|
public class ArmAction : MonoBehaviour
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 可伸缩的起始点
|
||
|
/// </summary>
|
||
|
public Transform StartTrans;
|
||
|
/// <summary>
|
||
|
/// 可伸缩的结束点
|
||
|
/// </summary>
|
||
|
public Transform EndTrans;
|
||
|
private List<ArmActionFrame> list;
|
||
|
Queue<ArmActionFrame> actions = new Queue<ArmActionFrame>();
|
||
|
[HideInInspector]
|
||
|
public Vector3 targetPosition;
|
||
|
public bool IsArm;
|
||
|
private void Awake()
|
||
|
{
|
||
|
list = GetComponentsInChildren<ArmActionFrame>().ToList();
|
||
|
}
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
if (list == null)
|
||
|
{
|
||
|
list = GetComponentsInChildren<ArmActionFrame>().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<BaseGameObjInfo>().GameObjID)
|
||
|
{
|
||
|
//var targetPosition = ((TargetCmdArgs)message.Data).targetPosition;
|
||
|
//MoveTo(targetPosition, 20f);
|
||
|
IsArm = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnDestroy()
|
||
|
{
|
||
|
MessageDispatcher.RemoveListener("AUTO_LIFT_TRUCK_ARM_COMMAND", AutoArm);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 举臂移动
|
||
|
/// </summary>
|
||
|
/// <param name="position"></param>
|
||
|
/// <param name="speed"></param>
|
||
|
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();
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 停止所有帧动画
|
||
|
/// </summary>
|
||
|
private void Stop()
|
||
|
{
|
||
|
//先停止所有帧动作
|
||
|
foreach (var item in actions)
|
||
|
{
|
||
|
item.Stop();
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 复位
|
||
|
/// </summary>
|
||
|
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();
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 下一次移动
|
||
|
/// </summary>
|
||
|
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();
|
||
|
// }
|
||
|
//}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 下一次复位
|
||
|
/// </summary>
|
||
|
private void ResetNext()
|
||
|
{
|
||
|
//如果动画队列中还有动作,继续播放
|
||
|
if (actions.Count > 0)
|
||
|
{
|
||
|
//从队列中取出动作
|
||
|
ArmActionFrame action = actions.Dequeue();
|
||
|
action.ResetArm();
|
||
|
//action.OnFinished = ResetNext;
|
||
|
}
|
||
|
}
|
||
|
}
|