private bool _wasOnTarget = false; //was the mouse on target last frame?

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Create a new MouseHandler for the given target.
        /// </summary>
        /// <param name="target">Target.</param>
        public MouseHandler(GameObject target)
        {
            _target = target;
            _game = _target.game;
            _game.OnAfterStep += HandleOnStep;
            _lastX = Input.mouseX;
            _lastY = Input.mouseY;
        }
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        Remove()
 //------------------------------------------------------------------------------------------------------------------------
 public void Remove(GameObject gameObject)
 {
     colliderList.Remove(gameObject);
     if (_collisionReferences.ContainsKey(gameObject)) {
         ColliderInfo colliderInfo = _collisionReferences[gameObject];
         activeColliderList.Remove(colliderInfo);
         _collisionReferences.Remove(gameObject);
     }
 }
 //------------------------------------------------------------------------------------------------------------------------
 //                                                 GetCurrentCollisions()
 //------------------------------------------------------------------------------------------------------------------------
 public GameObject[] GetCurrentCollisions(GameObject gameObject)
 {
     List<GameObject> list = new List<GameObject>();
     for (int j=colliderList.Count-1; j>=0; j--) {
         if (j >= colliderList.Count) continue; //fix for removal in loop
         GameObject other = colliderList[j];
         if (gameObject != other) {
             if (gameObject.HitTest(other)) {
                 list.Add(other);
             }
         }
     }
     return list.ToArray();
 }
        //------------------------------------------------------------------------------------------------------------------------
        //                                                        Add()
        //------------------------------------------------------------------------------------------------------------------------
        public void Add(GameObject gameObject)
        {
            if (gameObject.collider != null && !colliderList.Contains (gameObject)) {
                colliderList.Add(gameObject);
            }

            MethodInfo info = gameObject.GetType().GetMethod("OnCollision", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            if (info != null) {

                CollisionDelegate onCollision = (CollisionDelegate)Delegate.CreateDelegate(typeof(CollisionDelegate), gameObject, info, false);
                if (onCollision != null && !_collisionReferences.ContainsKey (gameObject)) {
                    ColliderInfo colliderInfo = new ColliderInfo(gameObject, onCollision);
                    _collisionReferences[gameObject] = colliderInfo;
                    activeColliderList.Add(colliderInfo);
                }

            } else {
                validateCase(gameObject);
            }
        }
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        ColliderInfo()
 //------------------------------------------------------------------------------------------------------------------------
 public ColliderInfo(GameObject gameObject, CollisionDelegate onCollision)
 {
     this.gameObject = gameObject;
     this.onCollision = onCollision;
 }
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        validateCase()
 //------------------------------------------------------------------------------------------------------------------------
 private void validateCase(GameObject gameObject)
 {
     MethodInfo info = gameObject.GetType().GetMethod("OnCollision", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
     if (info != null) {
         throw new Exception("'OnCollision' function was not binded. Please check it's correct case (capital O?)");
     }
 }
Example #7
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        Remove()
 //------------------------------------------------------------------------------------------------------------------------
 internal void Remove(GameObject gameObject)
 {
     if (_gameObjectsContained.Contains (gameObject)) {
         _updateManager.Remove (gameObject);
         _collisionManager.Remove (gameObject);
         _gameObjectsContained.Remove (gameObject);
     }
 }
Example #8
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        GetGameObjectCollisions()
 //------------------------------------------------------------------------------------------------------------------------
 internal GameObject[] GetGameObjectCollisions(GameObject gameObject)
 {
     return _collisionManager.GetCurrentCollisions(gameObject);
 }
Example #9
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        Add()
 //------------------------------------------------------------------------------------------------------------------------
 internal void Add(GameObject gameObject)
 {
     if (!_gameObjectsContained.Contains (gameObject)) {
         _updateManager.Add (gameObject);
         _collisionManager.Add (gameObject);
         _gameObjectsContained.Add (gameObject);
     }
 }
Example #10
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        HasChild()
 //------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Returns 'true' if the specified object is a child of this object.
 /// </summary>
 /// <param name='gameObject'>
 /// The GameObject that should be tested.
 /// </param>
 public bool HasChild(GameObject gameObject)
 {
     GameObject par = gameObject;
     while (par != null) {
         if (par == this) return true;
         par = par.parent;
     }
     return false;
 }
Example #11
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        removeChild()
 //------------------------------------------------------------------------------------------------------------------------
 private void removeChild(GameObject child)
 {
     _children.Remove(child);
 }
Example #12
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        addChild()
 //------------------------------------------------------------------------------------------------------------------------
 private void addChild(GameObject child)
 {
     if (child.HasChild(this)) return; //no recursive adding
     _children.Add(child);
     return;
 }
Example #13
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        SetChildIndex()
 //------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Inserts the specified object in this object's child list at given location.
 /// This will alter the position of other objects as well.
 /// You can use this to determine the layer order (z-order) of child objects.
 /// </summary>
 /// <param name='child'>
 /// Child.
 /// </param>
 /// <param name='index'>
 /// Index.
 /// </param>
 public void SetChildIndex(GameObject child, int index)
 {
     if (child.parent != this) AddChild(child);
     if (index < 0) index = 0;
     if (index >= _children.Count) index = _children.Count - 1;
     _children.Remove(child);
     _children.Insert(index, child);
 }
Example #14
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        RemoveChild()
 //------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Removes the specified child GameObject from this object.
 /// </summary>
 /// <param name='child'>
 /// Child object to remove.
 /// </param>
 public void RemoveChild(GameObject child)
 {
     if (child.parent == this) {
         child.parent = null;
     }
 }
Example #15
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        HitTest()
 //------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Tests if this object overlaps the one specified. 
 /// </summary>
 /// <returns>
 /// <c>true</c>, if test was hit, <c>false</c> otherwise.
 /// </returns>
 /// <param name='other'>
 /// Other.
 /// </param>
 public virtual bool HitTest(GameObject other)
 {
     return _collider != null && other._collider != null && _collider.HitTest (other._collider);
 }
Example #16
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        Contains()
 //------------------------------------------------------------------------------------------------------------------------
 public Boolean Contains(GameObject gameObject)
 {
     return _gameObjectsContained.Contains(gameObject);
 }
Example #17
0
 //------------------------------------------------------------------------------------------------------------------------
 //                                                        AddChild()
 //------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Adds the specified GameObject as a child to this one.
 /// </summary>
 /// <param name='child'>
 /// Child object to add.
 /// </param>
 public void AddChild(GameObject child)
 {
     child.parent = this;
 }