# TWA. Colouring mechanic ## Code ## Puzzle A (before Json fully integrated) ```cs using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using Unity.Collections; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Puzzle_A : MonoBehaviour { public TMP_InputField nameInputField; public string nextSceneName; public string currentAtlasID; // for Json public GameObject a_Atlas; public JsonObj_Atlas atlasJsonScript; private int n = 0; // for atlasID // FLAGS // To deactivate buttons during a victory animation [SerializeField] private List<Button> colorantButtonsList; // To check if at least one button is clicked in each pair in order to deactivate a button after it's clicked, // and reactivate it when a button paired with it is clicked // This way notes on the same button don't count twice and ausio sources don't activate twice (not needed in some other puzzles) private bool[] buttonPairFlags = new bool[7]; // To identify how many times the atlas has already been coloured private bool wasntColoured = true; private bool colouredOnce = false; private bool colouredTwice = false; // Flags for paired buttons with sound; // Numbers correlate with the numbers of buttons private bool swamp2 = false; // 1 note private bool steppe3 = false; // 2 notes private bool lake4 = false; // 1 note private bool swamp5 = false; // 1 note private bool pines7 = false; // 1 note private bool lava11 = false; // 2 notes private bool sea14 = false; // 1 note // VICTORY CONDITIONS private int notesPlayed = 0; // To check that a note's playing but no more than 3 notes private bool atlasColoured = false; // Is true if the atlas is fully coloured private Victory victoryScript; // To access atlasCorrect bool (the victory condition) // SOUND public AudioSource swampSourceA; public AudioSource steppeSource; public AudioSource lakeSource; public AudioSource swampSourceB; public AudioSource pinesSource; public AudioSource lavaSource; // next to a sources for the same button (button #14, Sea) public AudioSource seaGullsSource; public AudioSource seaWavesSource; private void Start() { atlasJsonScript = a_Atlas.GetComponent<JsonObj_Atlas>(); currentAtlasID = "Atlas_A" + n; n++; victoryScript = gameObject.GetComponent<Victory>(); // to get access to atlasCorrect bool in CheckVictory(); if (Names.A_atlasNames.Count == 0) wasntColoured = true; else if (Names.A_atlasNames.Count == 1) { wasntColoured = false; colouredOnce = true; } else if (Names.A_atlasNames.Count == 2) { wasntColoured = false; colouredOnce = false; colouredTwice = true; } } // Controls interactability of button (the player cannot click a button twice in a row), // turns off sound on paired button (if any), // calculates how much notes are being played, // and sends color data to AtlasData (allows the instantiation of a beetle with identical colours at the Orangery scene) public void OnButtonClicked(int index) { // To check if the atlas is fully coloured int pairIndex = index / 2; buttonPairFlags[pairIndex] = true; // To calculated the index of a just pressed button int pairedIndex = (index % 2 == 0) ? index + 1 : index - 1; // Switch the activity of buttons in the pair colorantButtonsList[index].interactable = false; colorantButtonsList[pairedIndex].interactable = true; switch (index) { case 0: if (wasntColoured) { AtlasData.sliceA1 = 0; atlasJsonScript.colourCode1 = 0; } //if (colouredOnce) // AtlasData.sliceA1_1 = 0; //if (colouredTwice) // AtlasData.sliceA1_2 = 0; // Could be in if-statement brackets too DeactivateSwampA(); // Deactivate the paired button effect (swamp) if (swamp2 == true) { swamp2 = false; notesPlayed--; } break; case 1: AtlasData.sliceA1 = 1; atlasJsonScript.colourCode1 = 1; // Activate the effect. No need to deactivate the paired silent colorant ActivateSwampA(); swamp2 = true; notesPlayed++; break; case 2: AtlasData.sliceA2 = 0; atlasJsonScript.colourCode2 = 0; ActivateSteppe(); // Deactivate the paired button effect (steppe) if (lake4 == true) { lake4 = false; notesPlayed--; } // Activate native effect steppe3 = true; notesPlayed += 2; break; case 3: AtlasData.sliceA2 = 1; atlasJsonScript.colourCode2 = 1; ActivateLake(); if (steppe3 == true) { steppe3 = false; notesPlayed -= 2; } lake4 = true; notesPlayed++; break; case 4: AtlasData.sliceA3 = 0; ActivateSwampB(); swamp5 = true; notesPlayed++; break; case 5: AtlasData.sliceA3 = 1; DeactivateSwampB(); if (swamp5 == true) { swamp5 = false; notesPlayed--; } break; case 6: AtlasData.sliceA4 = 0; ActivatePines(); pines7 = true; notesPlayed += 2; break; case 7: AtlasData.sliceA4 = 1; DeactivatePines(); if (pines7 == true) { pines7 = false; notesPlayed -= 2; } break; case 8: AtlasData.sliceA5 = 0; break; case 9: AtlasData.sliceA5 = 1; break; case 10: AtlasData.sliceA6 = 0; ActivateLava(); lava11 = true; notesPlayed += 2; break; case 11: AtlasData.sliceA6 = 1; DeactivateLava(); if (lava11 == true) { lava11 = false; notesPlayed -= 2; } break; case 12: AtlasData.sliceA7 = 0; DeactivateSea(); if (sea14 == true) { sea14 = false; notesPlayed -= 2; } break; case 13: AtlasData.sliceA7 = 1; ActivateSea(); sea14 = true; notesPlayed += 2; break; } } // SOUND private void DeactivateSwampA() // button #1 { if (swampSourceA.time > 0f) swampSourceA.Pause(); } private void ActivateSwampA() // button #2 { ActivateAudioSource(swampSourceA); } private void ActivateSteppe() // button #3 { if (lakeSource.time > 0f) { lakeSource.Pause(); ActivateAudioSource(steppeSource); } ActivateAudioSource(steppeSource); } private void ActivateLake() // button #4 { if (steppeSource.time > 0f) { steppeSource.Pause(); } ActivateAudioSource(lakeSource); } private void ActivateSwampB() // button #5 { ActivateAudioSource(swampSourceB); } private void DeactivateSwampB() // button #6 { if (swampSourceB.time > 0f) swampSourceB.Pause(); } private void ActivatePines() // button #7 { ActivateAudioSource(pinesSource); } private void DeactivatePines() // button #8 { if (pinesSource.time > 0f) pinesSource.Pause(); } private void ActivateLava() // button #11 { ActivateAudioSource(lavaSource); } private void DeactivateLava() // button #12 { if (lavaSource.time > 0f) lavaSource.Pause(); } private void DeactivateSea() // button #13 { if (seaGullsSource.time > 0f) seaGullsSource.Pause(); if (seaWavesSource.time > 0f) seaWavesSource.Pause(); } private void ActivateSea() // button #14 { ActivateAudioSource(seaGullsSource); ActivateAudioSource(seaWavesSource); } private void ActivateAudioSource(AudioSource source) { if (source.time > 0f) source.UnPause(); else source.Play(); } // VICTORY // Checks if the atlas is fully coloured // Then checks if the atlas is correctly coloured private void CheckVictory() { if (buttonPairFlags.All(flag => flag)) // All coloured? atlasColoured = true; if (atlasColoured && (4 > notesPlayed) && (notesPlayed > 0)) // Correctly coloured? { victoryScript.atlasCorrect = true; // Since we know that the victory condition is true, // we desable colorant buttons in this script // to avoid assigning them twice in the Inspector foreach (Button item in colorantButtonsList) item.interactable = false; } } public void OnNameInputButton() { // Sends the player's name to the Orangery scene string playerNameA = nameInputField.text; Names.A_atlasNames.Add(playerNameA); Debug.Log("Name added: " + playerNameA); // Open next atlas SceneManager.LoadScene(nextSceneName); } private void Update() { CheckVictory(); Debug.Log(quot;{notesPlayed} notes plays"); } } ``` ## OrangeryController ```cs using TMPro; using UnityEngine; using UnityEngine.SceneManagement; public class SceneContrOr : MonoBehaviour { // ATLASES // Prefabs public GameObject a_prefab; // Atlas A bug prefab public GameObject b_prefab; // Positions for the atlas to instantiate public float borderX1; public float borderX2; public float borderY1; public float borderY2; // UI public TMP_Text atlasName; private int a = 0; // to assign names for A atlases private int b = 0; // to assign name for B atlases private void Start() { // If no name in A atlas, no name in all subsequent atlases // so we can check just A_atlasNames once // A atlas if (Names.A_atlasNames != null) { //for (int i = 0; i < Names.A_atlasNames.Count; i++) //InstantiateAtlas_A(); } // B atlas if (Names.B_atlasNames != null) { //for (int i = 0; i < Names.B_atlasNames.Count; i++) //InstantiateAtlas_B(); } } // A atlas private void InstantiateAtlas_A() { Vector3 position = new Vector3( Random.Range(borderX1, borderX2), Random.Range(borderY1, borderY2), 0); GameObject newAtlasA = Instantiate(a_prefab, position, Quaternion.identity); //newAtlasA.GetComponent<Transform>().localScale = new Vector3(0.3f, 0.3f, 0.3f); //GameObject numbers = newAtlasA.transform.Find("Numbers").gameObject; //numbers.SetActive(false); // Atlas's name newAtlasA.name = Names.A_atlasNames[a]; a++; AddColor_A(newAtlasA); } private void AddColor_A(GameObject newAtlasA) { // Get ColorantManagers.cs on gameObject's childrens var colorantManagers = newAtlasA.GetComponentsInChildren<ColorantManager>(); // Each element with ColorantManager.cs // gets the colour assigned from AtlasData.cs for (int i = 0; i < colorantManagers.Length; i++) { int index = GetElementIndex_A(i); if (index == 0) colorantManagers[i].OnBtnColorA(); else if (index == 1) colorantManagers[i].OnBtnColorB(); } } private int GetElementIndex_A(int i) { switch (i) { case 0: return AtlasData.sliceA1; case 1: return AtlasData.sliceA2; case 2: return AtlasData.sliceA3; case 3: return AtlasData.sliceA4; case 4: return AtlasData.sliceA5; case 5: return AtlasData.sliceA6; case 6: return AtlasData.sliceA7; default: return 0; } } // B atlas private void InstantiateAtlas_B() { Vector3 position = new Vector3( Random.Range(borderX1, borderX2), Random.Range(borderY1, borderY2), 0); GameObject newAtlasB = Instantiate(b_prefab, position, Quaternion.identity); newAtlasB.name = Names.B_atlasNames[b]; b++; AddColor_B(newAtlasB); } private void AddColor_B(GameObject newAtlasB) { // Get ColorantManagers.cs on gameObject's childrens var colorantManagers = newAtlasB.GetComponentsInChildren<ColorantManager>(); // Each element with ColorantManager.cs // gets the colour assigned from AtlasData.cs for (int i = 0; i < colorantManagers.Length; i++) { int index = GetElementIndex_B(i); if (index == 0) colorantManagers[i].OnBtnColorA(); else if (index == 1) colorantManagers[i].OnBtnColorB(); else if (index == 2) colorantManagers[i].OnBtnColorC(); else if (index == 3) colorantManagers[i].OnBtnColorD(); else if (index == 4) colorantManagers[i].OnBtnColorD(); } } private int GetElementIndex_B(int i) { switch (i) { case 0: return AtlasData.sliceB1_body; case 1: return AtlasData.sliceB2_body; case 2: return AtlasData.sliceB3_body; case 3: return AtlasData.sliceB4_body; case 4: return AtlasData.sliceB5_body; case 5: return AtlasData.sliceB1_left; case 6: return AtlasData.sliceB2_left; case 7: return AtlasData.sliceB3_left; case 8: return AtlasData.sliceB4_left; case 9: return AtlasData.sliceB5_left; case 10: return AtlasData.sliceB1_right; case 11: return AtlasData.sliceB2_right; case 12: return AtlasData.sliceB3_right; case 13: return AtlasData.sliceB4_right; case 14: return AtlasData.sliceB5_right; default: return 0; } } public void OnOrangeryButton() { SceneManager.LoadScene(SceneHistory.LastSceneName); // Work-around while there are A & B atlases only if (SceneHistory.LastSceneName == null) SceneManager.LoadScene("A_Atlas"); } } ``` ![[atlas.borders]]