//Instantiates a number of lines GO's according to how many lines are requested; void CreateLineGameObjects() { for (int i = 0; i < numberOfLines; i++) { //Setting color of line equal to its extremity nodes and number each line; GameObject lineObject = Instantiate(linePrefab); LineGenerator lineGen = lineObject.GetComponent <LineGenerator>(); lineGen.SetupLine(specialPositions[i * 2], specialPositions[i * 2 + 1], i, colors[i]); lines.Add(lineGen); //Store each instantiated line on List 'lines'; } }
// Generates a gridSize x gridSize grid centered on the screen, and adds N-times two points to be connected private void GenerateGrid(int level) { //Create all GameObjects that will compose a level var GO = new GameObject(); GO.name = "Level " + level; var Grid = new GameObject(); Grid.name = "Grid"; var Lines = new GameObject(); Lines.name = "Lines"; LevelLineManager levelLineManager = Grid.AddComponent <LevelLineManager>(); levelLineManager.lines = new List <LineGenerator>(); Grid.transform.parent = GO.transform; Lines.transform.parent = GO.transform; levelLineManager.allNodes = new GameObject[gridSize * gridSize]; specialPositions = new List <int>(); //Add and setup lines for (int i = 0; i < numberOfLines; i++) { var line = Instantiate(linePrefab, Lines.transform); LineGenerator lineGen = line.GetComponent <LineGenerator>(); lineGen.SetupLine((int)extremity[i].x, (int)extremity[i].y, i, colors[i]); levelLineManager.lines.Add(lineGen); specialPositions.Add(lineGen.lineExtremities[0]); specialPositions.Add(lineGen.lineExtremities[1]); } levelLineManager.gridLayer = gridLayer; //DistanceFactor makes the distance between points smaller for larger grids; distanceFactor = 4.5f / gridSize; //Generate Grid based on gridSize and number of lines for (int x = 0; x < gridSize; x++) { for (int y = 0; y < gridSize; y++) { GameObject gridSquare, node; int currentPos = x * gridSize + y; //Array position (between 0 and gridSize*gridSize - 1) //Actual world position of nodes / square grids float xWorldPos = (x - (gridSize - 1) / 2.0f) * distanceFactor; float yWorldPos = (y - (gridSize - 1) / 2.0f) * distanceFactor; gridSquare = Instantiate(gridUnit, new Vector3(xWorldPos, yWorldPos, 0), Quaternion.identity); if (specialPositions.Contains(currentPos)) { //Color of special nodes. Nodes of the same color are paired: [0,1] - Color1, [2,3] - Color2, etc.... var listIndex = specialPositions.FindIndex(i => i == currentPos); Color nodeColor = colors[(int)Mathf.Floor((float)listIndex / 2)]; node = Instantiate(specialNode, new Vector3(xWorldPos, yWorldPos, 0), Quaternion.identity); node.GetComponent <SpriteRenderer>().color = nodeColor; } else { node = Instantiate(standardNode, new Vector3(xWorldPos, yWorldPos, 0), Quaternion.identity); } //Parent grid to this GameObject and parent individual node to each grid space; gridSquare.transform.parent = Grid.transform; node.transform.parent = gridSquare.transform; gridSquare.transform.localScale *= 5.0f / gridSize; gridSquare.name = (currentPos).ToString(); //Relation between array position with world (x,y) position stored in centerPositions levelLineManager.allNodes[currentPos] = node; } } }