123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- [RequireComponent(typeof(PoolObject))]
- public abstract class Pickup : CompareXMonoBehaviour, ILevelEditorGameEntity
- {
- public PickupType type;
- PoolObject _poolObject;
- bool _hasMouseBeenPressed;
- public const float PICKUP_ANIMATION_TIME = 0.5f;
- #region Unity Callbacks
- void Awake()
- {
- _poolObject = GetComponent<PoolObject>();
-
- #if LEVEL_EDITOR
- if (!Pool.isSpawning)
- {
- NotificationCenter.Post(NotificationType.AddPickupToLevelEditor, this);
- }
- #endif
- }
- void CustomFixedUpdate()
- {
- if (_hasMouseBeenPressed)
- {
- _hasMouseBeenPressed = false;
- SoundManager.Play(SoundEvent.pickup);
- PickupAction();
- AnimateTowardsTarget();
- }
- transform.gameObject.layer = LayerMask.NameToLayer("UI");
- /// Note that the pickup's sprite's pivot is on the left. So when position of the enemy goes offscreen,
- /// it means it is not visible anymore
- if (transform.localPosition.x > GameConstants.GAME_WIDTH)
- {
- _poolObject.Despawn();
- }
- }
- #endregion
- #region NGUI Callbacks
- void OnPress(bool isPressed)
- {
- //Debug.LogError("OnPress");
- if (isPressed)
- {
- //process on FixedUpdate
- _hasMouseBeenPressed = true;
- }
- }
- public void OnClick()
- {
- _hasMouseBeenPressed = true;
- }
- void OnMouseDown()
- {
- //Debug.LogError("OnPress");
- _hasMouseBeenPressed = true;
- }
- #endregion
- #region ILevelEditorGameEntity
- ushort ILevelEditorGameEntity.GetTypeForEncoding()
- {
- return (ushort)type;
- }
- LevelEditorGameEntityType ILevelEditorGameEntity.GetGameEntityType()
- {
- return LevelEditorGameEntityType.Pickup;
- }
- #endregion
- #if LEVEL_EDITOR
- //Since the game designer can drag during level editing, we need to ensure this
- void Update()
- {
- Vector3 pos = transform.localPosition;
- pos.z = 0;
- transform.localPosition = pos;
- }
- #endif
- void AnimateTowardsTarget()
- {
- iTween.MoveTo(gameObject, iTween.Hash(
- "usefixedupdate", true,
- "position", GetTarget().position,
- "time", PICKUP_ANIMATION_TIME,
- "easetype", iTween.EaseType.easeOutCubic,
- "oncomplete", "OniTweenFinishedAnimatingTowardsTarget",
- "oncompletetarget", gameObject));
- }
- protected abstract void PickupAction();
- protected virtual Transform GetTarget()
- {
- return CondomDispenser.GetTransform();
- }
- protected virtual void OniTweenFinishedAnimatingTowardsTarget()
- {
- _poolObject.Despawn();
- }
- }
|