예제 #1
0
        private void AddBuildings()
        {
            var allRooms  = new Dictionary <Room, Point>();
            var needRooms = CityBlockIds.Length * 3;

            foreach (var blockId in CityBlockIds)
            {
                var rooms = LayerHelper.GenerateRooms(World.Rnd, BaseMapBlock.Rect.Inflate(-2, -2).Offset(2, 2), new Point[0], blockId).OrderByDescending(_room => _room.RoomRectangle.Size);
                foreach (var room in rooms)
                {
                    allRooms.Add(room, blockId);
                    //if (allRooms.Count >= needRooms)
                    //{
                    //    break;
                    //}
                }
            }

            var availableBuildings = Util.GetAllTypesOf <Building>().Select(_type => (Building)Activator.CreateInstance(_type, this)).ToArray();
            var availableRoles     = Util.GetAllTypesOf <AbstractCitizenRole>().Select(_type => (AbstractCitizenRole)Activator.CreateInstance(_type)).ToArray();

            var buildings = new List <Building>();

            var surface = Layer as Surface;

            if (surface == null)
            {
                throw new NotImplementedException();
            }

            foreach (var abuilding in availableBuildings)
            {
                for (var i = 0; i < abuilding.MinCountInCity; i++)
                {
                    var building = (Building)Activator.CreateInstance(abuilding.GetType(), this);

                    var role = availableRoles.FirstOrDefault(_role => _role.BuildingType == building.BuildingType);
                    if (role != null)
                    {
                        var citizen     = new Citizen(Layer, World.Rnd, surface.GetNextCitizenName(new[] { ESex.MALE, ESex.FEMALE, }.RandomItem(World.Rnd)));
                        var citizenRole = (AbstractCitizenRole)Activator.CreateInstance(role.GetType());
                        citizenRole.SetBuilding(citizen, building);
                        citizen.AddRole(citizenRole);
                        m_citizens.Add(citizen);
                    }

                    buildings.Add(building);
                }
            }

            while (buildings.Count < allRooms.Count)
            {
                foreach (var abuilding in availableBuildings)
                {
                    var count = buildings.Count(_building => _building.GetType() == abuilding.GetType());
                    if (count >= abuilding.MaxCountInCity)
                    {
                        continue;
                    }

                    var building = (Building)Activator.CreateInstance(abuilding.GetType(), this);
                    var role     = availableRoles.FirstOrDefault(_role => _role.BuildingType == building.BuildingType);
                    if (role != null)
                    {
                        Citizen citizen = null;
                        if (building.BuildingType == EBuilding.HOUSE)
                        {
                            citizen = m_citizens.FirstOrDefault(_citizen => _citizen.Roles.OfType <AbstractCitizenRole>().All(_role => _role.BuildingType != EBuilding.HOUSE));
                        }
                        if (citizen == null)
                        {
                            citizen = new Citizen(Layer, World.Rnd, surface.GetNextCitizenName(new[] { ESex.MALE, ESex.FEMALE, }.RandomItem(World.Rnd)));
                            m_citizens.Add(citizen);
                        }
                        var citizenRole = (AbstractCitizenRole)Activator.CreateInstance(role.GetType());
                        citizenRole.SetBuilding(citizen, building);
                        citizen.AddRole(citizenRole);
                    }
                    buildings.Add(building);
                }
            }

            buildings = buildings.OrderBy(_building => World.Rnd.Next()).ToList();

            foreach (var building in buildings)
            {
                var pair = allRooms.First(_pair => building.IsFit(_pair.Key));
                allRooms.Remove(pair.Key);
                building.SetRoom(pair.Key);
                m_buildings.Add(building);
            }
        }