Capsule.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Capsule : MonoBehaviour {
  4. private float startTime;
  5. private float startY = 30f;
  6. private bool isOpen = false;
  7. private CraftData craftData;
  8. private Craft swapCraft;
  9. private int swapId;
  10. public Animation animation;
  11. // Use this for initialization
  12. void Start ()
  13. {
  14. Vector3 position = transform.localPosition;
  15. position.y = startY;
  16. transform.localPosition = position;
  17. startTime = GameTime.time;
  18. }
  19. public void SetCraftData(CraftData data)
  20. {
  21. this.craftData = data;
  22. transform.position = data.position;
  23. }
  24. public void SetSwap(Craft swapCraft, int swapId)
  25. {
  26. this.swapCraft = swapCraft;
  27. this.swapId = swapId;
  28. }
  29. // Update is called once per frame
  30. void Update ()
  31. {
  32. float currentTime = GameTime.time;
  33. float deltaTime = currentTime - startTime;
  34. if(deltaTime < 1f)
  35. {
  36. Vector3 position = transform.localPosition;
  37. position.y = startY*(1f-deltaTime);
  38. transform.localPosition = position;
  39. }
  40. else if(!isOpen)
  41. {
  42. isOpen = true;
  43. animation.Play("Open");
  44. if(craftData != null)
  45. {
  46. BattleSession battleSession = Session.GetInstance().GetBattleSession();
  47. try
  48. {
  49. Craft craft = battleSession.GetBattleController().CreateCraft(craftData);
  50. }
  51. catch(System.Exception e)
  52. {
  53. Debuger.LogException (e);
  54. }
  55. }
  56. else
  57. {
  58. CraftFactory.SwapModel(swapCraft, swapId);
  59. if(swapCraft.IsCtrl())
  60. Session.GetInstance().GetBattleSession().GetBattleController().battleUI.powerIconContainer.SetCraft(swapCraft);
  61. transform.SetParent(null);
  62. }
  63. }
  64. else if(isOpen)
  65. {
  66. if(!animation.isPlaying)
  67. {
  68. Destroy(this.gameObject);
  69. }
  70. }
  71. }
  72. }