171 lines
4.4 KiB
C#
171 lines
4.4 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 扳手螺丝
|
|
/// </summary>
|
|
public class Tool_SpannerScrew : Tool_Base
|
|
{
|
|
/// <summary>
|
|
/// 是否已拧紧
|
|
/// </summary>
|
|
public bool isInstall;
|
|
/// <summary>
|
|
/// 上螺丝位置
|
|
/// </summary>
|
|
public Transform installPos;
|
|
/// <summary>
|
|
/// 是否带电
|
|
/// </summary>
|
|
public bool hasElectricity;
|
|
|
|
/// <summary>
|
|
/// 安装状态时Y的local值
|
|
/// </summary>
|
|
public float initPostionY;
|
|
|
|
/// <summary>
|
|
/// 螺丝拆装事件回调
|
|
/// </summary>
|
|
private Action<bool> installAction;
|
|
/// <summary>
|
|
/// 螺丝判断条件回调
|
|
/// </summary>
|
|
private Func<bool> checkCanMove;
|
|
|
|
/// <summary>
|
|
/// 扳手旋转位置1
|
|
/// </summary>
|
|
public float maxLocalAng;
|
|
/// <summary>
|
|
/// 扳手旋转位置2
|
|
/// </summary>
|
|
public float minLocalAng;
|
|
|
|
public void AddinstallAction(Action<bool> action)
|
|
{
|
|
this.installAction = action;
|
|
}
|
|
public void AddCheckAction(Func<bool> checkCanMove)
|
|
{
|
|
this.checkCanMove = checkCanMove;
|
|
}
|
|
protected override void OnMDown()
|
|
{
|
|
base.OnMDown();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 螺丝是否可以拧
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool CanMove()
|
|
{
|
|
if (checkCanMove == null)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return checkCanMove.Invoke();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被拧紧
|
|
/// </summary>
|
|
/// <param name="screwdriver"></param>
|
|
public void BeInstalled(Tool_Spanner screwdriver)
|
|
{
|
|
//mesh转螺丝不转
|
|
int isjian = -1;
|
|
screwdriver.transform.localEulerAngles = new Vector3(0, maxLocalAng, 0);
|
|
float tmpY = maxLocalAng;
|
|
transform.DOLocalMoveY(initPostionY, 3)
|
|
.OnUpdate(() =>
|
|
{
|
|
transform.Find("mesh").Rotate(Vector3.forward, -Time.deltaTime * 360,Space.Self);
|
|
|
|
tmpY = Mathf.Clamp(tmpY + isjian * Time.deltaTime * 180, minLocalAng, maxLocalAng);
|
|
screwdriver.transform.localEulerAngles = new Vector3(0, tmpY, 0);
|
|
|
|
if (isjian == -1)
|
|
{
|
|
if (tmpY <= minLocalAng)
|
|
{
|
|
isjian *= -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (tmpY >= maxLocalAng)
|
|
{
|
|
isjian *= -1;
|
|
}
|
|
}
|
|
|
|
})
|
|
.OnComplete(() =>
|
|
{
|
|
Debug.Log("螺丝已拧紧");
|
|
screwdriver.transform.parent = null;
|
|
isInstall = true;
|
|
isMoving = false;
|
|
screwdriver.isMoving = false;
|
|
installAction?.Invoke(true);
|
|
screwdriver.ReBackHead();
|
|
CallScoreAction(true);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 被拧松
|
|
/// </summary>
|
|
/// <param name="screwdriver"></param>
|
|
public void BeUnInstalled(Tool_Spanner screwdriver)
|
|
{
|
|
//mesh转螺丝不转
|
|
int isjian = 1;
|
|
screwdriver.transform.localEulerAngles = new Vector3(0, minLocalAng, 0);
|
|
float tmpY = minLocalAng;
|
|
transform.DOLocalMoveY(initPostionY - 0.03f, 3)
|
|
.OnUpdate(() =>
|
|
{
|
|
transform.Find("mesh").Rotate(Vector3.forward, Time.deltaTime*360,Space.Self);
|
|
|
|
tmpY = Mathf.Clamp(tmpY + isjian*Time.deltaTime * 180, minLocalAng, maxLocalAng);
|
|
screwdriver.transform.localEulerAngles = new Vector3(0, tmpY, 0);
|
|
|
|
if (isjian == -1)
|
|
{
|
|
if (tmpY<=minLocalAng)
|
|
{
|
|
isjian *= -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (tmpY>=maxLocalAng)
|
|
{
|
|
isjian *= -1;
|
|
}
|
|
}
|
|
})
|
|
.OnComplete(() =>
|
|
{
|
|
Debug.Log("螺丝已拧松");
|
|
screwdriver.transform.parent = null;
|
|
isInstall = false;
|
|
isMoving = false;
|
|
screwdriver.isMoving = false;
|
|
installAction?.Invoke(false);
|
|
screwdriver.ReBackHead();
|
|
CallScoreAction(false);
|
|
});
|
|
}
|
|
}
|