195 lines
6.5 KiB
C#
195 lines
6.5 KiB
C#
using DefaultNamespace;
|
|
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Batteryanimation : MonoBehaviour
|
|
{
|
|
[Header("电池id")]
|
|
public string ID;
|
|
[Header("所有电池所位移的点位和需要初始化设备点位")]
|
|
public Transform point1, point2, point3, point4, point5, point6, point7, point8;
|
|
[Header("电池需要初始化")]
|
|
public GameObject battery;
|
|
[Header("电池爪子")]
|
|
public Transform paw;
|
|
[Header("判断是否有电池")]
|
|
public Transform Batspoint;
|
|
/// <summary>
|
|
/// 初始化判断
|
|
/// </summary>
|
|
private bool ispcon = false;
|
|
private float speed = 1f;
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
/// 接收电池数据
|
|
/// </summary>
|
|
/// <param name="qcbd"></param>
|
|
public void Batterydata(Qcbd qcbd)
|
|
{
|
|
if (!ispcon)
|
|
{
|
|
if (qcbd.BatteryStatus1 != "0")
|
|
{
|
|
Initbattery(point6);
|
|
}
|
|
if (qcbd.BatteryStatus2 != "0")
|
|
{
|
|
Initbattery(point7);
|
|
}
|
|
if (qcbd.BatteryStatus3 != "0")
|
|
{
|
|
Initbattery(point8);
|
|
}
|
|
ispcon = true;
|
|
}
|
|
if (qcbd.Take0Battery == "True")
|
|
{
|
|
Takebattery(point1, point5);
|
|
}
|
|
else if (qcbd.Put0Battery == "True")
|
|
{
|
|
Putbattery(point1, point5);
|
|
}
|
|
else if (qcbd.Take1Battery == "True")
|
|
{
|
|
Batterywithdrawal(point2, point6);
|
|
}
|
|
else if (qcbd.Put1Battery == "True")
|
|
{
|
|
Dischargecell(point2, point6);
|
|
}
|
|
else if (qcbd.Take2Battery == "True")
|
|
{
|
|
Batterywithdrawal(point3, point7);
|
|
}
|
|
else if (qcbd.Put2Battery == "True")
|
|
{
|
|
Dischargecell(point3, point7);
|
|
}
|
|
else if (qcbd.Take3Battery == "True")
|
|
{
|
|
Batterywithdrawal(point4, point8);
|
|
}
|
|
else if (qcbd.Put3Battery == "True")
|
|
{
|
|
Dischargecell(point4, point8);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 电池初始化
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
public void Initbattery(Transform pos)
|
|
{
|
|
if (pos)
|
|
{
|
|
GameObject battery = Instantiate(this.battery, pos);
|
|
battery.transform.SetParent(pos);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 取小车电池
|
|
/// </summary>
|
|
public void Takebattery(Transform pos, Transform pos2)
|
|
{
|
|
if (pos && pos2)
|
|
{
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
paw.transform.DOMove(pos2.position, (Vector3.Distance(paw.transform.position, pos2.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
GameObject battery = Instantiate(this.battery, Batspoint);
|
|
battery.transform.SetParent(Batspoint);
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 给小车装电池
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <param name="pos2"></param>
|
|
public void Putbattery(Transform pos, Transform pos2)
|
|
{
|
|
if (pos && pos2)
|
|
{
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
paw.transform.DOMove(pos2.position, (Vector3.Distance(paw.transform.position, pos2.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
if (Batspoint.childCount > 0)
|
|
{
|
|
GameObject battery = Batspoint.transform.GetChild(0).gameObject;
|
|
if (battery != null)
|
|
{
|
|
Destroy(battery);
|
|
}
|
|
}
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 放电池
|
|
/// </summary>
|
|
public void Dischargecell(Transform pos, Transform pos2)
|
|
{
|
|
if (pos && pos2)
|
|
{
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
paw.transform.DOMove(pos2.position, (Vector3.Distance(paw.transform.position, pos2.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
if (Batspoint.childCount > 0 && pos2.childCount == 0)
|
|
{
|
|
GameObject bat = Batspoint.transform.GetChild(0).gameObject;
|
|
bat.transform.SetParent(null);
|
|
bat.transform.position = pos2.position;
|
|
bat.transform.SetParent(pos2);
|
|
}
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 取电池
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <param name="pos2"></param>
|
|
public void Batterywithdrawal(Transform pos, Transform pos2)
|
|
{
|
|
if (pos && pos2)
|
|
{
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
paw.transform.DOMove(pos2.position, (Vector3.Distance(paw.transform.position, pos2.position) / speed)).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
if (pos2.childCount > 0 && Batspoint.childCount == 0)
|
|
{
|
|
GameObject bat = pos2.transform.GetChild(0).gameObject;
|
|
if (bat != null)
|
|
{
|
|
bat.transform.SetParent(null);
|
|
bat.transform.position = Batspoint.position;
|
|
bat.transform.SetParent(Batspoint);
|
|
}
|
|
}
|
|
paw.transform.DOMove(pos.position, (Vector3.Distance(paw.transform.position, pos.position) / speed)).SetEase(Ease.InOutQuad);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|