ArtContainerHandler.cs 20 KB

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