Beispiel #1
0
        public void MoveWithCollision(Room room)
        {
            if (Velocity.X > 0)
            {
                int x = ((position.X + Velocity.X) / Resolution + Width) / room.TileMap.TileSize.X;

                int yStart = Y / room.TileMap.TileSize.Y;
                int yEnd = (Y + Height - 2) / room.TileMap.TileSize.Y;

                for (int y = yStart; y <= yEnd; y++)
                {
                    if (room.TileMap.IsSolid(x, y))
                    {
                        X = x * room.TileMap.TileSize.X - Width;
                        Velocity.X = 0;
                        break;
                    }
                }

                if (Velocity.X != 0)
                {
                    IList<Entity> solids = room.GetCollidingSolidEntities(new Rectangle((position.X + Velocity.X) / Resolution, Y, Width, Height));

                    foreach (Entity s in solids)
                    {
                        if (s == this) continue;

                        X = s.X - Width;
                        Velocity.X = 0;
                    }
                }
            }
            else if (Velocity.X < 0)
            {
                int x = ((position.X + Velocity.X) / Resolution) / room.TileMap.TileSize.X;

                int yStart = Y / room.TileMap.TileSize.Y;
                int yEnd = (Y + Height - 2) / room.TileMap.TileSize.Y;

                for (int y = yStart; y <= yEnd; y++)
                {
                    if (room.TileMap.IsSolid(x, y))
                    {
                        X = (1 + x) * room.TileMap.TileSize.X;
                        Velocity.X = 0;
                        break;
                    }
                }

                if (Velocity.X != 0)
                {
                    IList<Entity> solids = room.GetCollidingSolidEntities(new Rectangle((position.X + Velocity.X) / Resolution, Y, Width, Height));

                    foreach (Entity s in solids)
                    {
                        if (s == this) continue;

                        X = s.X + s.Width;
                        Velocity.X = 0;
                    }
                }
            }

            if (Velocity.Y > 0)
            {
                int y = ((position.Y + Velocity.Y) / Resolution + Height) / room.TileMap.TileSize.Y;

                int xStart = ((position.X + Velocity.X) / Resolution) / room.TileMap.TileSize.X;
                int xEnd = ((position.X + Velocity.X) / Resolution + Width - 1) / room.TileMap.TileSize.X;

                for (int x = xStart; x <= xEnd; x++)
                {
                    if (room.TileMap.IsSolid(x, y))
                    {
                        Y = y * room.TileMap.TileSize.Y - Height;
                        Velocity.Y = 0;
                        break;
                    }
                }

                if (Velocity.Y != 0)
                {
                    IList<Entity> solids = room.GetCollidingSolidEntities(new Rectangle((position.X + Velocity.X) / Resolution, (position.Y + Velocity.Y) / Resolution, Width, Height));

                    foreach (Entity s in solids)
                    {
                        if (s == this) continue;

                        Y = s.Y - Height;
                        Velocity.Y = 0;
                    }
                }
            }
            else if (Velocity.Y < 0)
            {
                int y = ((position.Y + Velocity.Y) / Resolution - 1) / room.TileMap.TileSize.Y;

                int xStart = ((position.X + Velocity.X) / Resolution) / room.TileMap.TileSize.X;
                int xEnd = ((position.X + Velocity.X) / Resolution + Width - 1) / room.TileMap.TileSize.X;

                for (int x = xStart; x <= xEnd; x++)
                {
                    if (room.TileMap.IsSolid(x, y))
                    {
                        Y = (1 + y) * room.TileMap.TileSize.Y;
                        Velocity.Y = 0;
                        break;
                    }
                }

                if (Velocity.Y != 0)
                {
                    IList<Entity> solids = room.GetCollidingSolidEntities(new Rectangle((position.X + Velocity.X) / Resolution, (position.Y + Velocity.Y) / Resolution, Width, Height));

                    foreach (Entity s in solids)
                    {
                        if (s == this) continue;

                        Y = s.Y + s.Height;
                        Velocity.Y = 0;
                    }
                }
            }

            MoveWithoutCollision();
        }
Beispiel #2
0
        public bool IsTileSolidBelow(Room room)
        {
            int y = (Dimension.Y + Dimension.Height) / room.TileMap.TileSize.Y;

            int xStart = Dimension.X / room.TileMap.TileSize.X;
            int xEnd = (Dimension.X + Dimension.Width - 1) / room.TileMap.TileSize.X;

            for (int x = xStart; x <= xEnd; x++)
            {
                if (room.TileMap.IsSolid(x, y))
                {
                    return true;
                }
            }

            IList<Entity> solids = room.GetCollidingSolidEntities(new Rectangle(X, Y + Height, Width, 1));

            return solids.Count > 0 && !(solids.Count == 1 && solids.Contains(this));
        }