ArtContainerHandler.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. using DG.Tweening;
  2. using LitJson;
  3. using PublicTools.Unity;
  4. using ShadowStudio.Model;
  5. using ShadowStudio.Tool;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11. using XRTool.Util;
  12. using static BoundingBox;
  13. namespace ShadowStudio.Model
  14. {
  15. /// <summary>
  16. /// 容器抽象类
  17. /// </summary>
  18. public abstract class ArtContainerHandler : MonoBehaviour, ArtContainer
  19. {
  20. public ArtType artType;
  21. private ArtInfo info;
  22. //private GoodState goodsState = GoodState.Init;
  23. /// <summary>
  24. /// 同步时不会传递初始化符合
  25. /// </summary>
  26. private TransferState transferState = TransferState.Init;
  27. public ArtInfo Info { get => info; set => info = value; }
  28. public float smoothTween = 3f;
  29. /// <summary>
  30. /// 保存的向服务器发送数据的对象
  31. /// </summary>
  32. public GoodsInfo data = new GoodsInfo();
  33. private ArtHandler handler;
  34. private Transform editorRoot;
  35. public virtual Transform EditorRoot { get => editorRoot; set => editorRoot = value; }
  36. public virtual Vector3 Position
  37. {
  38. get => transform.localPosition; set
  39. {
  40. transform.localPosition = value;
  41. if (Transfer)
  42. {
  43. Transfer.OnRotateStopped();
  44. }
  45. }
  46. }
  47. public virtual Vector3 Angle
  48. {
  49. get => transform.localEulerAngles;
  50. set
  51. {
  52. transform.localEulerAngles = value;
  53. if (Transfer)
  54. {
  55. Transfer.OnRotateStopped();
  56. }
  57. }
  58. }
  59. public virtual Vector3 Scale
  60. {
  61. get => transform.localScale;
  62. set
  63. {
  64. transform.localScale = value;
  65. if (Transfer)
  66. {
  67. Transfer.OnScaleStopped();
  68. }
  69. //SendTransfer(2, (int)TransferState.Stop);
  70. }
  71. }
  72. public virtual float Distance => Vector3.Distance(Camera.main.transform.position, transform.position);
  73. private float alpha = 1;
  74. public virtual float Alpha { get => 1; set => alpha = value; }
  75. private Color goodsColor = Color.white;
  76. private Timer autoUnLockTimer;
  77. private ArtComponent artComponent;
  78. public virtual Color GoodsColor { get => Color.white; set => goodsColor = value; }
  79. /// <summary>
  80. /// 虚假的定义
  81. /// </summary>
  82. public virtual TransferCell Transfer { get => null; set { } }
  83. /// <summary>
  84. /// 物体的状态
  85. /// 同步服务器的状态
  86. /// </summary>
  87. public TransferState TransferState
  88. {
  89. get => transferState; set
  90. {
  91. if (transferState != value)
  92. {
  93. transferState = value;
  94. ///同步开始,锁定物体
  95. if (transferState == TransferState.Start)
  96. {
  97. ClearTimer();
  98. LockContainer();
  99. }
  100. ///同步结束,结束锁定
  101. else if (transferState == TransferState.Over)
  102. {
  103. ClearTimer();
  104. UnLockContainer();
  105. }
  106. ///姿态同步完成,自动倒计时解除锁定状态
  107. else if (transferState == TransferState.Stop)
  108. {
  109. TransferSyn(data, false);
  110. ClearTimer();
  111. autoUnLockTimer = TimerMgr.Instance.CreateTimer(() =>
  112. {
  113. TransferState = TransferState.Over;
  114. }, 7f);
  115. }
  116. }
  117. }
  118. }
  119. public ArtComponent ArtComponent { get => artComponent; }
  120. private void ClearTimer()
  121. {
  122. if (autoUnLockTimer != null && TimerMgr.Instance != null)
  123. {
  124. autoUnLockTimer = TimerMgr.Instance.DestroyTimer(autoUnLockTimer);
  125. }
  126. }
  127. public virtual void OnDisable()
  128. {
  129. ClearTimer();
  130. }
  131. /// <summary>
  132. /// 添加美术资源到容器中
  133. /// </summary>
  134. /// <param name="art"></param>
  135. /// <param name="info"></param>
  136. public virtual void AddToContainer(UnityEngine.Object art, ArtInfo info)
  137. {
  138. this.Info = info;
  139. name = name + "_" + info.ArtName;
  140. //if (ArtComponent != null)
  141. //{
  142. // ArtComponent.SetContainer(this, art, info);
  143. //}
  144. Debug.Log("添加AddToContainer");
  145. if (art is GameObject)
  146. {
  147. GameObject obj = art as GameObject;
  148. //artTaskChild = obj.GetComponentInChildren<ArtTaskChild>();
  149. //if (artTaskChild != null)
  150. //{
  151. // XBoundingBox xBox = Transfer.BoundBox;
  152. // xBox.ActiveHandle = HandleType.Rotation;
  153. // xBox.ShowRotationHandleForX = false;
  154. // xBox.ShowRotationHandleForY = true;
  155. // xBox.ShowRotationHandleForZ = false;
  156. // xBox.ActiveAxis = AxisType.None;
  157. // artTaskChild.artContainerHandler = this;
  158. //}
  159. //artTaskAction = obj.GetComponentInChildren<ArtTaskAction>();
  160. //if (artTaskAction != null)
  161. //{
  162. // XBoundingBox xBox = Transfer.BoundBox;
  163. // xBox.ActiveHandle = HandleType.None;
  164. // xBox.ShowRotationHandleForX = false;
  165. // xBox.ShowRotationHandleForY = false;
  166. // xBox.ShowRotationHandleForZ = false;
  167. // xBox.ActiveAxis = AxisType.None;
  168. // artTaskAction.artContainerHandler = this;
  169. //}
  170. }
  171. }
  172. /// <summary>
  173. /// 销毁此物体
  174. /// </summary>
  175. public virtual void DestroyArt()
  176. {
  177. if (ArtComponent != null)
  178. {
  179. ArtComponent.DestroyArt();
  180. }
  181. Destroy(gameObject);
  182. }
  183. /// <summary>
  184. /// 获取资源的类型
  185. /// </summary>
  186. /// <returns></returns>
  187. public virtual ArtType GetArtType()
  188. {
  189. return artType;
  190. }
  191. /// <summary>
  192. /// 获取此info的信息
  193. /// </summary>
  194. /// <returns></returns>
  195. public virtual GoodsInfo GetGoodsInfo()
  196. {
  197. return this.data;
  198. }
  199. /// <summary>
  200. /// 获取实例化的数据信息
  201. /// </summary>
  202. /// <returns></returns>
  203. public virtual GameObject GetInstace()
  204. {
  205. return gameObject;
  206. }
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. /// <param name="isShow"></param>
  211. public virtual void SetArtActive(bool isShow)
  212. {
  213. gameObject.SetActive(isShow);
  214. }
  215. public virtual void PlayEffect()
  216. {
  217. StartCoroutine(ContainerEffect());
  218. }
  219. private IEnumerator ContainerEffect()
  220. {
  221. while (string.IsNullOrEmpty(data.goods_info))
  222. {
  223. yield return new WaitForFixedUpdate();
  224. }
  225. if (!string.IsNullOrEmpty(data.goods_info))
  226. {
  227. transform.DOKill();
  228. Posture posture = UnityUtil.GetPosture(data.goods_info);
  229. Vector3 endPos = Camera.main.transform.TransformPoint(posture.position);
  230. bool isClone = false;
  231. if (data.status == (int)TransferState.Clone)
  232. {
  233. isClone = true;
  234. data.status = (int)TransferState.Init;
  235. }
  236. Vector3 endScale = posture.scale;
  237. if (!isClone)
  238. {
  239. //if (IconItem.ItemPos != Vector3.zero)
  240. //{
  241. // // Vector3 startPos = GameSession.Instance.GetHeadForwadPos(info.Distance / 5f, false);
  242. // Vector3 startPos = IconItem.ItemPos;
  243. // transform.position = startPos;
  244. // transform.DOMove(endPos, 1f).SetEase<Tween>(Ease.OutCubic);
  245. // IconItem.ItemPos = Vector3.zero;
  246. //}
  247. //else if (ItemView.itemPos != Vector3.zero)
  248. //{
  249. // // Vector3 startPos = GameSession.Instance.GetHeadForwadPos(info.Distance / 5f, false);
  250. // Vector3 startPos = ItemView.itemPos;
  251. // transform.position = startPos;
  252. // transform.DOMove(endPos, 1f).SetEase<Tween>(Ease.OutCubic);
  253. // IconItem.ItemPos = Vector3.zero;
  254. //}
  255. //else
  256. //{
  257. transform.position = endPos;
  258. //}
  259. }
  260. else
  261. {
  262. transform.localEulerAngles = posture.angle;
  263. transform.localScale = posture.scale;
  264. Vector3 startPos = endPos - Camera.main.transform.right * 0.5f;
  265. transform.position = startPos;
  266. transform.DOMove(endPos, 1f).SetEase<Tween>(Ease.OutCubic);
  267. }
  268. if (!isClone)
  269. {
  270. Vector3 startScale = endScale / 2f;
  271. transform.localScale = startScale;
  272. }
  273. transform.DOScale(endScale, 0.8f).SetEase<Tween>(Ease.OutCubic).SetDelay(0.2f).OnComplete(() =>
  274. {
  275. TransferState = TransferState.Over;
  276. TransferSyn(data, false);
  277. // transform.DOKill();
  278. }).OnKill(() =>
  279. {
  280. TransferState = TransferState.Over;
  281. TransferSyn(data, false);
  282. });
  283. }
  284. }
  285. /// <summary>
  286. /// 传输同步信息
  287. /// </summary>
  288. /// <param name="state"></param>
  289. /// <param name="status"></param>
  290. public virtual void SendTransfer(int state, int status)
  291. {
  292. if (!string.IsNullOrEmpty(data.goods_info))
  293. {
  294. data.goods_info = UnityUtil.TransferToString(transform, state);
  295. data.status = status;
  296. if (status == (int)TransferState.Stop)
  297. {
  298. ResetEditorPosition();
  299. //GameNode.Instance.PageGoods[data.id] = data;
  300. }
  301. if (Info != null)
  302. {
  303. ///单人模式下,减少数据传输
  304. if (ArtInfoMgr.Instance.IsSingleMan() && status == (int)TransferState.Doing)
  305. {
  306. return;
  307. }
  308. if (Info.ImmediateSyn)
  309. {
  310. ArtInfoMgr.Instance.SendGoodsTransfer(data);
  311. }
  312. else
  313. {
  314. ArtInfoMgr.Instance.SendTransfer(data);
  315. }
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 同步物体状态
  321. /// 初始化状态时,不同步
  322. /// 当物体处于catch或者release状态时进行同步
  323. /// </summary>
  324. /// <param name="goods"></param>
  325. /// <param name="smooth"></param>
  326. public virtual void TransferSyn(GoodsInfo goods, bool smooth = true)
  327. {
  328. //if (!string.IsNullOrEmpty(goods.goods_info))
  329. //{
  330. // data = goods;
  331. // Posture posture = UnityUtil.GetPosture(goods.goods_info);
  332. // if (TransferState != TransferState.Init)
  333. // {
  334. // if ((TransferState)goods.status != TransferState.Init)
  335. // {
  336. // TransferState = (TransferState)goods.status;
  337. // }
  338. // if (TransferState == TransferState.Stop)
  339. // {
  340. // smooth = false;
  341. // GameNode.Instance.PageGoods[data.id] = data;
  342. // }
  343. // else if (TransferState == TransferState.Over)
  344. // {
  345. // if (ArtComponent != null)
  346. // {
  347. // ArtComponent.TransferSyn(goods, smooth);
  348. // }
  349. // return;
  350. // }
  351. // if (smooth)
  352. // {
  353. // if (posture.count >= 0)
  354. // {
  355. // transform.localPosition = Vector3.Lerp(transform.localPosition, posture.position, (Time.deltaTime * smoothTween));
  356. // }
  357. // if (posture.count >= 1)
  358. // {
  359. // Quaternion q = Quaternion.Euler(posture.angle);
  360. // transform.localRotation = Quaternion.Lerp(transform.localRotation, q, (Time.deltaTime * smoothTween));
  361. // }
  362. // if (posture.count >= 2)
  363. // {
  364. // transform.localScale = Vector3.Lerp(transform.localScale, posture.scale, (Time.deltaTime * smoothTween));
  365. // }
  366. // }
  367. // else
  368. // {
  369. // if (posture.count >= 0)
  370. // {
  371. // transform.DOLocalMove(posture.position, Vector3.Distance(posture.position, transform.position) / (smoothTween * smoothTween));
  372. // }
  373. // if (posture.count >= 1)
  374. // {
  375. // transform.DOLocalRotate(posture.angle, 0.1f);
  376. // }
  377. // if (posture.count >= 2)
  378. // {
  379. // transform.DOScale(posture.scale, 0.1f);
  380. // }
  381. // }
  382. // }
  383. //}
  384. //if (ArtComponent != null)
  385. //{
  386. // ArtComponent.TransferSyn(goods, smooth);
  387. //}
  388. //if (artTaskAction != null)
  389. //{
  390. // JsonData dataInfo = new JsonData();
  391. // try
  392. // {
  393. // dataInfo = JsonMapper.ToObject(data.goods_extended);
  394. // if (dataInfo["task"] != null)
  395. // {
  396. // artTaskAction.showType(dataInfo["task"].ToString());
  397. // }
  398. // }
  399. // catch
  400. // {
  401. // }
  402. //}
  403. }
  404. /// <summary>
  405. /// 锁定此容器
  406. /// 将此容器设置为不可编辑状态,取消此容器的选中效果等等
  407. /// </summary>
  408. public virtual void LockContainer()
  409. {
  410. }
  411. /// <summary>
  412. /// 解锁此容器
  413. /// 将此容器设置为可编辑状态
  414. /// </summary>
  415. public virtual void UnLockContainer()
  416. {
  417. }
  418. /// <summary>
  419. /// 当处于Release状态时可被编辑(删除)
  420. /// </summary>
  421. /// <returns></returns>
  422. public bool CanEditor()
  423. {
  424. return TransferState == TransferState.Over;
  425. }
  426. public void DelArtSync()
  427. {
  428. //WSHandler.Room.DeleteGood(new int[] { data.id });
  429. }
  430. public virtual Texture GetContainerIcon()
  431. {
  432. if (handler != null)
  433. {
  434. return handler.GetIcon();
  435. }
  436. return null;
  437. }
  438. public virtual void GetContainerIcon(Action<Texture> load)
  439. {
  440. if (handler != null)
  441. {
  442. handler.GetIcon(load);
  443. }
  444. else
  445. {
  446. load?.Invoke(null);
  447. }
  448. }
  449. public virtual void SetArtHandler(ArtHandler handler)
  450. {
  451. this.handler = handler;
  452. }
  453. /// <summary>
  454. /// 设置此容器的选中状态
  455. /// </summary>
  456. public virtual void SetSelect()
  457. {
  458. }
  459. public virtual void UnSetSelect()
  460. {
  461. }
  462. public Transform GetEditorCenter()
  463. {
  464. if (!EditorRoot)
  465. {
  466. var obj = new GameObject(name + "_EditorRoot");
  467. EditorRoot = obj.transform;
  468. UnityUtil.SetParent(transform, EditorRoot);
  469. }
  470. ResetEditorPosition();
  471. return EditorRoot;
  472. }
  473. /// <summary>
  474. /// 计算位置信息
  475. /// </summary>
  476. public virtual void ResetEditorPosition()
  477. {
  478. }
  479. /// <summary>
  480. /// 后退
  481. /// </summary>
  482. /// <returns></returns>
  483. public virtual bool GoBack()
  484. {
  485. return false;
  486. }
  487. /// <summary>
  488. /// 前进
  489. /// </summary>
  490. /// <returns></returns>
  491. public virtual bool GoAhead()
  492. {
  493. return false;
  494. }
  495. /// <summary>
  496. /// 后退的步骤数量
  497. /// </summary>
  498. /// <returns></returns>
  499. public virtual int BackCount()
  500. {
  501. return 0;
  502. }
  503. /// <summary>
  504. /// 前进的步骤数量
  505. /// </summary>
  506. /// <returns></returns>
  507. public virtual int AheadCount()
  508. {
  509. return 0;
  510. }
  511. public virtual Vector3 GetClonePoint()
  512. {
  513. var root = GetEditorCenter();
  514. Vector3 point = transform.position;
  515. point = root.InverseTransformPoint(point);
  516. point += Vector3.right * 0.5f;
  517. return root.TransformPoint(point);
  518. }
  519. public virtual void CloneContainer()
  520. {
  521. //Vector3 point = GetClonePoint();
  522. //string datainfo = UnityUtil.TransferToString(GameNode.Instance.transform.InverseTransformPoint(point), transform.localEulerAngles, transform.localScale, 2);
  523. //GoodsInfo tmp = data;
  524. //tmp.id = 0;
  525. //tmp.goods_info = datainfo;
  526. //tmp.status = (int)TransferState.Clone;
  527. //WSHandler.Room.CreateGood(tmp);
  528. //Destroy(tmp);
  529. }
  530. /// <summary>
  531. /// 设置对应的组件
  532. /// </summary>
  533. /// <param name="component"></param>
  534. public virtual void AddArtComponent(ArtComponent component)
  535. {
  536. this.artComponent = component;
  537. try
  538. {
  539. if (artComponent != null && artComponent.GetInstace())
  540. {
  541. Transform target = artComponent.GetInstace().transform;
  542. if (target)
  543. {
  544. UnityUtil.SetParent(transform, target);
  545. }
  546. }
  547. }
  548. catch (Exception ex)
  549. {
  550. UnityLog.LogError(ex.ToString());
  551. artComponent = null;
  552. }
  553. }
  554. public virtual void UpdateProcess(float process)
  555. {
  556. }
  557. public void ArtState(string type, string info)
  558. {
  559. JsonData dataInfo = new JsonData();
  560. try
  561. {
  562. dataInfo = JsonMapper.ToObject(data.goods_extended);
  563. }
  564. catch
  565. {
  566. }
  567. dataInfo[type] = info;
  568. data.goods_extended = dataInfo.ToJson();
  569. Debug.Log("Art状态信息改变" + data.goods_extended);
  570. SendTransfer(0, (int)TransferState.Stop);
  571. }
  572. }
  573. }