public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
 {
     IntegerVector center = this.Bounds.Center;
     center.X += offsetX;
     center.Y += offsetY;
     return this.Contains(other.ClosestContainedPoint(this.Bounds.Center), offsetX, offsetY);
 }
示例#2
0
 public virtual bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
 {
     IntegerRect bounds = this.Bounds;
     bounds.Center.X += offsetX;
     bounds.Center.Y += offsetY;
     return bounds.Overlaps(other.Bounds);
 }
    public void RemoveCollider(LayerMask layer, IntegerCollider collider)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] != null)
            {
                _solids[x, y].Remove(collider);
            }

            /*else
             * {
             *  Debug.LogWarning("Collider to remove not found! " + collider.transform + ", " + collider.transform.root);
             * }*/
        }
        else
        {
            if (_collidersByLayer.ContainsKey(layer))
            {
                _collidersByLayer[layer].Remove(collider);
            }
        }
    }
示例#4
0
    public GameObject CollideFirst(int offsetX = 0, int offsetY = 0, int mask = Physics2D.DefaultRaycastLayers, string objectTag = null, List <IntegerCollider> potentialCollisions = null)
    {
        if (potentialCollisions == null)
        {
            potentialCollisions = this.GetPotentialCollisions(0, 0, offsetX, offsetY, mask);
        }

        if (potentialCollisions.Count == 0 || (potentialCollisions.Count == 1 && potentialCollisions[0] == this))
        {
            return(null);
        }

        for (int i = 0; i < potentialCollisions.Count; ++i)
        {
            IntegerCollider collider = potentialCollisions[i];
            if (collider != null && collider != this && (objectTag == null || collider.tag == objectTag) && collider.enabled)
            {
                if (this.Overlaps(collider, offsetX, offsetY))
                {
                    return(collider.gameObject);
                }
            }
        }

        return(null);
    }
示例#5
0
 public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
 {
     if (other.Id != ID)
     {
         return(other.Overlaps(this, -offsetX, -offsetY));
     }
     return(base.Overlaps(other, offsetX, offsetY));
 }
示例#6
0
 //TODO - use dynamic keyword to make use of run-time overloading? and have an extensions method place for inter-collider type collisions?
 //http://stackoverflow.com/questions/13095544/overloaded-method-why-is-base-class-given-precedence#comment25529590_13096565
 public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
 {
     if (other.GetType() == typeof(IntegerCircleCollider))
     {
         return(other.Overlaps(this, -offsetX, -offsetY));
     }
     return(base.Overlaps(other, offsetX, offsetY));
 }
