Beispiel #1
0
        public void Print(Map map)
        {
            SimpleNoise spikeNoise = new SimpleNoise(Random.Next());

            if (map.Width != Width || map.Height != Height)
            {
                throw new Exception();
            }
            var tiles = Rooms.SelectMany(x => x.GetCoveredTiles().Select(y => new Tuple <Room, Point>(x, y))).GroupBy(pair => pair.Item2, pair => pair.Item1);

            foreach (var rooms in tiles)
            {
                var        pos         = rooms.Key;
                var        tile        = map.GetTile(pos.X, pos.Y);
                var        count       = rooms.Count();
                var        singleRoom  = count == 1 ? rooms.Single() : null;
                RoomType[] normalRooms = new[] { RoomType.None, RoomType.Start, RoomType.End, RoomType.Corridor };
                RoomType[] startEnd    = new[] { RoomType.Start, RoomType.End };
                Template[] floors      = new[] { null, Template.Corridor };

                Template template = null;

                if (singleRoom != null && singleRoom.Type == RoomType.Filled)
                {
                    template = Template.Wall;
                }
                if (singleRoom != null && singleRoom.Type == RoomType.Chasm)
                {
                    template = Template.Chasm;
                }
                if (count > 1)
                {
                    template = Template.Wall;
                }
                if (rooms.Any(x => x.Type == RoomType.Chasm))
                {
                    template = Template.Corridor;
                }
                if (rooms.All(x => x.Type == RoomType.Chasm))
                {
                    template = Template.Chasm;
                }
                if (rooms.All(x => x.Type == RoomType.Corridor))
                {
                    template = Template.Corridor;
                }
                if (singleRoom?.Type == RoomType.Start || singleRoom?.Type == RoomType.End)
                {
                    template = Template.Floor;
                }
                if (count == 2 && IsConnected(rooms.First(), rooms.Last()) && !floors.Contains(template))
                {
                    template = null;
                }
                if (IsEdgeTile(pos) && floors.Contains(template) && !rooms.All(x => startEnd.Contains(x.Type)))
                {
                    template = Template.Wall;
                }

                int spikeValue = spikeNoise.GetValue(pos.X / 3, pos.Y / 3);

                if (template == Template.Wall && (spikeValue % 10 == 0 || spikeNoise.GetValue(pos.X, pos.Y) % 30 == 0))
                {
                    template = Template.SpikeWall;
                }
                if (template == Template.Wall && Random.NextDouble() < 0.1)
                {
                    template = Template.WraithWall;
                }

                if (template == null)
                {
                    template = Template.Floor;
                }

                if (template != null)
                {
                    tile.ApplyTemplate(template);
                }
                if (rooms.Any(x => x.Type == RoomType.Chasm) && template != Template.Chasm)
                {
                    Behavior.Apply(new BehaviorChasmSeam(tile, template == Template.Corridor ? new Color(155, 99, 74) : new Color(124, 88, 114)));
                }
                if (singleRoom?.Type == RoomType.Start)
                {
                    Behavior.Apply(new BehaviorLevelStart(tile, singleRoom.GetEdgeDirection()));
                }
                if (singleRoom?.Type == RoomType.End)
                {
                    Behavior.Apply(new BehaviorLevelEnd(tile, singleRoom.GetEdgeDirection()));
                    if (singleRoom.Interior.Center == pos)
                    {
                        var pointer = new Curio(Template.Pointer);
                        pointer.MoveTo(tile);
                        Behavior.Apply(new BehaviorEscapeTarget(pointer, singleRoom.Interior));
                    }
                }
            }
        }