Esempio n. 1
0
        private void HandleCollisions()
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = BoundingRectangle;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;


            // For each potentially colliding tile,
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    // If this tile is collidable,
                    TileType collision = game.map.GetCollision(x, y);
                    if (collision != TileType.Ground)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle tileBounds = game.map.GetBounds(x, y);
                        Vector2   depth      = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);

                            // Resolve the collision along the shallow axis.
                            if (collision == TileType.Wall)
                            {
                                if (absDepthY < absDepthX)
                                {
                                    // Resolve the collision along the Y axis.
                                    Position         = new Vector2(Position.X, Position.Y + depth.Y);
                                    velocity.Y      += depth.Y;
                                    mousePosition.Y += depth.Y;
                                }
                                if (absDepthX < absDepthY)
                                {
                                    Position         = new Vector2(Position.X + depth.X, Position.Y);
                                    velocity.X      += depth.X;
                                    mousePosition.X += depth.X;
                                }

                                // Perform further collisions with the new bounds.
                                bounds = BoundingRectangle;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void Reset()
        {
            int x = rand.Next(1, game.map.Width - 2);
            int y = rand.Next(1, game.map.Height - 2);

            position = RectangleExtensions.GetBottomCenter(game.map.GetBounds(x, y));
            //position = new Vector2(100, 100);
            health     = 200;
            shootWait  = 0.0f;
            isShooting = false;
            isRunning  = false;
            //Position = position;
            Velocity = Vector2.Zero;
            isAlive  = true;

            //mousePosition = position;
            Mouse.SetPosition((int)position.X, (int)position.Y);
            sprite.PlayAnimation(idleAnimation);
        }
Esempio n. 3
0
        public Enemy(Game game, Random rnd)
        {
            this.game = game;
            int x = rnd.Next(1, game.map.Width - 2);
            int y = rnd.Next(1, game.map.Height - 2);
            //LoadEnemyTile(x, y, "Zombie");
            Vector2 position = RectangleExtensions.GetBottomCenter(game.map.GetBounds(x, y));

            this.position = position;
            direction     = (float)rnd.NextDouble() * MathHelper.TwoPi;
            this.velocity = new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
            velocity.Normalize();
            this.health  = 1000;
            this.isAlive = true;
            behaviors    = new Dictionary <EnemyType, Behaviors>();
            MoveSpeed    = 0.1f;
            //BuildBehaviors();
            //LoadContent("Default");
        }
Esempio n. 4
0
        private void HandleCollisions()
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = BoundingRectangle;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            // Reset flag to search for ground collision.
            //isOnGround = false;

            // For each potentially colliding tile,
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    // If this tile is collidable,
                    TileType collision = map.GetCollision(x, y);
                    if (collision != TileType.Ground)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle tileBounds = Map.GetBounds(x, y);
                        Vector2   depth      = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);

                            if (collision == TileType.Wall)
                            {
                                // Y Axis Collision
                                if (absDepthY < absDepthX)
                                {
                                    position = new Vector2(Position.X, Position.Y + depth.Y);
                                    if (velocity.X > 0 && absDepthX < 44)
                                    {
                                        position.X += absDepthY;
                                    }
                                    else if (velocity.X < 0 && absDepthX < 44)
                                    {
                                        position.X -= absDepthY;
                                    }
                                }
                                //X Axis Collision
                                if (absDepthX < absDepthY)
                                {
                                    position = new Vector2(Position.X + depth.X, Position.Y);
                                    if (velocity.Y > 0 && absDepthY < 44)
                                    {
                                        position.Y += absDepthX;
                                    }
                                    else if (velocity.Y < 0 && absDepthY < 44)
                                    {
                                        position.Y -= absDepthX;
                                    }
                                }

                                // Perform further collisions with the new bounds.
                                bounds = BoundingRectangle;
                            }


                            //else if (collision == TileType.Wall) // Ignore platforms.
                            //{
                            //    // Resolve the collision along the X axis.
                            //    Position = new Vector2(Position.X + depth.X, Position.Y);

                            //    // Perform further collisions with the new bounds.
                            //    bounds = BoundingRectangle;
                            //}
                        }
                    }
                }
            }

            // Save the new bounds bottom.
            //previousBottom = bounds.Bottom;
        }