示例#7
0
    //TODO - use dynamic keyword to make use of run-time overloading? and have an extensions method place for inter-collider type collisions?
    //http://stackoverflow.com/questions/13095544/overloaded-method-why-is-base-class-given-precedence#comment25529590_13096565
    public virtual bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
    {
        IntegerRect bounds = this.Bounds;

        bounds.Center.X += offsetX;
        bounds.Center.Y += offsetY;
        return(bounds.Overlaps(other.Bounds));
    }
    public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
    {
        IntegerVector center = this.Bounds.Center;

        center.X += offsetX;
        center.Y += offsetY;
        return(this.Contains(other.ClosestContainedPoint(this.Bounds.Center), offsetX, offsetY));
    }
    public List <IntegerCollider> GetCollidersInRange(IntegerRect range, int mask = Physics2D.DefaultRaycastLayers, string objectTag = null, List <IntegerCollider> _listRef = null)
    {
        List <IntegerCollider> colliders;

        if (_listRef != null)
        {
            _listRef.Clear();
            colliders = _listRef;
        }
        else
        {
            colliders = new List <IntegerCollider>();
        }

        foreach (LayerMask key in _collidersByLayer.Keys)
        {
            if ((key & mask) != 0)
            {
                List <IntegerCollider> collidersInLayer = _collidersByLayer[key];
                for (int i = 0; i < collidersInLayer.Count; ++i)
                {
                    IntegerCollider collider = collidersInLayer[i];
                    if (collider == null)
                    {
                        Debug.LogWarning("Null collider!! key = " + LayerMask.LayerToName(Mathf.RoundToInt(Mathf.Sqrt(key))));
                    }
                    else
                    {
                        if ((objectTag == null || collider.tag == objectTag) && collider.enabled &&
                            collider.Bounds.Overlaps(range))
                        {
                            colliders.Add(collider);
                        }
                    }
                }
            }
        }

        if ((mask & this.SolidsLayerMask) != 0)
        {
            for (int x = xPositionToSolidsIndex(range.Min.X - this.MaxSolidSize); x <= xPositionToSolidsIndex(range.Max.X + this.MaxSolidSize); ++x)
            {
                for (int y = yPositionToSolidsIndex(range.Min.Y - this.MaxSolidSize); y <= yPositionToSolidsIndex(range.Max.Y + this.MaxSolidSize); ++y)
                {
                    if (_solids[x, y] != null)
                    {
                        colliders.AddRange(_solids[x, y]);
                    }
                }
            }
        }

        return(colliders);
    }
 public GameObject CollidePointFirst(IntegerVector point, List <IntegerCollider> potentialCollisions)
 {
     for (int i = 0; i < potentialCollisions.Count; ++i)
     {
         IntegerCollider collider = potentialCollisions[i];
         if (collider != null && collider.enabled && collider.Contains(point))
         {
             return(collider.gameObject);
         }
     }
     return(null);
 }
    public GameObject CollidePointFirst(IntegerVector point, int mask = Physics2D.DefaultRaycastLayers, string objectTag = null)
    {
        if ((mask & this.SolidsLayerMask) != 0)
        {
            int midX = xPositionToSolidsIndex(point.X);
            int midY = yPositionToSolidsIndex(point.Y);
            int minX = midX > 1 ? midX - 1 : 0;
            int maxX = midX < MAX_SOLIDS_X - 1 ? midX + 1 : MAX_SOLIDS_X - 1;
            int minY = midY > 1 ? midY - 1 : 0;
            int maxY = midY < MAX_SOLIDS_Y - 1 ? midY + 1 : MAX_SOLIDS_Y - 1;

            for (int x = minX; x <= maxX; ++x)
            {
                for (int y = minY; y <= maxY; ++y)
                {
                    if (_solids[x, y] != null)
                    {
                        List <IntegerCollider> colliders = _solids[x, y];
                        for (int i = 0; i < colliders.Count; ++i)
                        {
                            IntegerCollider collider = colliders[i];
                            if (collider.Contains(point) && collider.enabled)
                            {
                                return(collider.gameObject);
                            }
                        }
                    }
                }
            }
        }

        foreach (LayerMask key in _collidersByLayer.Keys)
        {
            if ((key & mask) != 0)
            {
                List <IntegerCollider> colliders = _collidersByLayer[key];
                for (int i = 0; i < colliders.Count; ++i)
                {
                    IntegerCollider collider = colliders[i];
                    if ((objectTag == null || collider.tag == objectTag) && collider.enabled &&
                        collider.Contains(point))
                    {
                        return(collider.gameObject);
                    }
                }
            }
        }

        return(null);
    }
    //NOTE: Only partial support of circle colliders here
    public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
    {
        IntegerRect   otherBounds = other.Bounds;
        int           otherY      = other.Bounds.Min.Y;
        IntegerVector ourPos      = this.integerPosition + this.Offset;

        // Check if the colliders current position is above us, and their new (offset) position is on or below us
        if (otherY > ourPos.Y && otherY - offsetY <= ourPos.Y)
        {
            IntegerRect ourBounds = this.Bounds;

            // Make sure they're within our width
            return(otherBounds.Min.X - offsetX <= ourBounds.Max.X && otherBounds.Max.X - offsetX >= ourBounds.Min.X);
        }
        return(false);
    }
示例#13
0
    public void RemoveCollider(LayerMask layer, IntegerCollider collider, bool solid = false)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] != null)
                _solids[x, y].Remove(collider);
        }
        else
        {
            if (_collidersByLayer.ContainsKey(layer))
                _collidersByLayer[layer].Remove(collider);
        }
    }
示例#14
0
    public bool Push(DirectionalVector2 dir, IntegerCollider fromBounds, int fromOffsetX, int fromOffsetY)
    {
        IntegerVector d = IntegerVector.Zero;

        while (fromBounds.CollideCheck(this.gameObject, fromOffsetX - d.X, fromOffsetY - d.Y))
        {
            d.X += dir.X;
            d.Y += dir.Y;
        }

        IntegerVector target = this.integerPosition + d;

        this.Move(d);

        return(this.integerPosition == target);
    }
    public void AddCollider(LayerMask layer, IntegerCollider collider)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] == null)
                _solids[x, y] = new List<IntegerCollider>();
            _solids[x, y].Add(collider);
        }
        else
        {
            if (!_collidersByLayer.ContainsKey(layer))
                _collidersByLayer.Add(layer, new List<IntegerCollider>());
            _collidersByLayer[layer].AddUnique(collider);
        }
    }
示例#16
0
    public void RemoveCollider(LayerMask layer, IntegerCollider collider, bool solid = false)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] != null)
            {
                _solids[x, y].Remove(collider);
            }
        }
        else
        {
            if (_collidersByLayer.ContainsKey(layer))
            {
                _collidersByLayer[layer].Remove(collider);
            }
        }
    }
    public void RemoveCollider(LayerMask layer, IntegerCollider collider)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] != null)
            {
                _solids[x, y].Remove(collider);
            }
            /*else
            {
                Debug.LogWarning("Collider to remove not found! " + collider.transform + ", " + collider.transform.root);
            }*/
        }
        else
        {
            if (_collidersByLayer.ContainsKey(layer))
                _collidersByLayer[layer].Remove(collider);
        }
    }
