ArtContainerHandler.cs 20 KB

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