void Spawn(SpawnableScenery item, Rect spawnLocation) { GameObject itemSpawned = Instantiate(item.SceneryObject, parent, true); itemSpawned.transform.localScale = item.RandomScale(); itemSpawned.transform.eulerAngles = item.RandomRotation(); float itemLength = item.GetLength() * 1.25f; bool found = false; Rect rect = new Rect(randomPosition(spawnLocation), new Vector2(itemLength, itemLength)); while (!found) { if (!TestColliding(rect)) { found = true; } else { rect.position = randomPosition(spawnLocation); } } spawned.Add(rect); itemSpawned.transform.position = new Vector3(rect.x, height, rect.y); if (item.SpawnList != UnitTypes.NONE) { switch (item.SpawnList) { case UnitTypes.Scenery: GlobalGame.UnitManager.addUnit(itemSpawned.GetComponent <Building>(), item.SpawnList); break; case UnitTypes.Tree: GlobalGame.UnitManager.addUnit(itemSpawned.GetComponent <Building>(), item.SpawnList); break; default: GlobalGame.UnitManager.addUnit(itemSpawned, item.SpawnList); break; } } }
Rect randomLocation(SpawnableScenery scenery) { SpawnableLocation finalLoc = SpawnLocations[SpawnLocations.Count - 1]; bool found = false; foreach (SpawnableLocation loc in SpawnLocations) { if (loc.SpawnCounts[scenery] > 0) { loc.SpawnCounts[scenery] -= 1; finalLoc = loc; found = true; break; } } if (!found) { scenery.CurrentSpawnCount = -1; } return(finalLoc.AreaRect); }