示例#18
0
    public void AddCollider(LayerMask layer, IntegerCollider collider)
    {
        if ((layer & this.SolidsLayerMask) != 0)
        {
            int x = xPositionToSolidsIndex(collider.Bounds.Center.X);
            int y = yPositionToSolidsIndex(collider.Bounds.Center.Y);

            if (_solids[x, y] == null)
            {
                _solids[x, y] = new List <IntegerCollider>();
            }
            _solids[x, y].Add(collider);
        }
        else
        {
            if (!_collidersByLayer.ContainsKey(layer))
            {
                _collidersByLayer.Add(layer, new List <IntegerCollider>());
            }
            _collidersByLayer[layer].AddUnique(collider);
        }
    }
示例#19
0
    public bool CollideCheck(GameObject checkObject, int offsetX = 0, int offsetY = 0)
    {
        IntegerCollider other = checkObject.GetComponent <IntegerCollider>();

        return(other && this.Overlaps(other, offsetX, offsetY));
    }
示例#20
0
 //TODO - use dynamic keyword to make use of run-time overloading? and have an extensions method place for inter-collider type collisions?
 //http://stackoverflow.com/questions/13095544/overloaded-method-why-is-base-class-given-precedence#comment25529590_13096565
 public override bool Overlaps(IntegerCollider other, int offsetX = 0, int offsetY = 0)
 {
     if (other.GetType() == typeof(IntegerCircleCollider))
         return other.Overlaps(this, -offsetX, -offsetY);
     return base.Overlaps(other, offsetX, offsetY);
 }
示例#21
0
    private bool spawnAvailable(Transform spawn)
    {
        IntegerCollider collider = spawn.GetComponent <IntegerCollider>();

        return(collider == null || collider.CollideFirst(0, 0, this.SpawnCollisionLayers) == null);
    }
示例#22
0
    public RaycastResult RaycastUntil(List <RaycastCollision> passThroughCollisions, IntegerVector origin, Vector2 direction, int passThroughMask, int haltMask, float range = 100000.0f)
    {
        passThroughMask &= ~haltMask;
        Vector2 d      = direction * range;
        Vector2 chunkD = range <= this.RaycastChunkSize ? d : direction * this.RaycastChunkSize;

        IntegerVector          halfwayPoint       = new IntegerVector(chunkD / 2.0f) + origin;
        IntegerVector          rangeVector        = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
        List <IntegerCollider> possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);

        Vector2       positionModifier = Vector2.zero;
        IntegerVector position         = origin;

        float incX = d.x;
        float incY = d.y;

        if (Mathf.Abs(incX) > RAYCAST_MAX_POSITION_INCREMENT || Mathf.Abs(incY) > RAYCAST_MAX_POSITION_INCREMENT)
        {
            Vector2 dNormalized = d.normalized * RAYCAST_MAX_POSITION_INCREMENT;
            incX = dNormalized.x;
            incY = dNormalized.y;
        }

        Vector2                projected  = Vector2.zero;
        Vector2                soFar      = Vector2.zero;
        float                  dMagnitude = d.magnitude;
        RaycastResult          result     = new RaycastResult();
        List <IntegerCollider> collided   = new List <IntegerCollider>();
        bool endReached  = false;
        int  chunksSoFar = 1;

        while (true)
        {
            if (soFar.magnitude >= this.RaycastChunkSize * chunksSoFar)
            {
                // Recalculate chunk
                halfwayPoint       = new IntegerVector(chunkD / 2.0f) + position;
                rangeVector        = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
                possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);
                foreach (IntegerCollider collider in collided)
                {
                    possibleCollisions.Remove(collider);
                }
                ++chunksSoFar;
            }

            projected.x += incX;
            projected.y += incY;

            if (projected.magnitude > dMagnitude)
            {
                incX       = d.x - soFar.x;
                incY       = d.y - soFar.y;
                endReached = true;
            }

            positionModifier.x += incX;
            int move = (int)positionModifier.x;

            positionModifier.x -= move;
            int unitDir = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X + unitDir, position.Y);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent <IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedX      = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions    = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided)
            {
                break;
            }

            positionModifier.y += incY;
            move = (int)positionModifier.y;

            positionModifier.y -= move;
            unitDir             = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X, position.Y + unitDir);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent <IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedY      = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions    = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided || endReached)
            {
                break;
            }

            soFar.x = projected.x;
            soFar.y = projected.y;
        }

        result.FarthestPointReached = position;
        return(result);
    }