123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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<TransferCell>();
- 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<PlayerAniSynchronize>();
- 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);
- }
- }
|