private MainViewModel BuildMainDataContext() { var width = Properties.Settings.Default.EcosystemWidth; var height = Properties.Settings.Default.EcosystemHeight; // the event aggregator might be used by view models to inform of changes var eventaggregator = new EventAggregator(); var habitats = new Habitat[width, height]; var habitatViewModels = new List<List<HabitatViewModel>>(); for (var x = 0; x < width; x++) { habitatViewModels.Add(new List<HabitatViewModel>()); for (var y = 0; y < height; y++) { // initially set each habitat to have an unknown environment and no organism var environment = new Environment(Terrain.Unknown, false); var environmentViewModel = new EnvironmentViewModel(environment, eventaggregator); var organismViewModel = new OrganismViewModel(null, eventaggregator); var habitat = new Habitat(environment, null); var habitatViewModel = new HabitatViewModel(habitat, environmentViewModel, organismViewModel, eventaggregator); habitats[x, y] = habitat; habitatViewModels[x].Add(habitatViewModel); } } var ecosystem = new Ecosystem(habitats, new Dictionary<Organism, Habitat>()); var ecosystemViewModel = new EcosystemViewModel(ecosystem, habitatViewModels, eventaggregator); this.InitialiseTerrain(ecosystem); var initialOrganismCoordinates = this.InitialiseOrganisms(ecosystem); // TODO: do this in InitialiseOrganisms // boshed together so the organisms are visible before ecosystem is switched on the first time foreach (var organismCoordinate in initialOrganismCoordinates) { habitatViewModels[organismCoordinate.X][organismCoordinate.Y].RefreshOrganismViewModel(); } // TODO: setting up the summary view after organisms initialised... how to handle this nicely var organismViewModels = habitatViewModels.SelectMany(habitatViewModel => habitatViewModel) .Select(habitatViewModel => habitatViewModel.OrganismViewModel) .Where(organismViewModel => organismViewModel.HasOrganism).ToList(); var organismSummaryViewModel = new OrganismSummaryViewModel(ecosystem, organismViewModels, eventaggregator); var main = new Main(ecosystem); var mainViewModel = new MainViewModel(main, ecosystemViewModel, organismSummaryViewModel, eventaggregator); return mainViewModel; }
private static Habitat[,] GenerateBaseHabitats(int width, int height) { var habitats = new Habitat[width,height]; for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var environment = new Environment(Terrain.Unknown, false); habitats[x, y] = new Habitat(environment, null); } } return habitats; }