ArtContainerHandler.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. Debug.LogError(transform.name + " DoScale 1 ");
  285. transform.DOScale(endScale, 0.8f).SetEase<Tween>(Ease.OutCubic).SetDelay(0.2f).OnComplete(() =>
  286. {
  287. TransferState = TransferState.Over;
  288. TransferSyn(data, false);
  289. // transform.DOKill();
  290. }).OnKill(() =>
  291. {
  292. TransferState = TransferState.Over;
  293. TransferSyn(data, false);
  294. });
  295. }
  296. }
  297. /// <summary>
  298. /// 传输同步信息
  299. /// </summary>
  300. /// <param name="state"></param>
  301. /// <param name="status"></param>
  302. public virtual void SendTransfer(int state, int status)
  303. {
  304. if (!string.IsNullOrEmpty(data.goods_info))
  305. {
  306. data.goods_info = UnityUtil.TransferToString(transform, state);
  307. data.status = status;
  308. if (status == (int)TransferState.Stop)
  309. {
  310. ResetEditorPosition();
  311. GameNode.Instance.PageGoods[data.id] = data;
  312. }
  313. if (Info != null)
  314. {
  315. ///单人模式下,减少数据传输
  316. if (ArtInfoMgr.Instance.IsSingleMan() && status == (int)TransferState.Doing)
  317. {
  318. return;
  319. }
  320. if (Info.ImmediateSyn)
  321. {
  322. ArtInfoMgr.Instance.SendGoodsTransfer(data);
  323. }
  324. else
  325. {
  326. ArtInfoMgr.Instance.SendTransfer(data);
  327. }
  328. }
  329. }
  330. }
  331. /// <summary>
  332. /// 同步物体状态
  333. /// 初始化状态时,不同步
  334. /// 当物体处于catch或者release状态时进行同步
  335. /// </summary>
  336. /// <param name="goods"></param>
  337. /// <param name="smooth"></param>
  338. public virtual void TransferSyn(GoodsInfo goods, bool smooth = true)
  339. {
  340. if (!string.IsNullOrEmpty(goods.goods_info))
  341. {
  342. data = goods;
  343. Posture posture = UnityUtil.GetPosture(goods.goods_info);
  344. if (TransferState != TransferState.Init)
  345. {
  346. if ((TransferState)goods.status != TransferState.Init)
  347. {
  348. TransferState = (TransferState)goods.status;
  349. }
  350. if (TransferState == TransferState.Stop)
  351. {
  352. smooth = false;
  353. GameNode.Instance.PageGoods[data.id] = data;
  354. }
  355. else if (TransferState == TransferState.Over)
  356. {
  357. if (ArtComponent != null)
  358. {
  359. ArtComponent.TransferSyn(goods, smooth);
  360. }
  361. return;
  362. }
  363. if (smooth)
  364. {
  365. if (posture.count >= 0)
  366. {
  367. transform.localPosition = Vector3.Lerp(transform.localPosition, posture.position, (Time.deltaTime * smoothTween));
  368. }
  369. if (posture.count >= 1)
  370. {
  371. Quaternion q = Quaternion.Euler(posture.angle);
  372. transform.localRotation = Quaternion.Lerp(transform.localRotation, q, (Time.deltaTime * smoothTween));
  373. }
  374. if (posture.count >= 2)
  375. {
  376. Debug.Log(transform.name+" localScale " + 1);
  377. transform.localScale = Vector3.Lerp(transform.localScale, posture.scale, (Time.deltaTime * smoothTween));
  378. }
  379. }
  380. else
  381. {
  382. if (posture.count >= 0)
  383. {
  384. transform.DOLocalMove(posture.position, Vector3.Distance(posture.position, transform.position) / (smoothTween * smoothTween));
  385. }
  386. if (posture.count >= 1)
  387. {
  388. transform.DOLocalRotate(posture.angle, 0.1f);
  389. }
  390. if (posture.count >= 2)
  391. {
  392. Debug.LogError(transform.name + " DoScale 2 ");
  393. // if(artType == ArtType.WorldDlg) { }
  394. transform.DOScale(posture.scale, 0.1f);
  395. // transform.localScale
  396. }
  397. }
  398. }
  399. }
  400. if (ArtComponent != null)
  401. {
  402. ArtComponent.TransferSyn(goods, smooth);
  403. }
  404. if (artTaskAction != null)
  405. {
  406. JsonData dataInfo = new JsonData();
  407. try
  408. {
  409. dataInfo = JsonMapper.ToObject(data.goods_extended);
  410. if (dataInfo["task"] != null)
  411. {
  412. artTaskAction.showType(dataInfo["task"].ToString());
  413. }
  414. }
  415. catch
  416. {
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 锁定此容器
  422. /// 将此容器设置为不可编辑状态,取消此容器的选中效果等等
  423. /// </summary>
  424. public virtual void LockContainer()
  425. {
  426. }
  427. /// <summary>
  428. /// 解锁此容器
  429. /// 将此容器设置为可编辑状态
  430. /// </summary>
  431. public virtual void UnLockContainer()
  432. {
  433. }
  434. /// <summary>
  435. /// 当处于Release状态时可被编辑(删除)
  436. /// </summary>
  437. /// <returns></returns>
  438. public bool CanEditor()
  439. {
  440. return TransferState == TransferState.Over;
  441. }
  442. public void DelArtSync()
  443. {
  444. WSHandler.Room.DeleteGood(new int[] { data.id });
  445. }
  446. public virtual Texture GetContainerIcon()
  447. {
  448. if (handler != null)
  449. {
  450. return handler.GetIcon();
  451. }
  452. return null;
  453. }
  454. public virtual void GetContainerIcon(Action<Texture> load)
  455. {
  456. if (handler != null)
  457. {
  458. handler.GetIcon(load);
  459. }
  460. else
  461. {
  462. load?.Invoke(null);
  463. }
  464. }
  465. public virtual void SetArtHandler(ArtHandler handler)
  466. {
  467. this.handler = handler;
  468. }
  469. /// <summary>
  470. /// 设置此容器的选中状态
  471. /// </summary>
  472. public virtual void SetSelect()
  473. {
  474. }
  475. public virtual void UnSetSelect()
  476. {
  477. }
  478. public Transform GetEditorCenter()
  479. {
  480. if (!EditorRoot)
  481. {
  482. var obj = new GameObject(name + "_EditorRoot");
  483. EditorRoot = obj.transform;
  484. UnityUtil.SetParent(transform, EditorRoot);
  485. }
  486. ResetEditorPosition();
  487. return EditorRoot;
  488. }
  489. /// <summary>
  490. /// 计算位置信息
  491. /// </summary>
  492. public virtual void ResetEditorPosition()
  493. {
  494. }
  495. /// <summary>
  496. /// 后退
  497. /// </summary>
  498. /// <returns></returns>
  499. public virtual bool GoBack()
  500. {
  501. return false;
  502. }
  503. /// <summary>
  504. /// 前进
  505. /// </summary>
  506. /// <returns></returns>
  507. public virtual bool GoAhead()
  508. {
  509. return false;
  510. }
  511. /// <summary>
  512. /// 后退的步骤数量
  513. /// </summary>
  514. /// <returns></returns>
  515. public virtual int BackCount()
  516. {
  517. return 0;
  518. }
  519. /// <summary>
  520. /// 前进的步骤数量
  521. /// </summary>
  522. /// <returns></returns>
  523. public virtual int AheadCount()
  524. {
  525. return 0;
  526. }
  527. public virtual Vector3 GetClonePoint()
  528. {
  529. var root = GetEditorCenter();
  530. Vector3 point = transform.position;
  531. point = root.InverseTransformPoint(point);
  532. point += Vector3.right * 0.5f;
  533. return root.TransformPoint(point);
  534. }
  535. public virtual void CloneContainer()
  536. {
  537. Vector3 point = GetClonePoint();
  538. string datainfo = UnityUtil.TransferToString(GameNode.Instance.transform.InverseTransformPoint(point), transform.localEulerAngles, transform.localScale, 2);
  539. GoodsInfo tmp = data;
  540. tmp.rid = Info.AID;
  541. tmp.goods_info = datainfo;
  542. tmp.status = (int)TransferState.Clone;
  543. WSHandler.Room.CreateGood(tmp);
  544. //Destroy(tmp);
  545. }
  546. /// <summary>
  547. /// 设置对应的组件
  548. /// </summary>
  549. /// <param name="component"></param>
  550. public virtual void AddArtComponent(ArtComponent component)
  551. {
  552. this.artComponent = component;
  553. try
  554. {
  555. if (artComponent != null && artComponent.GetInstace())
  556. {
  557. Transform target = artComponent.GetInstace().transform;
  558. if (target)
  559. {
  560. UnityUtil.SetParent(transform, target);
  561. }
  562. }
  563. }
  564. catch (Exception ex)
  565. {
  566. UnityLog.Instance.LogError(ex.ToString());
  567. artComponent = null;
  568. }
  569. }
  570. public virtual void UpdateProcess(float process)
  571. {
  572. }
  573. public void ArtState(string type, string info)
  574. {
  575. JsonData dataInfo = new JsonData();
  576. try
  577. {
  578. dataInfo = JsonMapper.ToObject(data.goods_extended);
  579. }
  580. catch
  581. {
  582. }
  583. dataInfo[type] = info;
  584. data.goods_extended = dataInfo.ToJson();
  585. Debug.Log("Art状态信息改变" + data.goods_extended);
  586. SendTransfer(0, (int)TransferState.Stop);
  587. }
  588. }
  589. }