Ejemplo 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;
    }
Ejemplo n.º 2
0
    protected void HandleEntityCollisions(string id)
    {
        //check entity collision
        Entity entity = GameWorld.GetObject(id) as Entity;

        if (entity == null || !BoundingBox.Intersects(entity.BoundingBox))
        {
            return;
        }

        Vector2 depth       = Collision.CalculateIntersectionDepth(BoundingBox, entity.BoundingBox);
        int     totalWeight = weight + entity.Weight;
        float   push        = ((float)weight / (float)totalWeight);
        Item    item        = entity as Item;

        if (Math.Abs(depth.X) < Math.Abs(depth.Y))
        {
            //move position
            position.X += depth.X;
            if (item != null && item.ItemType == ItemType.InMovible)
            {
                return;
            }
            entity.position -= new Vector2(depth.X * push, 0);
            return;
        }
        position.Y += depth.Y;
        if (item != null && item.ItemType == ItemType.InMovible)
        {
            return;
        }
        entity.position -= new Vector2(0, depth.Y * push);
    }
Ejemplo n.º 3
0
    protected virtual void HandleCollisions()
    {
        LevelGrid tiles = GameWorld.GetObject("levelgrid") as LevelGrid;

        //check surrounding tiles
        for (int x = (int)gridPos.X - 2; x <= (int)gridPos.X + 2; x++)
        {
            for (int y = (int)gridPos.Y - 2; y <= (int)gridPos.Y + 2; y++)
            {
                TileType tileType    = tiles.GetTileType(x, y);
                Tile     currentTile = tiles.Get(x, y) as Tile;

                Vector2   tilePos = new Vector2(x * tiles.CellWidth / 2 - tiles.CellWidth / 2 * y, y * tiles.CellHeight / 2 + tiles.CellHeight / 2 * x);
                Rectangle tileBounds;
                if (currentTile == null)
                {
                    tileBounds = new Rectangle((int)(tilePos.X - tiles.CellWidth / 2), (int)(tilePos.Y - tiles.CellHeight / 2), tiles.CellWidth, tiles.CellHeight);
                }
                else
                {
                    for (int i = 0; i < currentTile.Passengers.Count; i++)
                    {
                        if (currentTile.Passengers[i] != id)
                        {
                            //check tile passenger collision
                            HandleEntityCollisions(currentTile.Passengers[i]);
                        }
                    }
                    //check collision
                    if (tileType == TileType.Floor)
                    {
                        continue;
                    }
                    tileBounds = currentTile.GetBoundingBox();
                }

                if (!tileBounds.Intersects(BoundingBox))
                {
                    continue;
                }

                //mouve position
                Vector2 depth = Collision.CalculateIntersectionDepth(BoundingBox, tileBounds);
                if (Math.Abs(depth.X) < Math.Abs(depth.Y))
                {
                    position.X += depth.X;
                    continue;
                }
                position.Y += depth.Y;
            }
        }
    }
Ejemplo n.º 4
0
    private void HandleEntityCollisions(string id)
    {
        Entity entity = GameWorld.GetObject(id) as Entity;

        if (entity == null || !BoundingBox.Intersects(entity.BoundingBox))
        {
            return;
        }

        Vector2 depth       = Collision.CalculateIntersectionDepth(BoundingBox, entity.BoundingBox);
        int     totalWeight = weight + entity.Weight;
        float   push        = ((float)weight / (float)totalWeight);

        if (Math.Abs(depth.X) < Math.Abs(depth.Y))
        {
            position.X      += depth.X;
            entity.position -= new Vector2(depth.X * push, 0);
            return;
        }
        position.Y      += depth.Y;
        entity.position -= new Vector2(0, depth.Y * push);
    }
    /// <summary>Check and handle the collisions.</summary>
    public void CheckCollisions()
    {
        TileField tiles   = GameWorld.Find("tiles") as TileField;
        int       x_floor = (int)position.X / tiles.CellWidth;
        int       y_floor = (int)position.Y / tiles.CellHeight;

        // tile collisions
        for (int y = y_floor - 2; y <= y_floor + 1; ++y)
        {
            for (int x = x_floor - 1; x <= x_floor + 1; ++x)
            {
                if (y < 0 || y >= tiles.Rows || x < 0 || x >= tiles.Columns)
                {
                    continue;
                }

                TileType tileType = tiles.GetTileType(x, y);
                if (tileType == TileType.Normal || tileType == TileType.Platform)
                {
                    Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight,
                                                         tiles.CellWidth, tiles.CellHeight);
                    if (tileBounds.Intersects(BoundingBox))
                    {
                        Reset();
                        return;
                    }
                }
            }
        }

        Player player = GameWorld.Find("player") as Player;

        // projectile collisions
        foreach (Projectile projectile in player.Projectiles)
        {
            if (BoundingBox.Intersects(projectile.BoundingBox) && projectile.Active)
            {
                projectile.Hit = true;
                hp--;
                if (hp <= 0)
                {
                    Reset();
                    return;
                }
            }
        }

        // player collision
        if (CollidesWith(player))
        {
            Vector2 depth = Collision.CalculateIntersectionDepth(BoundingBox, player.BoundingBox);
            if (player.BoundingBox.Y < BoundingBox.Y && player.Velocity.Y > 0 && depth.X > depth.Y)
            {
                velocity.Y = 600; player.Jump();
            }
            else
            {
                player.LowerHP(50);
                Reset();
            }
        }
    }
    /// <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;
    }