예제 #1
0
 /// <summary>
 /// Checks if a point is within a given distance of a rectangle's edge.
 /// </summary>
 /// <param name="distance"></param>
 /// <param name="gent"></param>
 /// <returns></returns>
 public bool Within(double distance, Rectangle rect)
 {
     Ellipse distCheck = new Ellipse(rect.x - (distance / 2), rect.y - (distance / 2),
         rect.width + distance, rect.height + distance);
     //Circle distCheck = new Circle(rect.x, rect.y, (rect.Size.GetGreatest() + distance) / 2);
     return distCheck.Contains(this);
 }
예제 #2
0
        public bool Contains(Rectangle rect)
        {
            if (rect.Right < this.x ||
                rect.x > this.Right ||
                rect.y > this.Top ||
                rect.Top < this.y)
            {
                return false;
            }

            return true;
        }
예제 #3
0
 public bool Above(Rectangle rect)
 {
     return this.Bottom > rect.Top;
 }
예제 #4
0
 public bool Below(Rectangle rect)
 {
     return this.Top < rect.Bottom;
 }
예제 #5
0
        private void checkCollisions()
        {
            // We want to find anything within the entitiy's "reach", which is it's size plus the speed it's moving.
            // For simplicity's sake, we'll use the greatest dimension for size and speed.
            // This won't cause issues if entities aren't particularly fast on one axis, or oblong.
            double distance = this.GetSize().GetGreatest() + Math.Abs(this.speed.GetGreatest());
            //retrieve of all entities in range
            List<GameEntity> entList = Stage.CurrentStage.GetEntitiesNear(this.GetPosition(), distance, true);
            foreach (GameEntity ent in entList)
            {
                //skip checking for collision against itself, and against entities that aren't physics objects
                if (ent == this || ent.ObeysPhysics == false || ent.GetId() == this.GetId())
                {
                    continue;
                }

                if (ent.GetTypeName() == "Tile" && ent.As<Tile>().GetWalkable() == true)
                {
                    continue;
                }

                //don't collide with units of the same faction (for now...we need a better way to handle this)
                if (this.Faction != null && this.Faction == ent.Faction)
                {
                    continue;
                }

                //don't collide with bullets, they collide with me
                //if (this is Projectile == false && ent is Projectile == true)
                //if (ent.instanceof<Projectile>() == true)
                //I really wanted to avoid using strings here, but I have to get around the typing problem caused in the cloning...for a bit...
                if (ent.GetTypeName() == "Projectile")
                {
                    if (ent.instanceof<Projectile>()) Debug.log("Instanceof works now. Magically. You don't have to use strings here.");

                    continue;
                }

                // If the entities don't overlap, they don't collide.
                // Check for actual overlap at this point (as it's more expensive than checking for distance)
                Rectangle thisRect = new Rectangle(this.position.x, this.position.y, this.size.width, this.size.height);
                if (this.speed.X > 0)
                {
                    thisRect.width += this.speed.X;
                }
                else
                {
                    thisRect.x += this.speed.X;
                }
                if (this.speed.Y > 0)
                {
                    thisRect.height += this.speed.Y;
                }
                else
                {
                    thisRect.y += this.speed.Y;
                }
                Rectangle entRect = new Rectangle(ent.position.x, ent.position.y, ent.size.width, ent.size.height);
                if (ent.speed.X > 0)
                {
                    entRect.width += ent.speed.X;
                }
                else
                {
                    entRect.x += ent.speed.X;
                }
                if (ent.speed.Y > 0)
                {
                    entRect.height += ent.speed.Y;
                }
                else
                {
                    entRect.y += ent.speed.Y;
                }

                if (thisRect.Contains(entRect))
                {
                    if (ent.GetTypeName() == "TerrainCollider")
                    {
                        ent.As<TerrainCollider>().CheckCollision(this);
                    }
                    else
                    {
                        this.Collision(ent);
                    }
                }

                //Debug.Watch("Nearent is ", "Origin: " + ent.GetPosition().x + ", " + ent.GetPosition().y + ". Destination: " + this.GetPosition().x + ", " + this.GetPosition().y + ". Distance: " + this.GetSize().GetGreatest());

                //Debug.log(this.GetId() + " is colliding with " + ent.GetId());
            }
        }