Ejemplo n.º 1
0
        //Rectangle
        public bool Collide(float left, float top, float right, float bottom)
        {
            //Need to check every tile that this rect contains for collisions.
            var startX = (int)(left / CellWidth);
            var startY = (int)(top / CellHeight);
            var endX   = (int)(right / CellWidth);
            var endY   = (int)(bottom / CellHeight);

            var key = new Point(0, 0);

            for (int x = startX; x <= endX; x++)
            {
                for (int y = startY; y <= endY; y++)
                {
                    key.X = x;
                    key.Y = y;
                    if (grid.Contains(key))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        private static InfiniteGrid <TileWithOrientation>?Search(List <Tile> tiles,
                                                                 InfiniteGrid <TileWithOrientation> grid, int size)
        {
            if (grid.Width > size || grid.Height > size)
            {
                return(null);
            }

            if (grid.Width == size && grid.Height == size && grid.Positions().Count() == size * size)
            {
                return(grid);
            }

            var openPositions = grid.Positions()
                                .SelectMany(p => p.Orthogonal())
                                .Where(p => !grid.Contains(p));

            foreach (var position in openPositions)
            {
                var constraints = new List <FacingAndValue> ();
                foreach (var facing in new[] { Vector.North, Vector.East, Vector.South, Vector.West }.Zip(new[] { 0, 1, 2, 3 }))
                {
                    if (grid.TryPosition(position + facing.First, out var borderingCell))
                    {
                        var borderingCellsFacing = facing.Second switch
                        {
                            0 => 2,
                            1 => 3,
                            2 => 0,
                            3 => 1,
                            _ => throw new ApplicationException()
                        };
                        constraints.Add(new FacingAndValue(facing: facing.Second, borderingCell.Borders[borderingCellsFacing]));
                    }
                }

                var validTiles = new List <TileWithOrientation>();
                foreach (var tile in tiles)
                {
                    var c = constraints.First();
                    if (!tile.Orientations.TryGetValue(c, out var proposed))
                    {
                        continue;
                    }
                    foreach (var constraint in constraints.Skip(1))
                    {
                        if (!tile.Orientations.TryGetValue(constraint, out var proposed2) ||
                            proposed != proposed2)
                        {
                            proposed = null;
                            break;
                        }
                    }

                    if (proposed == null)
                    {
                        break;
                    }
                    validTiles.Add(proposed);
                }

                foreach (var tile in validTiles)
                {
                    var gridCopy = grid.Clone();
                    gridCopy.Add(position, tile);
                    var tilesCopy = tiles.ToList();
                    tilesCopy.Remove(tile.Tile);
                    var newGrid = Search(tilesCopy, gridCopy, size);
                    if (newGrid != null)
                    {
                        return(newGrid);
                    }
                }
            }

            return(null);
        }