using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 螺丝 /// public class Tool_Screw : Tool_Base { /// /// 标识 /// public string id; /// /// 是否已拧紧 /// public bool isInstall; /// /// 上螺丝位置 /// public Transform installPos; /// /// 是否带电 /// public bool hasElectricity; /// /// 正在移动不能操作 /// private bool isMoving; /// /// 安装状态时Y的local值 /// public float initPostionY; /// /// 螺丝拆装事件回调 /// private Action installAction; //protected override void OnAwake() //{ // base.OnAwake(); // id = gameObject.name; //} public void AddAction(Action action) { this.installAction=action; } /// /// 被拧紧 /// /// 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); }); }); } } /// /// 被拧松 /// /// 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); }); }); } } }