void Start() { //params du menu xAmount = PlayerPrefs.GetInt("levelWidth"); zAmount = PlayerPrefs.GetInt("levelDepth"); tileAmount = xAmount * zAmount; playerAvatarsAmount = PlayerPrefs.GetInt("nbCharacters"); enemyAvatarsAmount = PlayerPrefs.GetInt("nbEnemies"); decorAmount = PlayerPrefs.GetInt("nbDecors"); //init levelParent = new GameObject().transform; levelParent.name = "LevelParent"; playerAvatarsParent = new GameObject().transform; playerAvatarsParent.name = "playerAvatarsParent"; enemiesParent = new GameObject().transform; enemiesParent.name = "enemiesParent"; outBoundaries = GameObject.Find("AABB"); outBoundaries.transform.localScale = (xAmount + 5) * tileSize * new Vector3(1, 1, 1); outBoundaries.transform.position = xAmount / 2 * tileSize * new Vector3(1, 0, 1); map = new MapGraph.Node <MapElement> [xAmount, zAmount]; graphStruct = new MapGraph(); indexX = 0; indexZ = 0; //generation du level StartCoroutine(GenerateLevel()); }
/** Add the edges between the current node and the ones with whom their triangle shares an edge **/ public static void AddEdges(MapGraph.Node node, Vector3 a, Vector3 b, Vector3 c) { for (int i = 0; i < nodes.Count; i++) { int n = 3 * i; //if a triangle share 2 points, they share an edge bool p1 = false, p2 = false, p3 = false; /*p1 = V3Equal(vertices[n],a) || V3Equal(vertices[n+1], a) || V3Equal(vertices[n+2], a); * p2 = V3Equal(vertices[n], b) || V3Equal(vertices[n + 1], b) || V3Equal(vertices[n + 2], b); * p3 = V3Equal(vertices[n], c) || V3Equal(vertices[n + 1], c) || V3Equal(vertices[n + 2], c);*/ p1 = (a == vertices[n] && (TestEdges(vertices[n], vertices[n + 1], a) || TestEdges(vertices[n], vertices[n + 2], a))) || (a == vertices[n + 1] && (TestEdges(vertices[n + 1], vertices[n], a) || TestEdges(vertices[n + 1], vertices[n + 2], a))) || (a == vertices[n + 2] && (TestEdges(vertices[n + 2], vertices[n], a) || TestEdges(vertices[n + 2], vertices[n + 1], a))); p2 = (b == vertices[n] && (TestEdges(vertices[n], vertices[n + 1], b) || TestEdges(vertices[n], vertices[n + 2], b))) || (b == vertices[n + 1] && (TestEdges(vertices[n + 1], vertices[n], b) || TestEdges(vertices[n + 1], vertices[n + 2], b))) || (b == vertices[n + 2] && (TestEdges(vertices[n + 2], vertices[n], b) || TestEdges(vertices[n + 2], vertices[n + 1], b))); p3 = (c == vertices[n] && (TestEdges(vertices[n], vertices[n + 1], c) || TestEdges(vertices[n], vertices[n + 2], c))) || (c == vertices[n + 1] && (TestEdges(vertices[n + 1], vertices[n], c) || TestEdges(vertices[n + 1], vertices[n + 2], c))) || (c == vertices[n + 2] && (TestEdges(vertices[n + 2], vertices[n], c) || TestEdges(vertices[n + 2], vertices[n + 1], c))); Debug.Assert(!(p1 && p2 && p3)); if ((p1 && p2) || (p2 && p3) || (p3 && p1)) { node.AddEdge(nodes[i]); nodes[i].AddEdge(node); } } }
//Génération d'un bloc de terrain void CreateTile(int TileIndex) { GameObject TileObject; MapElement mapElement; int heightnbr = 0; string material; TileObject = Instantiate(tiles[TileIndex], transform.position, transform.rotation) as GameObject; material = TileObject.GetComponent <Renderer>().sharedMaterial.name.ToUpper(); TileObject.transform.parent = levelParent; switch (material) { case "WALL": heightnbr = (int)height.black; break; case "BLUE": heightnbr = (int)height.blue; break; case "PLATFORM": heightnbr = (int)height.green; break; case "YELLOW": heightnbr = (int)height.yellow; break; } mapElement = new MapElement(TileObject.transform.position, heightnbr, tileSize); map[indexX, indexZ] = new MapGraph.Node <MapElement>(mapElement, 1); }
static void SpawnTreePrefab(PrefabSpawner spawner, MapGraph.Node node, MeshRenderer prefab, float scale) { MeshRenderer obj = spawner.SpawnPrefab( prefab, RandomOffset(node.centerPoint, mapGraph.center, settings.treePositionDeviation), RandomRotation(settings.treeRotationDeviation), RandomScaling(scale, settings.treeScaleDeviation) ); obj.transform.gameObject.AddComponent <MeshCollider>(); // Increment the number of environment occupants for the // current node. node.numEnvironmentOccupants++; }
static void SpawnSpecies(PrefabSpawner spawner, AnimalSpeciesSettings settings) { int initialPopulationSize = prng.Next( settings.minStartingPopulation, settings.maxStartingPopulation ); // Select an initial spawn point prior to starting the loop MapGraph.Node spawnPoint = SelectSpawnPoint(); for (int i = 0; i < initialPopulationSize; i++) { // Spawn a new instance of the prefab. MeshRenderer obj = spawner.SpawnPrefab(settings.prefab, spawnPoint.centerPoint - mapGraph.center, RandomRotation()); // Mark the node as occupied. spawnPoint.numAnimalOccupants++; // Randomize the neighbours so that we're not just iterating // through linearly. var neighbours = spawnPoint.GetNeighbourNodes() .OrderBy(_ => prng.Next()) .ToList(); // Attempt to select a random neighbouring node as the next spawn // point. int j; for (j = 0; j < neighbours.Count; j++) { if (allowedNodeTypes.Contains(neighbours[j].nodeType) && !neighbours[j].occupied) { spawnPoint = neighbours[j]; break; } } // If none of the neighbouring nodes are valid candidates, just // randomly select a new one instead. if (j >= neighbours.Count) { spawnPoint = SelectSpawnPoint(); } } }
private static void FloodFill( MapGraph.Node node, MapGraph.NodeType targetType, MapGraph.NodeType replacementType ) { if (targetType == replacementType || node.nodeType != targetType) { return; } node.nodeType = replacementType; foreach (var neighbor in node.GetNeighbourNodes()) { FloodFill(neighbor, targetType, replacementType); } }
public static void AddNode(Vector3 a, Vector3 b, Vector3 c) { Vector3 barycenter = (a + b + c) / 3f; MapGraph.Node node = new MapGraph.Node(barycenter); //AddEdges(node, a, b, c); float area = Mathf.Abs(a.x * (b.z - c.z) + b.x * (a.z - c.z) + c.x * (a.z - b.z)); node.area = area; vertices.Add(a); vertices.Add(b); vertices.Add(c); nodes.Add(node); }
/** * Remove the node corresponging to the id and fusion it with one of its neighbors **/ public static void RemoveNode(int i) { //New Node MapGraph.Node fusion = nodes[i].neighbors.Min; if (fusion is null) { //ignore for the moment foreach (MapGraph.Node nei in nodes[i].neighbors) { nei.neighbors.Remove(nodes[i]); } nodes.RemoveAt(i); return; } //Add the neighbors of the deleted node to the new node and deleting node from neighbors fusion.neighbors.Remove(nodes[i]); foreach (MapGraph.Node nei in nodes[i].neighbors) { if (nei == fusion) { continue; } fusion.neighbors.Add(nei); nei.neighbors.Remove(nodes[i]); } Vector3 newCenter = (nodes[i].center + fusion.center) / 2.0f;; float newArea = nodes[i].area + fusion.area; fusion.center = newCenter; fusion.area = newArea; nodes.RemoveAt(i); }