SwapManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. using System.Collections;
  3. public class SwapManager
  4. {
  5. public const float PREPARING_SWAP_DURATION = 3f;
  6. public const float SWAP_CD = 3f;
  7. public int targetSwapId;
  8. public bool isPreparingSwap;
  9. public float preparingSwapStartTime;
  10. public bool requestedSwap;
  11. public float lastSwapTime;
  12. private Craft craft;
  13. public SwapManager(Craft craft)
  14. {
  15. this.craft = craft;
  16. lastSwapTime = GameTime.time;
  17. }
  18. public void PrepareSwap(int targetSwapId)
  19. {
  20. this.targetSwapId = targetSwapId;
  21. isPreparingSwap = true;
  22. preparingSwapStartTime = GameTime.time;
  23. craft.StopMove();
  24. }
  25. public float GetPreparingSwapTime()
  26. {
  27. if(preparingSwapStartTime < 0)
  28. return 0;
  29. return Mathf.Min(GameTime.time - preparingSwapStartTime, PREPARING_SWAP_DURATION);
  30. }
  31. public bool IsReadySwap()
  32. {
  33. return GetPreparingSwapTime() == PREPARING_SWAP_DURATION;
  34. }
  35. public void AbortSwap()
  36. {
  37. if(requestedSwap)
  38. return;
  39. preparingSwapStartTime = -1f;
  40. isPreparingSwap = false;
  41. craft.GetHeadBar().HideSpell();
  42. }
  43. public int GetTargetSwapId()
  44. {
  45. return targetSwapId;
  46. }
  47. public void RequestedSwap()
  48. {
  49. preparingSwapStartTime = -1f;
  50. requestedSwap = true;
  51. }
  52. public void Swap(Craft newCraft, int craftId, CraftEquipModify equipModify)
  53. {
  54. if(craft.IsDead())
  55. return;
  56. lastSwapTime = GameTime.time;
  57. craft.GetHeadBar().HideSpell();
  58. isPreparingSwap = false;
  59. requestedSwap = false;
  60. craft.SetEquipModify (equipModify);
  61. craft.SetCraftId (craftId);
  62. craft.bodyTrans.SetParent(null);
  63. GameObject.Destroy(craft.bodyTrans.gameObject);
  64. Transform newBody = newCraft.transform.Find("Body");
  65. newBody.SetParent(craft.transform);
  66. newBody.localPosition = Vector3.zero;
  67. newBody.localScale = new Vector3(1f, 1f, 1f);
  68. craft.textureArr = newCraft.textureArr;
  69. GameObject.Destroy(newCraft.gameObject);
  70. craft.Init();
  71. craft.team = craft.team;
  72. craft.SwapComplete();
  73. }
  74. public float GetSwapCoolDown()
  75. {
  76. return Mathf.Min(GameTime.time - lastSwapTime, SWAP_CD);
  77. }
  78. }