using DG.Tweening; using ShadowStudio.Tool; using System.Collections; using System.Collections.Generic; using UnityEngine; public class ModelSynchronize : MonoBehaviour { public bool isSyn = false; public bool isPlayer = false; public bool isOwnDo = false; public string id; public ModelData data; public ModelData Playerdata; public float Speed = 3; private float framSpeed; private float rotSpeed; private float dis; private float pross; private float doTimes = 0.4f; private TransferCell transferCeel; private PlayerAniSynchronize playerAni; public void Start() { Speed = 3; if (isPlayer) { TransformData transformData = new TransformData(Vector3.zero,Vector3.zero,Vector3.one); data = new ModelData(NetWorkLANManager.Instance.isServer?"server":"client","GamePlayer", transformData,false, false,true); isSyn = true; SynchronizeManager.Instance.AddModelSynchronize(this); MsgHandler.AddListener(NetWorkLANManager.Instance.isServer ? "client" : "server", SetData); Playerdata = new ModelData(NetWorkLANManager.Instance.isServer ? "server" : "client", "GamePlayer", transformData, false, false, true); gameObject.SetActive(false); } } public void Init(ModelData data) { this.data = data; this.id = data.id; this.isPlayer = data.isPlayer; MsgHandler.AddListener(data.id, SetData); transform.localPosition = data.transformData.pos; transform.localEulerAngles = data.transformData.eul; SynchronizeManager.Instance.AddModelSynchronize(this); //if (isPlayer) //{ // isSyn = true; // return; //} transferCeel = GetComponent(); transform.localScale = data.transformData.scale; } public ModelData GetData() { if(isPlayer ) { if(OpenXRCamera.Instance.head != null) { Playerdata.transformData.pos = OpenXRCamera.Instance.head.position; Playerdata.transformData.eul = OpenXRCamera.Instance.head.localEulerAngles; Playerdata.transformData.scale = Vector3.one; return Playerdata; } } else { data.transformData.pos = transform.position; data.transformData.eul = transform.eulerAngles; data.transformData.scale = transform.localScale; } return data; } public void SetData(ModelData data) { if(isPlayer) { this.data = data; if (playerAni == null) playerAni = GetComponent(); gameObject.SetActive(true); playerAni.SetData(data); return; } if (isOwnDo) return; this.data = data; Debug.Log("SetData "+ this.data.id); //transform.position = Vector3.Lerp(transform.position, data.transformData.pos, Time.deltaTime * 10f); //Quaternion rot = Quaternion.Euler(data.transformData.eul); //transform.rotation = Quaternion.Lerp(transform.rotation, rot, Time.deltaTime * 10f); //transform.DOLocalMove(data.transformData.pos, doTimes); //transform.DOLocalRotate(data.transformData.eul, doTimes); //transform.localPosition = Vector3.Lerp(transform.localPosition, data.transformData.pos, (Time.deltaTime * 3f)); Quaternion q = Quaternion.Euler(data.transformData.eul); transform.localRotation = Quaternion.Lerp(transform.localRotation, q, 0.2f); // transform.DOScale(data.transformData.scale, doTimes); transform.localScale = Vector3.Lerp(transform.localScale, data.transformData.scale, 0.2f); // transform.localScale = Vector3.Lerp(transform.localScale, data.transformData.scale, Time.deltaTime * 10f); transferCeel.IsDragEnable = data.isShowBox; // transferCeel.IsLock = data.isSnatch; if (data.isSnatch) { transferCeel.OnEditTransfer(TransferState.Start); } else { transferCeel.OnEditTransfer(TransferState.Stop); } } private void Update() { if (data != null &&transform.position!= data.transformData.pos) { framSpeed = Time.deltaTime * Speed; dis = Vector3.Distance(transform.position, data.transformData.pos); pross = 0; if (framSpeed > dis) { transform.position = data.transformData.pos; } else { pross = framSpeed / dis; transform.position = Vector3.Lerp(transform.position, data.transformData.pos, pross); } //rotSpeed = Time.deltaTime * Speed; //dis = Vector3.Distance(transform.eulerAngles, data.transformData.eul); //if (rotSpeed > dis) //{ // transform.eulerAngles = data.transformData.eul; //} //else //{ // pross = framSpeed / dis; // transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, data.transformData.eul, pross); //} } } public void isShowBox( bool value) { data.isShowBox = value; EditorModelManager.Instance.SetModelEditor(Vector3.zero, Vector3.zero, value,data.id); } private Vector3 point; private Vector3 forwad; public void isSnatch(TransferState state) { bool value = state == TransferState.Start ? true : false; if (data.isSnatch != value && value) { isOwnDo = value; point = transferCeel.GetCastPoint(OpenXRCamera.Instance.head.position); point.y -= 0.1f; forwad = point - transform.position; forwad.y = 0; EditorModelManager.Instance.SetModelEditor(point, -forwad, !value, data.id); } data.isSnatch = value; } public void isSnatch(bool value) { isSyn = value; if (!value) { GetData(); isOwnDo = value; } //if (isOwnDo == value) // return; // isOwnDo = value; // data.isSnatch = value; point = transferCeel.GetCastPoint(OpenXRCamera.Instance.head.position); point.y -= 0.1f; forwad = point - transform.position; forwad.y = 0; EditorModelManager.Instance.SetModelEditor(point, -forwad, !value, data.id); } private void OnDisable() { MsgHandler.RemoveListener(id, SetData); } }