ModelSynchronize.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using DG.Tweening;
  2. using ShadowStudio.Tool;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class ModelSynchronize : MonoBehaviour
  7. {
  8. public bool isSyn = false;
  9. public bool isPlayer = false;
  10. public bool isOwnDo = false;
  11. public string id;
  12. public ModelData data;
  13. public ModelData Playerdata;
  14. public float Speed = 3;
  15. private float framSpeed;
  16. private float rotSpeed;
  17. private float dis;
  18. private float pross;
  19. private float doTimes = 0.4f;
  20. private TransferCell transferCeel;
  21. private PlayerAniSynchronize playerAni;
  22. public void Start()
  23. {
  24. Speed = 3;
  25. if (isPlayer)
  26. {
  27. TransformData transformData = new TransformData(Vector3.zero,Vector3.zero,Vector3.one);
  28. data = new ModelData(NetWorkLANManager.Instance.isServer?"server":"client","GamePlayer", transformData,false, false,true);
  29. isSyn = true;
  30. SynchronizeManager.Instance.AddModelSynchronize(this);
  31. MsgHandler.AddListener(NetWorkLANManager.Instance.isServer ? "client" : "server", SetData);
  32. Playerdata = new ModelData(NetWorkLANManager.Instance.isServer ? "server" : "client", "GamePlayer", transformData, false, false, true);
  33. gameObject.SetActive(false);
  34. }
  35. }
  36. public void Init(ModelData data)
  37. {
  38. this.data = data;
  39. this.id = data.id;
  40. this.isPlayer = data.isPlayer;
  41. MsgHandler.AddListener(data.id, SetData);
  42. transform.localPosition = data.transformData.pos;
  43. transform.localEulerAngles = data.transformData.eul;
  44. SynchronizeManager.Instance.AddModelSynchronize(this);
  45. //if (isPlayer)
  46. //{
  47. // isSyn = true;
  48. // return;
  49. //}
  50. transferCeel = GetComponent<TransferCell>();
  51. transform.localScale = data.transformData.scale;
  52. }
  53. public ModelData GetData()
  54. {
  55. if(isPlayer )
  56. {
  57. if(OpenXRCamera.Instance.head != null)
  58. {
  59. Playerdata.transformData.pos = OpenXRCamera.Instance.head.position;
  60. Playerdata.transformData.eul = OpenXRCamera.Instance.head.localEulerAngles;
  61. Playerdata.transformData.scale = Vector3.one;
  62. return Playerdata;
  63. }
  64. }
  65. else
  66. {
  67. data.transformData.pos = transform.position;
  68. data.transformData.eul = transform.eulerAngles;
  69. data.transformData.scale = transform.localScale;
  70. }
  71. return data;
  72. }
  73. public void SetData(ModelData data)
  74. {
  75. if(isPlayer)
  76. {
  77. this.data = data;
  78. if (playerAni == null)
  79. playerAni = GetComponent<PlayerAniSynchronize>();
  80. gameObject.SetActive(true);
  81. playerAni.SetData(data);
  82. return;
  83. }
  84. if (isOwnDo)
  85. return;
  86. this.data = data;
  87. Debug.Log("SetData "+ this.data.id);
  88. //transform.position = Vector3.Lerp(transform.position, data.transformData.pos, Time.deltaTime * 10f);
  89. //Quaternion rot = Quaternion.Euler(data.transformData.eul);
  90. //transform.rotation = Quaternion.Lerp(transform.rotation, rot, Time.deltaTime * 10f);
  91. //transform.DOLocalMove(data.transformData.pos, doTimes);
  92. //transform.DOLocalRotate(data.transformData.eul, doTimes);
  93. //transform.localPosition = Vector3.Lerp(transform.localPosition, data.transformData.pos, (Time.deltaTime * 3f));
  94. Quaternion q = Quaternion.Euler(data.transformData.eul);
  95. transform.localRotation = Quaternion.Lerp(transform.localRotation, q, 0.2f);
  96. // transform.DOScale(data.transformData.scale, doTimes);
  97. transform.localScale = Vector3.Lerp(transform.localScale, data.transformData.scale, 0.2f);
  98. // transform.localScale = Vector3.Lerp(transform.localScale, data.transformData.scale, Time.deltaTime * 10f);
  99. transferCeel.IsDragEnable = data.isShowBox;
  100. // transferCeel.IsLock = data.isSnatch;
  101. if (data.isSnatch)
  102. {
  103. transferCeel.OnEditTransfer(TransferState.Start);
  104. }
  105. else
  106. {
  107. transferCeel.OnEditTransfer(TransferState.Stop);
  108. }
  109. }
  110. private void Update()
  111. {
  112. if (data != null &&transform.position!= data.transformData.pos)
  113. {
  114. framSpeed = Time.deltaTime * Speed;
  115. dis = Vector3.Distance(transform.position, data.transformData.pos);
  116. pross = 0;
  117. if (framSpeed > dis)
  118. {
  119. transform.position = data.transformData.pos;
  120. }
  121. else
  122. {
  123. pross = framSpeed / dis;
  124. transform.position = Vector3.Lerp(transform.position, data.transformData.pos, pross);
  125. }
  126. //rotSpeed = Time.deltaTime * Speed;
  127. //dis = Vector3.Distance(transform.eulerAngles, data.transformData.eul);
  128. //if (rotSpeed > dis)
  129. //{
  130. // transform.eulerAngles = data.transformData.eul;
  131. //}
  132. //else
  133. //{
  134. // pross = framSpeed / dis;
  135. // transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, data.transformData.eul, pross);
  136. //}
  137. }
  138. }
  139. public void isShowBox( bool value)
  140. {
  141. data.isShowBox = value;
  142. EditorModelManager.Instance.SetModelEditor(Vector3.zero, Vector3.zero, value,data.id);
  143. }
  144. private Vector3 point;
  145. private Vector3 forwad;
  146. public void isSnatch(TransferState state)
  147. {
  148. bool value = state == TransferState.Start ? true : false;
  149. if (data.isSnatch != value && value)
  150. {
  151. isOwnDo = value;
  152. point = transferCeel.GetCastPoint(OpenXRCamera.Instance.head.position);
  153. point.y -= 0.1f;
  154. forwad = point - transform.position;
  155. forwad.y = 0;
  156. EditorModelManager.Instance.SetModelEditor(point, -forwad, !value, data.id);
  157. }
  158. data.isSnatch = value;
  159. }
  160. public void isSnatch(bool value)
  161. {
  162. isSyn = value;
  163. if (!value)
  164. {
  165. GetData();
  166. isOwnDo = value;
  167. }
  168. //if (isOwnDo == value)
  169. // return;
  170. // isOwnDo = value;
  171. // data.isSnatch = value;
  172. point = transferCeel.GetCastPoint(OpenXRCamera.Instance.head.position);
  173. point.y -= 0.1f;
  174. forwad = point - transform.position;
  175. forwad.y = 0;
  176. EditorModelManager.Instance.SetModelEditor(point, -forwad, !value, data.id);
  177. }
  178. private void OnDisable()
  179. {
  180. MsgHandler.RemoveListener(id, SetData);
  181. }
  182. }