Exemplo n.º 1
0
    private void HandleCollisions(bool movingThroughPlatform)
    {
        isOnTheGround = false;
        walkingOnIce  = false;
        walkingOnHot  = false;

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

        for (int y = yFloor - 2; y <= yFloor + 1; ++y)
        {
            for (int x = xFloor - 1; x <= xFloor + 1; ++x)
            {
                TileType tileType = tiles.GetTileType(x, y);
                if (tileType == TileType.Background)
                {
                    continue;
                }
                Tile      currentTile = tiles.Get(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)
                {
                    if (tileType == TileType.Platform && movingThroughPlatform)
                    {
                        continue;
                    }
                    isOnTheGround = true;
                    velocity.Y    = 0;
                    if (currentTile != null)
                    {
                        walkingOnIce = walkingOnIce || currentTile.Ice;
                        walkingOnHot = walkingOnHot || currentTile.Hot;
                    }
                }
                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;
    }
    /// <summary>Handle the player collisions.</summary>
    private void HandleCollisions()
    {
        isOnTheGround = false;
        walkingOnIce  = false;
        walkingOnHot  = 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;
                }
                // solid tile
                Tile      currentTile = tiles.Get(x, y) as Tile;
                Rectangle tileBounds  = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight,
                                                      tiles.CellWidth, tiles.CellHeight);
                Rectangle boundingBox = BoundingBox;
                boundingBox.Height += 1;
                if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox))
                {
                    continue;
                }
                // colliding
                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)
                {
                    if (velocity.Y > 1250) // fall damage
                    {
                        LowerHP(((int)velocity.Y - 1250) / 100);
                    }
                    isOnTheGround = true;
                    velocity.Y    = 0;
                    if (currentTile != null) // update status effects
                    {
                        walkingOnIce = currentTile.Ice;
                        walkingOnHot = currentTile.Hot;
                    }
                }
                if (tileType == TileType.Normal || isOnTheGround)
                {
                    position.Y += depth.Y + 1; // make sure we stand on top of the tile
                }
            }
        }
        position          = new Vector2((int)position.X, (int)position.Y);
        previousYPosition = position.Y;
    }
Exemplo n.º 3
0
    public void tileCollide()
    {
        TileField tiles = GameWorld.Find("tiles") as TileField;

        for (int y = 0; y < tiles.CellHeight - 1; y++)
        {
            for (int x = 0; x < tiles.CellWidth - 1; x++)
            {
                if (tiles.GetTileType(x, y) != TileType.Background && tiles.Get(x, y) != null)
                {
                    Tile collisionTile = tiles.Get(x, y) as Tile;
                    if (CollidesWith(collisionTile))
                    {
                        if (!collisionTile.Ice && !collisionTile.Hot)
                        {
                            Vector2 vectorFromTile = (collisionTile.Center + collisionTile.GlobalPosition) - (this.Center + this.GlobalPosition);
                            if (Math.Abs(vectorFromTile.X) > Math.Abs(vectorFromTile.Y) * (((float)collisionTile.Width) / (float)collisionTile.Height))
                            {
                                if (Math.Sign(vectorFromTile.X) == Math.Sign(velocity.X))
                                {
                                    velocity.X *= -.9f;
                                }
                                if (velocity.X > 0)
                                {
                                    position.X = collisionTile.Position.X + collisionTile.Width;
                                }
                                else
                                {
                                    position.X = collisionTile.Position.X - this.Width;
                                }
                            }
                            else
                            {
                                if (Math.Sign(vectorFromTile.Y) == Math.Sign(velocity.Y))
                                {
                                    velocity.Y *= -.9f;
                                }
                                if (velocity.Y > 0)
                                {
                                    position.Y = collisionTile.Position.Y + collisionTile.Width;
                                }
                                else
                                {
                                    position.Y = collisionTile.Position.Y - this.Height;
                                }
                            }
                            return;
                        }
                        else if (collisionTile.Ice)
                        {
                            velocity = Vector2.Zero;
                            frozen   = true;
                        }
                        else if (collisionTile.Hot)
                        {
                            explode();
                        }
                    }
                }
            }
        }
    }