public static bool CanPlace(City city, IntStruct indexes, Projection projection) { int x = indexes.Item1; int y = indexes.Item2; if (x - projection.centerX < 0 || y - projection.centerY < 0 || x - projection.centerX + projection.matrix.GetLength(0) - 1 >= city.Grid.GetLength(0) || y - projection.centerY + projection.matrix.GetLength(1) - 1 >= city.Grid.GetLength(1)) { return(false); } for (int i = x - projection.centerX; i < x - projection.centerX + projection.matrix.GetLength(0); i++) { for (int j = y - projection.centerY; j < y - projection.centerY + projection.matrix.GetLength(1); j++) { if (projection.matrix[i - x + projection.centerX, j - y + projection.centerY] != CellState.Empty && city.Grid[i, j] != null) { return(false); } } } return(true); }
public void Awake() { GridProjection = new Projection(new CellState[, ] { { CellState.Center } }); Center = new IntStruct(0, 0); }
public override void Initialize(City city, IntStruct indexes) { base.Initialize(city, indexes); for (int i = 0; i < 3 && city.FreeCitizens.Count > 0; i++) { Citizens.Add(city.FreeCitizens.Dequeue()); } }
public void Awake() { satisfactionName = "CarPark"; Radius = 20; GridProjection = new Projection(new CellState[, ] { { CellState.Fill, CellState.Fill, CellState.Fill }, { CellState.Fill, CellState.Center, CellState.Fill }, { CellState.Fill, CellState.Fill, CellState.Fill } }); Center = new IntStruct(1, 1); }
public void Awake() { satisfactionName = "ScienceCenter"; Radius = 16; GridProjection = new Projection(new CellState[, ] { { CellState.Fill, CellState.Fill, CellState.Empty }, { CellState.Fill, CellState.Center, CellState.Fill }, { CellState.Empty, CellState.Fill, CellState.Fill } }); Center = new IntStruct(1, 1); }
private bool BuildRandomBuilding(System.Random random, IntStruct index) { bool result = false; switch (random.Next(4)) { case 0: if (CanPlace(this, index, CarPark.defaultProjection)) { Instantiate(PrefabManager.Manager.carParkPrefab) .GetComponent <CarPark>().Initialize(this, index); result = true; } break; case 1: if (CanPlace(this, index, Park.defaultProjection)) { Instantiate(PrefabManager.Manager.parkPrefab) .GetComponent <Park>().Initialize(this, index); result = true; } break; case 2: if (CanPlace(this, index, ScienceCenter.defaultProjection)) { Instantiate(PrefabManager.Manager.scienceCenterPrefab) .GetComponent <ScienceCenter>().Initialize(this, index); result = true; } break; case 3: if (CanPlace(this, index, Shop.defaultProjection)) { Instantiate(PrefabManager.Manager.shopPrefab) .GetComponent <Shop>().Initialize(this, index); result = true; } break; } return(result); }
public virtual void Initialize(City city, IntStruct indexes) { cityParent = city; Center = indexes; cityParent.Buildings.Add(this); try { for (int i = indexes.Item1 - Center.Item1; i < indexes.Item1 - Center.Item1 + GridProjection.matrix.GetLength(0); i++) { for (int j = indexes.Item2 - Center.Item2; j < indexes.Item2 - Center.Item2 + GridProjection.matrix.GetLength(1); j++) { if (GridProjection.matrix[i - indexes.Item1 + Center.Item1, j - indexes.Item2 + Center.Item2] != CellState.Empty) { cityParent.Grid[i, j] = this; } } } } catch (Exception) { Debug.Log("IndexOutOfRangeException in Build()"); } }
public override void Initialize(City city, IntStruct indexes) { base.Initialize(city, indexes); }
public void Awake() { GridProjection = defaultProjection; Center = new IntStruct(0, 0); }
public void ProceduralGenerating(string seed) { DateTime milli = DateTime.Now; foreach (var item in Buildings.Select(building => building.gameObject)) { Destroy(item); } Grid = new Building[GridSideSize, GridSideSize]; Buildings = new List <Building>(); Debug.Log($"Destroy: {(DateTime.Now - milli).TotalMilliseconds}"); milli = DateTime.Now; System.Random random = new System.Random(seed.GetHashCode()); int spaceCounter = 0; List <int> xSpaces = new List <int>(); for (int i = 0; i < GridSideSize - 3; i++, spaceCounter++) { if (spaceCounter + random.Next(8) > 10 + random.Next(3)) { for (int j = 0; j <= GridSideSize; j++) { Debug.Log(PrefabManager.Manager == null); Debug.Log(PrefabManager.Manager.roadPrefab == null); IntStruct indexes = new IntStruct(i, j); if (CanPlace(this, indexes, Road.defaultProjection)) { Instantiate(PrefabManager.Manager.roadPrefab) .GetComponent <Road>().Initialize(this, indexes); } //Road road = city.AddComponent<Road>(); //Buildings.Add(road); //IntStruct tuple = new IntStruct(i, j); //if (CanBuild(tuple, road)) // road.Build("", this, tuple, null); } xSpaces.Add(spaceCounter); spaceCounter = -1; } } xSpaces.Add(spaceCounter + 2); spaceCounter = 0; List <int> zSpaces = new List <int>(); for (int i = 0; i < GridSideSize - 3; i++, spaceCounter++) { if (spaceCounter + random.Next(7) > 10 + random.Next(3)) { for (int j = 0; j <= GridSideSize; j++) { IntStruct indexes = new IntStruct(i, j); if (CanPlace(this, indexes, Road.defaultProjection)) { Instantiate(PrefabManager.Manager.roadPrefab) .GetComponent <Road>().Initialize(this, indexes); } } zSpaces.Add(spaceCounter); spaceCounter = -1; } } zSpaces.Add(spaceCounter + 2); for (int i = 0; i < Grid.GetLength(0); i++) { for (int j = 0; j < Grid.GetLength(1); j++) { if (i == 0 || Grid[i - 1, j] is Road || i + 1 == Grid.GetLength(0) || Grid[i + 1, j] is Road || j == 0 || Grid[i, j - 1] is Road || j + 1 == Grid.GetLength(1) || Grid[i, j + 1] is Road) { IntStruct indexes = new IntStruct(i, j); if (CanPlace(this, indexes, Sidewalk.defaultProjection)) { Instantiate(PrefabManager.Manager.sidewalkPrefab) .GetComponent <Sidewalk>().Initialize(this, indexes); } } } } Graph <Sidewalk> sidewalksGraph = new Graph <Sidewalk>(); for (int i = 0; i < Grid.GetLength(0); i++) { for (int j = 0; j < Grid.GetLength(1); j++) { if (Grid[i, j] is Sidewalk sidewalk) { var newNode = sidewalksGraph.AddNode(sidewalk); if (i > 0 && Grid[i - 1, j] is Sidewalk sidewalkLeft) { var nodeLeft = sidewalksGraph.Nodes.Where(node => node.Item == sidewalkLeft).FirstOrDefault(); if (nodeLeft != null) { sidewalksGraph.Bind(newNode, nodeLeft); } } if (j > 0 && Grid[i, j - 1] is Sidewalk sidewalkUp) { var nodeUp = sidewalksGraph.Nodes.Where(node => node.Item == sidewalkUp).FirstOrDefault(); if (nodeUp != null) { sidewalksGraph.Bind(newNode, nodeUp); } } } } } foreach (var node in sidewalksGraph.Nodes.OrderBy(node => random.Next()).Take(random.Next(10, 20))) { //Citizen generating } Debug.Log($"Generating roads: {(DateTime.Now - milli).TotalMilliseconds}"); List <IntStruct[, ]> districts = new List <IntStruct[, ]>(); int xSum = 0, zSum = 0; for (int x = 0; x < xSpaces.Count; x++) { for (int z = 0; z < zSpaces.Count; z++) { IntStruct[,] district = new IntStruct[xSpaces[x], zSpaces[z]]; for (int i = 0; i < xSpaces[x]; i++) { for (int j = 0; j < zSpaces[z]; j++) { district[i, j] = new IntStruct(i + xSum + x, j + zSum + z); } } zSum += zSpaces[z]; districts.Add(district); } zSum = 0; xSum += xSpaces[x]; } foreach (var district in districts.OrderBy(a => random.Next())) { foreach (var index in district) { if (BuildRandomBuilding(random, index)) { break; } } } int counter = 0; do { var index = new IntStruct(random.Next(GridSideSize), random.Next(GridSideSize)); if (CanPlace(this, index, Home.defaultProjection)) { Instantiate(PrefabManager.Manager.homePrefab) .GetComponent <Home>().Initialize(this, index); } counter++; }while (counter + random.Next(GridSideSize / 2) < GridSideSize * 2); }