public static NatureObjectScript SpawnNatureObject(GameNatureObject no) { GameObject obj = Instantiate(no.GetModelToSpawn(), no.GetPosition(), no.GetRotation(), Instance.plantsParent); NatureObjectScript nos = obj.AddComponent <NatureObjectScript>(); nos.tag = "NatureObject"; nos.SetNatureObject(no); nature.Add(nos); for (int dx = 0; dx < nos.GridWidth; dx++) { for (int dy = 0; dy < nos.GridHeight; dy++) { if (!Grid.ValidNode(nos.GridX + dx, nos.GridY + dy)) { continue; } Node n = Grid.GetNode(nos.GridX + dx, nos.GridY + dy); n.SetNodeObject(obj.transform); n.objectWalkable = nos.Walkable; } } return(nos); }
public static void SaveNature() { myGameState.natureData = NatureObjectScript.AllGameNatureObjects(); }
public static void SpawnRandomPosNatureObject(NatureObject baseNo, int rndSize) { Terrain terrain = Terrain.activeTerrain; bool invalid = false; int x = 0; int z = 0; int count = 0; if (baseNo.type == NatureObjectType.Reed) { List <Node> sn = new List <Node>(shore); if (sn.Count == 0) { return; } Node rsn = sn[Random.Range(0, sn.Count)]; while (rsn.nodeObject != null && (++count) < 100) { rsn = sn[Random.Range(0, sn.Count)]; } if (count == 100) { return; } x = rsn.gridX; z = rsn.gridY; } else { x = UnityEngine.Random.Range(0, Grid.WIDTH); z = UnityEngine.Random.Range(0, Grid.HEIGHT); while ((Grid.GetNode(x, z).IsOccupied() || Grid.GetNode(x, z).IsWater() || (Mathf.Abs(Grid.SpawnX - x) < Mathf.Max(5, baseNo.minDistToCenter) && Mathf.Abs(Grid.SpawnY - z) < Mathf.Max(5, baseNo.minDistToCenter))) && (++count) < 100) { x = UnityEngine.Random.Range(0, Grid.WIDTH); z = UnityEngine.Random.Range(0, Grid.HEIGHT); } if (count == 100) { return; } for (int dx = 0; dx < 3; dx++) { if (invalid) { continue; } for (int dy = 0; dy < 3; dy++) { if (invalid) { continue; } if (!Grid.ValidNode(x + dx, z + dy)) { invalid = true; } else if (Grid.GetNode(x + dx, z + dy).IsOccupied()) { invalid = true; } } } } if (invalid) { return; } GameNatureObject toSpawn = new GameNatureObject(baseNo, x, z); toSpawn.SetPosition(Grid.ToWorld(x + baseNo.gridWidth / 2, z + baseNo.gridHeight / 2)); toSpawn.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0)); NatureObjectScript nos = SpawnNatureObject(toSpawn); nos.SetRandSize(rndSize); }