コード例 #1
0
        public Structure(Grid buildGrid, StructureData data)
        {
            this._grid = buildGrid;
            RectangleBody rect;

            if (data.X1 < 0 || data.Y1 < 0)
            {
                rect            = new RectangleBody(buildGrid.TileToPixelRect(0, 0));
                rect.Position.X = -10000;
                rect.Position.Y = -10000;
            }
            else
            {
                rect = new RectangleBody(buildGrid.TileToPixelRect(data.X1, data.Y1));
            }
            this.Collision = rect;
            rect.Size      = new Vector2(rect.Region().Width *data.Width, rect.Region().Height *data.Height);
            this.Data      = data;

            Collision        = rect;
            Collision.Parent = this;

            Velocity     = new Vector2(0f);
            Acceleration = new Vector2(0f);
        }
コード例 #2
0
        public bool CollidesWith(CollisionBody other)
        {
            if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Circle)
            {
                CircleBody a = (CircleBody)this;
                CircleBody b = (CircleBody)other;
                return(Vector2.Distance(this.Position, other.Position) <= a.Radius + b.Radius);
            }
            else if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Rectangle)
            {
                return(RectCircCollision((CircleBody)this, (RectangleBody)other));
            }
            else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Circle)
            {
                return(RectCircCollision((CircleBody)other, (RectangleBody)this));
            }
            else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Rectangle)
            {
                RectangleBody a = (RectangleBody)this;
                RectangleBody b = (RectangleBody)other;

                return((a.Position.X <= b.Position.X + b.Size.X && a.Position.X + a.Size.X >= b.Position.X) &&
                       (a.Position.Y <= b.Position.Y + b.Size.Y && a.Position.Y + a.Size.Y >= b.Position.Y));
            }
            else
            {
                throw new NotImplementedException("Attempted collision with a shape that doesn't exist");
            }
        }
コード例 #3
0
        private bool RectCircCollision(CircleBody a, RectangleBody b)
        {
            float   nearestX     = Clamp(a.Position.X, b.Position.X, b.Position.X + b.Size.X);
            float   nearestY     = Clamp(a.Position.Y, b.Position.Y, b.Position.Y + b.Size.Y);
            Vector2 nearestPoint = new Vector2(nearestX, nearestY);

            return(Vector2.Distance(nearestPoint, a.Position) <= a.Radius);
        }
コード例 #4
0
ファイル: Town.cs プロジェクト: BrandonKPugh/CityBuilder
 public Town(Grid grid)
 {
     //_structures = new List<Structure>();
     _structures = new Dictionary <int, Structure>();
     _resources  = new Dictionary <Resource.ResourceType, float>();
     this._grid  = grid;
     _townZone   = new RectangleBody(grid.Info.GridRectangle);
 }
コード例 #5
0
 public Structure(CollisionBody collision, StructureData data) : base(collision)
 {
     this.Data = data;
     if (collision.Shape == CollisionBody.ShapeType.Rectangle)
     {
         RectangleBody rect = ((RectangleBody)this.Collision);
         rect.Size = new Vector2(collision.Region().Width *data.Width, collision.Region().Height *data.Height);
     }
 }
