132 lines
3.7 KiB
C#
132 lines
3.7 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 螺丝
|
|
/// </summary>
|
|
public class Tool_Screw : Tool_Base
|
|
{
|
|
/// <summary>
|
|
/// 标识
|
|
/// </summary>
|
|
public string id;
|
|
|
|
/// <summary>
|
|
/// 是否已拧紧
|
|
/// </summary>
|
|
public bool isInstall;
|
|
/// <summary>
|
|
/// 上螺丝位置
|
|
/// </summary>
|
|
public Transform installPos;
|
|
/// <summary>
|
|
/// 是否带电
|
|
/// </summary>
|
|
public bool hasElectricity;
|
|
|
|
/// <summary>
|
|
/// 正在移动不能操作
|
|
/// </summary>
|
|
private bool isMoving;
|
|
|
|
|
|
/// <summary>
|
|
/// 安装状态时Y的local值
|
|
/// </summary>
|
|
public float initPostionY;
|
|
|
|
/// <summary>
|
|
/// 螺丝拆装事件回调
|
|
/// </summary>
|
|
private Action<bool> installAction;
|
|
|
|
|
|
//protected override void OnAwake()
|
|
//{
|
|
// base.OnAwake();
|
|
// id = gameObject.name;
|
|
//}
|
|
|
|
public void AddAction(Action<bool> action)
|
|
{
|
|
this.installAction=action;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被拧紧
|
|
/// </summary>
|
|
/// <param name="screwdriver"></param>
|
|
public void BeInstalled(Tool_Screwdriver screwdriver)
|
|
{
|
|
if (!isMoving)
|
|
{
|
|
isMoving = true;
|
|
Debug.Log("开始拧紧螺丝");
|
|
//设置螺丝刀初始位置
|
|
screwdriver.transform.parent = null;
|
|
screwdriver.transform.DOLocalRotate(installPos.localEulerAngles, 0.5f);
|
|
screwdriver.transform.DOMove(installPos.position, 1).OnComplete(() =>
|
|
{
|
|
//动画
|
|
transform.DOLocalMoveY(initPostionY, 1)
|
|
.OnUpdate(() =>
|
|
{
|
|
transform.RotateAroundLocal(Vector3.up, 1);
|
|
screwdriver.transform.position = installPos.position;
|
|
screwdriver.transform.RotateAroundLocal(Vector3.right, 10);
|
|
})
|
|
.OnComplete(() =>
|
|
{
|
|
Debug.Log("螺丝已拧紧");
|
|
isInstall = true;
|
|
isMoving = false;
|
|
installAction?.Invoke(true);
|
|
screwdriver.ReBackHead();
|
|
screwdriver.isUseing = false;
|
|
CallScoreAction(true);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被拧松
|
|
/// </summary>
|
|
/// <param name="screwdriver"></param>
|
|
public void BeUnInstalled(Tool_Screwdriver screwdriver)
|
|
{
|
|
if (!isMoving)
|
|
{
|
|
isMoving = true;
|
|
Debug.Log("开始卸螺丝");
|
|
//设置螺丝刀初始位置
|
|
screwdriver.transform.parent = null;
|
|
screwdriver.transform.DOLocalRotate(installPos.localEulerAngles, 0.5f);
|
|
screwdriver.transform.DOMove(installPos.position, 1).OnComplete(() =>
|
|
{
|
|
//动画
|
|
transform.DOLocalMoveY(initPostionY - 0.02f, 1)
|
|
.OnUpdate(() =>
|
|
{
|
|
transform.RotateAroundLocal(Vector3.up, 1);
|
|
screwdriver.transform.position = installPos.position;
|
|
screwdriver.transform.RotateAroundLocal(Vector3.right, 10);
|
|
})
|
|
.OnComplete(() =>
|
|
{
|
|
Debug.Log("螺丝已拧松");
|
|
isInstall = false;
|
|
isMoving = false;
|
|
installAction?.Invoke(false);
|
|
screwdriver.ReBackHead();
|
|
screwdriver.isUseing = false;
|
|
CallScoreAction(false);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|