public void Awake() { Transform ca = transform.parent; codingArea = ca.GetComponent <CodingArea>().GetInstance(); clickable = true; variableDisplay.localScale = new Vector3(1, 0, 1); varText = variableDisplay.Find("Display").Find("Label").GetComponent <Text>(); }
void Awake() { audioSource = GetComponent <AudioSource>(); rectTransform = GetComponent <RectTransform>(); //Load Map --- Ratio 16:9 string[] lines = Maps.GetRandomMap(); //System.IO.File.ReadAllLines("game2.map"); if (lines == null) { throw new System.InvalidOperationException("Could not load game map"); } map_height = lines.Length; map_width = lines[0].Length; char[,] _map; _map = new char[map_width, map_height]; map = new GridStruct[map_width, map_height]; int y = 0; foreach (string line in lines) { int x = 0; foreach (char c in line) { _map[x, y] = c; x++; } y++; } // Scale gridsize ratio = rectTransform.sizeDelta.x / map_width; float deltaSize_y = rectTransform.sizeDelta.y - (ratio * map_height); float deltaSize_x = 0; if (deltaSize_y >= 0) { gridSize = Mathf.FloorToInt(ratio); } else { ratio = rectTransform.sizeDelta.y / map_height; deltaSize_x = rectTransform.sizeDelta.x - (ratio * map_width); deltaSize_y = 0; gridSize = Mathf.FloorToInt(ratio); } //Grid position offset float x_offset = (-rectTransform.sizeDelta.x + deltaSize_x + ratio) * 0.5f; float y_offset = (-rectTransform.sizeDelta.y + deltaSize_y + ratio) * 0.5f; // Draw Grid for (int h = 0; h < map_height; h++) { for (int w = 0; w < map_width; w++) { //Create Transform instance = Instantiate(gridTransform); instance.SetParent(transform); //Scale SpriteRenderer sr = instance.GetComponent <SpriteRenderer>(); instance.localScale = new Vector3(ratio / sr.size.x, ratio / sr.size.y, 1); //Postion instance.localPosition = new Vector3(w * ratio + x_offset, -h * ratio - y_offset, -1); map[w, h].grid = instance; map[w, h].type = Type.Empty; if (_map[w, h] == 'x') { //Summon block Transform _block = Instantiate(pfReefArray[UnityEngine.Random.Range(0, pfReefArray.Length)]); SpriteRenderer srBlock = _block.GetComponent <SpriteRenderer>(); float _scale = 0.9f; _block.localScale = new Vector3(ratio * _scale / srBlock.size.x, ratio * _scale / srBlock.size.y, 1); _block.parent = map[w, h].grid; _block.localPosition = new Vector3(0, 0, 0); map[w, h].content = _block; map[w, h].type = Type.Block; } if (_map[w, h] == 's') { Transform _fish = Instantiate(fish); SpriteRenderer srBlock = _fish.GetComponent <SpriteRenderer>(); float _scale = 0.6f; _fish.localScale = new Vector3(ratio * _scale / srBlock.size.x, ratio * _scale / srBlock.size.y, 1); _fish.parent = map[w, h].grid; _fish.localPosition = new Vector3(0, 0, 0); map[w, h].content = _fish; map[w, h].type = Type.Fish; fishPosition = new Vector2Int(w, h); startPosition = new Vector2Int(w, h); fishDirection = new Direction(); } if (_map[w, h] == 'e') { Transform _end = Instantiate(goal); SpriteRenderer srBlock = _end.GetComponent <SpriteRenderer>(); float _scale = 0.7f; _end.localScale = new Vector3(ratio * _scale / srBlock.size.x, ratio * _scale / srBlock.size.y, 1); _end.parent = map[w, h].grid; _end.localPosition = new Vector3(0, 0, 0); map[w, h].content = _end; map[w, h].type = Type.End; } } } Debug.Log("Created grid..."); codingArea = codingAreaReferenceTransform.GetComponent <CodingArea>(); codingArea.OnButtonStart += CodingArea_OnButtonStart; //codingArea.ActionEvent += CodingArea_ActionEvent; codingArea.ResetEvent += CodingArea_ResetEvent; }
CodingArea() { instance = this; }