Esempio n. 1
0
        private void HandleCollisions(TileMap tileMap, GameTime gameTime)
        {
            // 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;

            if (IsAlignedToLadder() == false)
            {
                isOnLadder = 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,
                    TileCollision collision = level.GetCollision(x, y);

                    if (collision != TileCollision.Passable)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle tileBounds = level.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 == TileCollision.Ladder && movement.Y > 0.25f &&
                                IsAlignedToLadder() == true)
                            {
                                isOnLadder = true;
                            }

                            // Resolve the collision along the shallow axis.
                            if (absDepthY < absDepthX && collision != TileCollision.Patrol ||
                                collision == TileCollision.Platform)
                            {
                                // If we crossed the top of a tile, we are on the ground.
                                if (previousBottom <= tileBounds.Top && collision != TileCollision.Ladder)
                                {
                                    isOnGround = true;
                                    camera.MoveTrapUp(tileBounds.Top);
                                }

                                // Ignore platforms, unless we are on the ground.
                                if (collision == TileCollision.Impassable || IsOnGround)
                                {
                                    // Resolve the collision along the Y axis.
                                    Position = new Vector2(Position.X, Position.Y + depth.Y);

                                    // Perform further collisions with the new bounds.
                                    bounds = BoundingRectangle;
                                }
                            }
                            else if (collision == TileCollision.Impassable) // 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;
        }
Esempio n. 2
0
        public void ApplyPhysics(GameTime gameTime, TileMap tileMap)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (IsKnockedBack == true)
            {
                counter            += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                stopMovement        = true;
                StopPlayerCollision = true;
                color = Color.Red;

                if (knockbackDirection == false)
                {
                    if (velocity.X > 0)
                    {
                        moveback = -20000;
                    }
                    else
                    {
                        moveback = 20000;
                    }

                    knockbackDirection = true;
                }

                if (counter < 100)
                {
                    velocity   = Vector2.Zero;
                    velocity.X = moveback * elapsed;
                    velocity.Y = -smallJump * elapsed;

                    velocity.Y = MathHelper.Clamp(
                        velocity.Y + GravityAcceleration * elapsed,
                        -MaxFallSpeed,
                        MaxFallSpeed);

                    Position += velocity * elapsed;
                }

                if (counter > 600)
                {
                    stopMovement = false;
                    IsHit        = false;

                    if (counter > 1500)
                    {
                        color               = Color.White;
                        counter             = 0;
                        IsKnockedBack       = false;
                        StopPlayerCollision = false;

                        moveback           = 0;
                        knockbackDirection = false;
                    }
                }
            }

            ChargeAttackColour();
            PlayerMovement(gameTime);
            HandleCollisions(tileMap, gameTime);
        }
Esempio n. 3
0
 public void ApplyPhysics(GameTime gameTime, TileMap tileMap)
 {
     PlayerMovement(gameTime);
     HandleCollisions(tileMap, gameTime);
 }