Esempio n. 1
0
        protected override void ExecuteCommand(IMap map, DungeonParameters parameters)
        {
            var sparseFactor = parameters.CellSparseFactor;
            var expectedNumberOfRemovedCells =
                   (int)Math.Ceiling(map.Size * (sparseFactor / 100m)) - 1;

            var removedCellsCount = 0;
            var nonWalls = map.Where(c => !c.IsWall).ToList();
            if (!nonWalls.Any())
            {
                throw new InvalidOperationException("All cells are walls.");
            }

            while (removedCellsCount < expectedNumberOfRemovedCells)
            {
                foreach (var cell in nonWalls.Where(c => c.IsDeadEnd).ToList())
                {
                    if (!cell.IsDeadEnd)
                        continue;

                    var emptySide = cell.Sides
                        .Single(s => s.Value != Side.Wall)
                        .Key;

                    map.CreateWall(cell, emptySide);
                    cell.IsCorridor = false;
                    nonWalls.Remove(cell);
                    removedCellsCount++;
                }
            }
        }
Esempio n. 2
0
        public IDungeon ConvertToDungeon(IMap map)
        {
            var dungeon = new Dungeon(map.Width * 2 + 1, map.Height * 2 + 1);

            for (var x = 0; x < map.Width * 2 + 1; x++)
            {
                for (var y = 0; y < map.Height * 2 + 1; y++)
                {
                    dungeon[x, y].Type = XType.Wall;
                }
            }

            foreach (var room in map.Rooms)
            {
                var roomMinPoint = new Point(
                    room.Bounds.Location.X * 2 + 1,
                    room.Bounds.Location.Y * 2 + 1);
                var roomMaxPoint = new Point(
                    room.Bounds.Right * 2,
                    room.Bounds.Bottom * 2);

                // Fill tiles with corridor values for each room in dungeon
                for (var i = roomMinPoint.X; i < roomMaxPoint.X; i++)
                    for (var j = roomMinPoint.Y; j < roomMaxPoint.Y; j++)
                        dungeon[i, j].Type = XType.Empty;
            }

            // Expand it each corridor cell
            foreach (var cell in map.Where(c => c.IsCorridor))
            {
                var location = new Point(
                    cell.Location.X * 2 + 1,
                    cell.Location.Y * 2 + 1);

                dungeon[location.X, location.Y].Type = XType.Empty;

                switch (map[cell.Location].Sides[Dir.N])
                {
                    case Side.Empty:
                        dungeon[location.X, location.Y - 1].Type = XType.Empty;
                        break;
                    case Side.Door:
                        dungeon[location.X, location.Y - 1].Type = XType.DoorClosed;
                        break;
                }

                switch (map[cell.Location].Sides[Dir.S])
                {
                    case Side.Empty:
                        dungeon[location.X, location.Y + 1].Type = XType.Empty;
                        break;
                    case Side.Door:
                        dungeon[location.X, location.Y + 1].Type = XType.DoorClosed;
                        break;
                }

                switch (map[cell.Location].Sides[Dir.W])
                {
                    case Side.Empty:
                        dungeon[location.X - 1, location.Y].Type = XType.Empty;
                        break;
                    case Side.Door:
                        dungeon[location.X - 1, location.Y].Type = XType.DoorClosed;
                        break;
                }

                switch (map[cell.Location].Sides[Dir.E])
                {
                    case Side.Empty:
                        dungeon[location.X + 1, location.Y].Type = XType.Empty;
                        break;
                    case Side.Door:
                        dungeon[location.X + 1, location.Y].Type = XType.DoorClosed;
                        break;
                }
            }

            return dungeon;
        }
Esempio n. 3
0
        public void FindNeighbors(IMap map)
        {
            Point[] coordinates = new Point[8];
            Directions[] directionArray = new Directions[8];
            int adjustedSize = this.rectangle.Width;   // size is distance from center to edge,
            // double to ensure we end up outside of this box

            directionArray[(int)Directions.East] = Directions.East;
            directionArray[(int)Directions.North] = Directions.North;
            directionArray[(int)Directions.NorthEast] = Directions.NorthEast;
            directionArray[(int)Directions.NorthWest] = Directions.NorthWest;
            directionArray[(int)Directions.South] = Directions.South;
            directionArray[(int)Directions.SouthEast] = Directions.SouthEast;
            directionArray[(int)Directions.SouthWest] = Directions.SouthWest;
            directionArray[(int)Directions.West] = Directions.West;

            coordinates[(int)Directions.East] = new Point(this.rectangle.Center.X + adjustedSize, this.rectangle.Center.Y);
            coordinates[(int)Directions.North] = new Point(this.rectangle.Center.X, this.rectangle.Center.Y - adjustedSize);
            coordinates[(int)Directions.NorthEast] = new Point(this.rectangle.Center.X + adjustedSize, this.rectangle.Center.Y - adjustedSize);
            coordinates[(int)Directions.NorthWest] = new Point(this.rectangle.Center.X - adjustedSize, this.rectangle.Center.Y - adjustedSize);
            coordinates[(int)Directions.South] = new Point(this.rectangle.Center.X, this.rectangle.Center.Y + adjustedSize);
            coordinates[(int)Directions.SouthEast] = new Point(this.rectangle.Center.X + adjustedSize, this.rectangle.Center.Y + adjustedSize);
            coordinates[(int)Directions.SouthWest] = new Point(this.rectangle.Center.X - adjustedSize, this.rectangle.Center.Y + adjustedSize);
            coordinates[(int)Directions.West] = new Point(this.rectangle.Center.X - adjustedSize, this.rectangle.Center.Y);

            foreach (Directions d in directionArray)
            {
                int index = (int)d;
                Point point = coordinates[index];
                ITile tile = map.Where(x => x.Rectangle.Center == point).SingleOrDefault() ?? new NullTile(point.ToVector());

                this.adjacentTiles[index] = tile;
            }

            this.enabled = true;
        }