Pickup.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. [RequireComponent(typeof(PoolObject))]
  6. public abstract class Pickup : CompareXMonoBehaviour, ILevelEditorGameEntity
  7. {
  8. public PickupType type;
  9. PoolObject _poolObject;
  10. bool _hasMouseBeenPressed;
  11. public const float PICKUP_ANIMATION_TIME = 0.5f;
  12. #region Unity Callbacks
  13. void Awake()
  14. {
  15. _poolObject = GetComponent<PoolObject>();
  16. #if LEVEL_EDITOR
  17. if (!Pool.isSpawning)
  18. {
  19. NotificationCenter.Post(NotificationType.AddPickupToLevelEditor, this);
  20. }
  21. #endif
  22. }
  23. void CustomFixedUpdate()
  24. {
  25. if (_hasMouseBeenPressed)
  26. {
  27. _hasMouseBeenPressed = false;
  28. SoundManager.Play(SoundEvent.pickup);
  29. PickupAction();
  30. AnimateTowardsTarget();
  31. }
  32. transform.gameObject.layer = LayerMask.NameToLayer("UI");
  33. /// Note that the pickup's sprite's pivot is on the left. So when position of the enemy goes offscreen,
  34. /// it means it is not visible anymore
  35. if (transform.localPosition.x > GameConstants.GAME_WIDTH)
  36. {
  37. _poolObject.Despawn();
  38. }
  39. }
  40. #endregion
  41. #region NGUI Callbacks
  42. void OnPress(bool isPressed)
  43. {
  44. //Debug.LogError("OnPress");
  45. if (isPressed)
  46. {
  47. //process on FixedUpdate
  48. _hasMouseBeenPressed = true;
  49. }
  50. }
  51. public void OnClick()
  52. {
  53. _hasMouseBeenPressed = true;
  54. }
  55. void OnMouseDown()
  56. {
  57. //Debug.LogError("OnPress");
  58. _hasMouseBeenPressed = true;
  59. }
  60. #endregion
  61. #region ILevelEditorGameEntity
  62. ushort ILevelEditorGameEntity.GetTypeForEncoding()
  63. {
  64. return (ushort)type;
  65. }
  66. LevelEditorGameEntityType ILevelEditorGameEntity.GetGameEntityType()
  67. {
  68. return LevelEditorGameEntityType.Pickup;
  69. }
  70. #endregion
  71. #if LEVEL_EDITOR
  72. //Since the game designer can drag during level editing, we need to ensure this
  73. void Update()
  74. {
  75. Vector3 pos = transform.localPosition;
  76. pos.z = 0;
  77. transform.localPosition = pos;
  78. }
  79. #endif
  80. void AnimateTowardsTarget()
  81. {
  82. iTween.MoveTo(gameObject, iTween.Hash(
  83. "usefixedupdate", true,
  84. "position", GetTarget().position,
  85. "time", PICKUP_ANIMATION_TIME,
  86. "easetype", iTween.EaseType.easeOutCubic,
  87. "oncomplete", "OniTweenFinishedAnimatingTowardsTarget",
  88. "oncompletetarget", gameObject));
  89. }
  90. protected abstract void PickupAction();
  91. protected virtual Transform GetTarget()
  92. {
  93. return CondomDispenser.GetTransform();
  94. }
  95. protected virtual void OniTweenFinishedAnimatingTowardsTarget()
  96. {
  97. _poolObject.Despawn();
  98. }
  99. }