void setDoors() { bool[] used = { false, false, false, false, false, false }; for (int i = 0; i < doors.Length; i++) { Color NewColor = Color.black; DoorProperties door = doors[i].GetComponent <DoorProperties>(); string color = "black"; int caseSwitch = Random.Range(1, 6); while (used[caseSwitch] && i < 5) //if i>6 we have more doors than colors and this loop would go forever { caseSwitch = Random.Range(1, 6); } used[caseSwitch] = true; switch (caseSwitch) { case 1: color = "red"; NewColor = Color.red; //new Color(0f, 0f, 0f, 1f) break; case 2: color = "yellow"; NewColor = Color.yellow; break; case 3: color = "green"; NewColor = Color.green; break; case 4: color = "blue"; NewColor = Color.blue; break; case 5: color = "pink"; NewColor = pink; break; case 6: color = "purple"; NewColor = Color.magenta; break; } door.color = color; doors[i].GetComponent <SpriteRenderer>().color = NewColor; } }
void setOneDoor() { string color = "black"; Color NewColor = Color.black; DoorProperties door = doors[0].GetComponent <DoorProperties>(); switch (currentColor) { case "red": color = "red"; NewColor = Color.red; //new Color(0f, 0f, 0f, 1f) break; case "yellow": color = "yellow"; NewColor = Color.yellow; break; case "green": color = "green"; NewColor = Color.green; break; case "blue": color = "blue"; NewColor = Color.blue; break; case "pink": color = "pink"; NewColor = pink; break; case "purple": color = "purple"; NewColor = Color.magenta; break; } door.color = color; doors[0].GetComponent <SpriteRenderer>().color = NewColor; }
//Builds wall segments of the room based on already-spawned doors' positions. void BuildWalls() { platformPrefab = Resources.Load ("Platform"); if(wallTop) { DoorProperties[] orderedDoors = new DoorProperties[0]; //First, gets an array of all walls on this side and sorts them by offset. int nextPos = 0; for(int counter = 0; counter < doors.Length; counter++) { if(doors[counter].top) { System.Array.Resize(ref orderedDoors, orderedDoors.Length+1); orderedDoors[nextPos] = doors[counter]; nextPos++; } } for(int counter = 0; counter < orderedDoors.Length; counter++) //Brute force (suboptimal but works). Does the full cycle as many times as there are entries in the array, to ensure full completion. { for(int counter1 = 0; counter1 < orderedDoors.Length; counter1++) //Cycles through array. { for(int counter2 = counter1+1; counter2 < orderedDoors.Length; counter2++) //Iterates through remainder of array, comparing to current entry of cycle. { if(orderedDoors[counter1].offset > orderedDoors[counter2].offset) //If any other entries are found with a smaller offset, the current entry is swapped with them. { DoorProperties placeholder = orderedDoors[counter2]; orderedDoors[counter2] = orderedDoors[counter1]; orderedDoors[counter1] = placeholder; } } } } //Second, spawns walls for before first door and after last door. if(orderedDoors.Length == 0) //If no doors, simply places the full wall segment. { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Top Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2(0, roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y); wall.transform.localScale = new Vector3 (roomBounds.size.x+2, 1, 1); } else { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Top-Left Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((orderedDoors[0].offset-4f) - (((orderedDoors[0].offset-4f) + (roomBounds.extents.x+1))/2), roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y); wall.transform.localScale = new Vector3((roomBounds.extents.x+1) + (orderedDoors[0].offset-4f), 1, 1); wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Top-Right Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((roomBounds.extents.x-1) - (((roomBounds.extents.x-3) - (orderedDoors[orderedDoors.Length-1].offset+4))/2), roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y); wall.transform.localScale = new Vector3((roomBounds.extents.x+1) - (orderedDoors[orderedDoors.Length-1].offset+4), 1, 1); } //Third, spawns in the spaces between doors if there are any. if(orderedDoors.Length > 1) { for(int counter = 0; counter < orderedDoors.Length-1; counter++) { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Top-Mid Platform"; wall.transform.SetParent(room.transform); //Sets position of wall segment by taking the next door's offset and subtracting half the distance between it and the previous door. float offsetDifference = ((orderedDoors[counter+1].offset) - (orderedDoors[counter].offset)); wall.transform.localPosition = new Vector2(orderedDoors[counter+1].offset - offsetDifference/2, roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y); //Sets X scale of wall segment by taking the distance between the doors' sprites. float boundsDifference = offsetDifference - 8f; //Length 8 taken from sprite. wall.transform.localScale = new Vector3(boundsDifference, 1, 1); } } } if(wallBottom) { DoorProperties[] orderedDoors = new DoorProperties[0]; //First, gets an array of all walls on this side and sorts them by offset. int nextPos = 0; for(int counter = 0; counter < doors.Length; counter++) { if(doors[counter].bottom) { System.Array.Resize(ref orderedDoors, orderedDoors.Length+1); orderedDoors[nextPos] = doors[counter]; nextPos++; } } for(int counter = 0; counter < orderedDoors.Length; counter++) //Brute force (suboptimal but works). Does the full cycle as many times as there are entries in the array, to ensure full completion. { for(int counter1 = 0; counter1 < orderedDoors.Length; counter1++) //Cycles through array. { for(int counter2 = counter1+1; counter2 < orderedDoors.Length; counter2++) //Iterates through remainder of array, comparing to current entry of cycle. { if(orderedDoors[counter1].offset > orderedDoors[counter2].offset) //If any other entries are found with a smaller offset, the current entry is swapped with them. { DoorProperties placeholder = orderedDoors[counter2]; orderedDoors[counter2] = orderedDoors[counter1]; orderedDoors[counter1] = placeholder; } } } } //Second, spawns walls for before first door and after last door. if(orderedDoors.Length == 0) //If no doors, simply places the full wall segment. { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Bottom Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2(0, -(roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y)); wall.transform.localScale = new Vector3 (roomBounds.size.x+2, 1, 1); } else { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Bottom-Left Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((orderedDoors[0].offset-4f) - (((orderedDoors[0].offset-4f) + (roomBounds.extents.x+1))/2), -(roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y)); wall.transform.localScale = new Vector3((roomBounds.extents.x+1) + (orderedDoors[0].offset-4f), 1, 1); wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Bottom-Right Platform"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((roomBounds.extents.x-1) - (((roomBounds.extents.x-3) - (orderedDoors[orderedDoors.Length-1].offset+4))/2), -(roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y)); wall.transform.localScale = new Vector3((roomBounds.extents.x+1) - (orderedDoors[orderedDoors.Length-1].offset+4), 1, 1); } //Third, spawns in the spaces between doors if there are any. if(orderedDoors.Length > 1) { for(int counter = 0; counter < orderedDoors.Length-1; counter++) { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Bottom-Mid Platform"; wall.transform.SetParent(room.transform); //Sets position of wall segment by taking the next door's offset and subtracting half the distance between it and the previous door. float offsetDifference = ((orderedDoors[counter+1].offset) - (orderedDoors[counter].offset)); wall.transform.localPosition = new Vector2(orderedDoors[counter+1].offset - offsetDifference/2, -(roomBounds.extents.y + wall.GetComponent<Collider2D>().bounds.extents.y)); //Sets X scale of wall segment by taking the distance between the doors' sprites. float boundsDifference = offsetDifference - 8f; //Length 8 taken from sprite. wall.transform.localScale = new Vector3(boundsDifference, 1, 1); } } } if(wallLeft) { DoorProperties[] orderedDoors = new DoorProperties[0]; //First, gets an array of all walls on this side and sorts them by offset. int nextPos = 0; for(int counter = 0; counter < doors.Length; counter++) { if(doors[counter].left) { System.Array.Resize(ref orderedDoors, orderedDoors.Length+1); orderedDoors[nextPos] = doors[counter]; nextPos++; } } for(int counter = 0; counter < orderedDoors.Length; counter++) //Brute force (suboptimal but works). Does the full cycle as many times as there are entries in the array, to ensure full completion. { for(int counter1 = 0; counter1 < orderedDoors.Length; counter1++) //Cycles through array. { for(int counter2 = counter1+1; counter2 < orderedDoors.Length; counter2++) //Iterates through remainder of array, comparing to current entry of cycle. { if(orderedDoors[counter1].offset > orderedDoors[counter2].offset) //If any other entries are found with a smaller offset, the current entry is swapped with them. { DoorProperties placeholder = orderedDoors[counter2]; orderedDoors[counter2] = orderedDoors[counter1]; orderedDoors[counter1] = placeholder; } } } } //Second, spawns walls for before first door and after last door. if(orderedDoors.Length == 0) //If no doors, simply places the full wall segment. { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Left Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2(-(roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x), 0); wall.transform.localScale = new Vector3 (1, roomBounds.size.y+2, 1); } else { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Left-Bottom Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2( -(roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x),(orderedDoors[0].offset-4f) - (((orderedDoors[0].offset-4f) + (roomBounds.extents.y+1))/2)); wall.transform.localScale = new Vector3(1, (roomBounds.extents.y+1) + (orderedDoors[0].offset-4f), 1); wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Left-Top Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2(-(roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x),(roomBounds.extents.y-1) - (((roomBounds.extents.y-3) - (orderedDoors[orderedDoors.Length-1].offset+4))/2)); wall.transform.localScale = new Vector3(1, (roomBounds.extents.y+1) - (orderedDoors[orderedDoors.Length-1].offset+4), 1); } //Third, spawns in the spaces between doors if there are any. if(orderedDoors.Length > 1) { for(int counter = 0; counter < orderedDoors.Length-1; counter++) { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Left-Mid Wall"; wall.transform.SetParent(room.transform); //Sets position of wall segment by taking the next door's offset and subtracting half the distance between it and the previous door. float offsetDifference = ((orderedDoors[counter+1].offset) - (orderedDoors[counter].offset)); wall.transform.localPosition = new Vector2(-(roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x), orderedDoors[counter+1].offset - offsetDifference/2); //Sets X scale of wall segment by taking the distance between the doors' sprites. float boundsDifference = offsetDifference - 8f; //Length 8 taken from sprite. wall.transform.localScale = new Vector3(1, boundsDifference, 1); } } } if(wallRight) { DoorProperties[] orderedDoors = new DoorProperties[0]; //First, gets an array of all walls on this side and sorts them by offset. int nextPos = 0; for(int counter = 0; counter < doors.Length; counter++) { if(doors[counter].right) { System.Array.Resize(ref orderedDoors, orderedDoors.Length+1); orderedDoors[nextPos] = doors[counter]; nextPos++; } } for(int counter = 0; counter < orderedDoors.Length; counter++) //Brute force (suboptimal but works). Does the full cycle as many times as there are entries in the array, to ensure full completion. { for(int counter1 = 0; counter1 < orderedDoors.Length; counter1++) //Cycles through array. { for(int counter2 = counter1+1; counter2 < orderedDoors.Length; counter2++) //Iterates through remainder of array, comparing to current entry of cycle. { if(orderedDoors[counter1].offset > orderedDoors[counter2].offset) //If any other entries are found with a smaller offset, the current entry is swapped with them. { DoorProperties placeholder = orderedDoors[counter2]; orderedDoors[counter2] = orderedDoors[counter1]; orderedDoors[counter1] = placeholder; } } } } //Second, spawns walls for before first door and after last door. if(orderedDoors.Length == 0) //If no doors, simply places the full wall segment. { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Right Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x), 0); wall.transform.localScale = new Vector3 (1, roomBounds.size.y+2, 1); } else { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Right-Bottom Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x),(orderedDoors[0].offset-4f) - (((orderedDoors[0].offset-4f) + (roomBounds.extents.y+1))/2)); wall.transform.localScale = new Vector3(1, (roomBounds.extents.y+1) + (orderedDoors[0].offset-4f), 1); wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Right-Top Wall"; wall.transform.SetParent(room.transform); wall.transform.localPosition = new Vector2((roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x),(roomBounds.extents.y-1) - (((roomBounds.extents.y-3) - (orderedDoors[orderedDoors.Length-1].offset+4))/2)); wall.transform.localScale = new Vector3(1, (roomBounds.extents.y+1) - (orderedDoors[orderedDoors.Length-1].offset+4), 1); } //Third, spawns in the spaces between doors if there are any. if(orderedDoors.Length > 1) { for(int counter = 0; counter < orderedDoors.Length-1; counter++) { GameObject wall = (GameObject)PrefabUtility.InstantiatePrefab(platformPrefab); wall.name = "Right-Mid Wall"; wall.transform.SetParent(room.transform); //Sets position of wall segment by taking the next door's offset and subtracting half the distance between it and the previous door. float offsetDifference = ((orderedDoors[counter+1].offset) - (orderedDoors[counter].offset)); wall.transform.localPosition = new Vector2((roomBounds.extents.x + wall.GetComponent<Collider2D>().bounds.extents.x), orderedDoors[counter+1].offset - offsetDifference/2); //Sets X scale of wall segment by taking the distance between the doors' sprites. float boundsDifference = offsetDifference - 8f; //Length 8 taken from sprite. wall.transform.localScale = new Vector3(1, boundsDifference, 1); } } } }