using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 扳手螺丝 /// public class Tool_SpannerScrew : Tool_Base { /// /// 是否已拧紧 /// public bool isInstall; /// /// 上螺丝位置 /// public Transform installPos; /// /// 是否带电 /// public bool hasElectricity; /// /// 安装状态时Y的local值 /// public float initPostionY; /// /// 螺丝拆装事件回调 /// private Action installAction; /// /// 螺丝判断条件回调 /// private Func checkCanMove; /// /// 扳手旋转位置1 /// public float maxLocalAng; /// /// 扳手旋转位置2 /// public float minLocalAng; public void AddinstallAction(Action action) { this.installAction = action; } public void AddCheckAction(Func checkCanMove) { this.checkCanMove = checkCanMove; } protected override void OnMDown() { base.OnMDown(); } /// /// 螺丝是否可以拧 /// /// public bool CanMove() { if (checkCanMove == null) { return true; } else { return checkCanMove.Invoke(); } } /// /// 被拧紧 /// /// 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); }); } /// /// 被拧松 /// /// 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); }); } }