コード例 #6
0
        public Dictionary <uint, CollisionBody> GetPotentialCollisions(CollisionBody toCheck)
        {
            int xMin = 0;
            int xMax = 0;
            int yMin = 0;
            int yMax = 0;

            if (toCheck.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody body = (CircleBody)toCheck;
                xMin = ((int)(Math.Max(body.Position.X - body.Radius / 2, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Radius / 2, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y - body.Radius / 2, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Radius / 2, maxHeight - 1)) / tileSize;
            }
            else if (toCheck.Shape == CollisionBody.ShapeType.Rectangle)
            {
                RectangleBody body = (RectangleBody)toCheck;
                xMin = ((int)(Math.Max(body.Position.X, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Size.X, 49)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Size.Y, 49)) / tileSize;
            }

            // Get every possible CollisionBody
            Dictionary <uint, CollisionBody> potentials = new Dictionary <uint, CollisionBody>();

            for (int y = yMin; y <= yMax; y++)
            {
                for (int x = xMin; x <= xMax; x++)
                {
                    foreach (CollisionBody c in Tiles[y * columns + x].Data.Values)
                    {
                        if (!potentials.ContainsKey(c.ID))
                        {
                            potentials.Add(c.ID, c);
                        }
                    }
                }
            }

            return(potentials);
        }
コード例 #7
0
        private void BounceOffTop(Object other)
        {
            if (this.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;

                float dist = (c.Position.Y + c.Radius) - (r.Position.Y);
                this.Collision.Position.Y -= dist * 2;
                this.Velocity.Y           *= -1;
            }
            else
            {
                RectangleBody r1 = (RectangleBody)this.Collision;
                RectangleBody r2 = (RectangleBody)other.Collision;

                float dist = (r1.Position.Y + r1.Size.Y) - (r2.Position.Y);
                this.Collision.Position.Y -= dist * 2;
                this.Velocity.Y           *= -1;
            }
        }
コード例 #8
0
        private void BounceOffRight(Object other)
        {
            if (this.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;

                float dist = (r.Position.X + r.Size.X) - (c.Position.X - c.Radius);
                this.Collision.Position.X += dist * 2;
                this.Velocity.X           *= -1;
            }
            else
            {
                RectangleBody r1 = (RectangleBody)this.Collision;
                RectangleBody r2 = (RectangleBody)other.Collision;

                float dist = (r2.Position.X + r2.Size.X) - (r1.Position.X);
                this.Collision.Position.X += dist * 2;
                this.Velocity.X           *= -1;
            }
        }
コード例 #9
0
        private List <PhysicsGridTile> GetCoveredTiles(CollisionBody body)
        {
            int xMin = 0;
            int xMax = 0;
            int yMin = 0;
            int yMax = 0;

            if (body.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody circBody = (CircleBody)body;
                xMin = ((int)(Math.Max(circBody.Position.X - circBody.Radius / 2, 0)) / tileSize);
                xMax = (int)(Math.Min(circBody.Position.X + circBody.Radius / 2, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(circBody.Position.Y - circBody.Radius / 2, 0)) / tileSize);
                yMax = (int)(Math.Min(circBody.Position.Y + circBody.Radius / 2, maxHeight - 1)) / tileSize;
            }
            else if (body.Shape == CollisionBody.ShapeType.Rectangle)
            {
                RectangleBody rectBody = (RectangleBody)body;
                xMin = ((int)(Math.Max(rectBody.Position.X, 0)) / tileSize);
                xMax = (int)(Math.Min(rectBody.Position.X + rectBody.Size.X, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(rectBody.Position.Y, 0)) / tileSize);
                yMax = (int)(Math.Min(rectBody.Position.Y + rectBody.Size.Y, maxWidth - 1)) / tileSize;
            }

            List <PhysicsGridTile> toReturn = new List <PhysicsGridTile>();

            for (int y = yMin; y <= yMax; y++)
            {
                for (int x = xMin; x <= xMax; x++)
                {
                    toReturn.Add(Tiles[y * columns + x]);
                }
            }

            return(toReturn);
        }
コード例 #10
0
        public void BounceOff(Object other)
        {
            Vector2 velocityToCheck = this.Velocity - other.Velocity;

            if (this.Collision.Shape == CollisionBody.ShapeType.Circle && other.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                // Circle on Circle
            }
            else if (this.Collision.Shape == CollisionBody.ShapeType.Circle && other.Collision.Shape == CollisionBody.ShapeType.Rectangle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;
                if (velocityToCheck.X >= 0)
                {
                    // Moving right
                    if (velocityToCheck.Y >= 0)
                    {
                        // Moving right-down
                        if (r.Position.X - c.Position.X <= r.Position.Y - c.Position.Y)
                        {
                            this.BounceOffTop(other);
                        }
                        else
                        {
                            this.BounceOffLeft(other);
                        }
                    }
                    else
                    {
                        // Moving right-up
                        if (-1 * (r.Position.X - c.Position.X) <= (r.Position.Y + r.Size.Y - c.Position.Y))
                        {
                            this.BounceOffLeft(other);
                        }
                        else
                        {
                            this.BounceOffBottom(other);
                        }
                    }
                }
                else
                {
                    // Moving left
                    if (velocityToCheck.Y >= 0)
                    {
                        // Moving left-down
                        if (r.Position.X + r.Size.X - c.Position.X <= -1 * (r.Position.Y - c.Position.Y))
                        {
                            this.BounceOffRight(other);
                        }
                        else
                        {
                            this.BounceOffTop(other);
                        }
                    }
                    else
                    {
                        // Moving left-up
                        if (r.Position.X + r.Size.X - c.Position.X <= r.Position.Y + r.Size.Y - c.Position.Y)
                        {
                            this.BounceOffRight(other);
                        }
                        else
                        {
                            this.BounceOffBottom(other);
                        }
                    }
                }
            }
        }