Exemplo n.º 1
0
    private void HandleCollisions()
    {
        IsOnTheGround = false;
        WalkingOnIce  = false;

        TileField tiles   = GameWorld.Find("tiles") as TileField;
        int       x_floor = (int)position.X / tiles.CellWidth;
        int       y_floor = (int)position.Y / tiles.CellHeight;

        for (int y = y_floor - 2; y <= y_floor + 1; ++y)
        {
            for (int x = x_floor - 1; x <= x_floor + 1; ++x)
            {
                TileType tileType = tiles.GetTileType(x, y);
                if (tileType == TileType.Background)
                {
                    continue;
                }
                Tile      currentTile = tiles.findAt(x, y) as Tile;
                Rectangle tileBounds  = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight,
                                                      tiles.CellWidth, tiles.CellHeight);
                Rectangle boundingBox = this.BoundingBox;
                boundingBox.Height += 1;
                if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox))
                {
                    continue;
                }
                Vector2 depth = Collision.CalculateIntersectionDepth(boundingBox, tileBounds);
                if (Math.Abs(depth.X) < Math.Abs(depth.Y))
                {
                    if (tileType == TileType.Normal)
                    {
                        position.X += depth.X;
                    }
                    continue;
                }
                if (PreviousYPosition <= tileBounds.Top && tileType != TileType.Background)
                {
                    IsOnTheGround = true;
                    velocity.Y    = 0;
                    if (currentTile != null)
                    {
                        WalkingOnIce = WalkingOnIce || currentTile.Ice;
                    }
                }
                if (tileType == TileType.Normal || IsOnTheGround)
                {
                    position.Y += depth.Y + 1;
                }
            }
        }
        position          = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y));
        PreviousYPosition = position.Y;
    }