void ShuffleTypes() { int temp = 0; foreach (GameObject g in gems) { gemScript.Add(gems[temp].GetComponent <Gem>()); temp++; } foreach (Gem g in gemScript) { g.type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } }
// Start is called before the first frame update void Start() { if (NSM == null) { NSM = GameObject.Find("SelectControl").GetComponent <NodeStateMachine>(); } objectPooler = ObjectPooler.Instance; for (int k = 0; k < gridX; k++) { GameObject g = objectPooler.SpawnFromPool(genTag, new Vector3(-startX + k * offX, 4.4f, 0), Quaternion.identity); if (!gemGenerator.Contains(g)) { gemGenerator.Add(g); } } for (int k = 0; k < gridY; k++) { for (int l = 0; l < gridX; l++) { GameObject g = objectPooler.SpawnFromPool(objTag, new Vector3(-startX + l * offX, startY - k * offY, 0), Quaternion.identity); g.gameObject.GetComponent <Gem>().type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); if (!gems.Contains(g)) { gems.Add(g); } g.gameObject.GetComponent <Gem>().parent = gemGenerator[l].gameObject.GetComponent <NewGemGenerator>(); if (!gemGenerator[l].gameObject.GetComponent <NewGemGenerator>().column.Contains(g)) { gemGenerator[l].gameObject.GetComponent <NewGemGenerator>().column.Add(g); } //vecs.Add(g.transform.position); } } }
// Update is called once per frame void Update() { RaycastHit2D hit = Physics2D.Raycast(new Vector2(this.transform.position.x, this.transform.position.y + rayOffset), new Vector2(this.transform.position.x, this.transform.position.y + rayOffset) + new Vector2(0, rayLenght)); /*Debug.DrawLine(new Vector2(this.transform.position.x, this.transform.position.y + rayOffset), * new Vector2(this.transform.position.x, this.transform.position.y + rayOffset) + new Vector2(0, rayLenght) * , Color.red);*/ if (hit) { rayTouch = true; ready = true; } else { rayTouch = false; } if (column.Count < 6) { if (!rayTouch) { if (canCreate && ready) { GameObject g = objectPooler.SpawnFromPool(objTag, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, 0), Quaternion.identity); g.gameObject.GetComponent <Gem>().type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); if (!gl.gems.Contains(g)) { gl.gems.Add(g); } if (!column.Contains(g)) { column.Add(g); } canCreate = false; StartCoroutine(Countdown(startTimer)); cache = false; ready = false; } else if (ready) { cache = true; } } if (cache && canCreate && ready) { GameObject g = objectPooler.SpawnFromPool(objTag, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, 0), Quaternion.identity); g.gameObject.GetComponent <Gem>().type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); if (!gl.gems.Contains(g)) { gl.gems.Add(g); } if (!column.Contains(g)) { column.Add(g); } canCreate = false; StartCoroutine(Countdown(startTimer)); cache = false; ready = false; } } foreach (GameObject g in column) { g.gameObject.GetComponent <Gem>().parent = this; float testvec = this.gameObject.transform.position.x - g.transform.position.x; if (Mathf.Abs(testvec) > 1f) { column.Remove(g); } } }
private void MatchShuffle() { int temp = 0; foreach (GameObject g in gems) { if (!gemScript.Contains(g.GetComponent <Gem>())) { gemScript.Add(gems[temp].GetComponent <Gem>()); temp++; } } for (int k = 0; k < gemScript.Count; k++) { if (k - 1 >= 0 && k - 2 >= 0 && (k) % 6 != 0 && k % gridX != 0 && k % gridX != 1) { if (gemScript[k].type == gemScript[k - 1].type && gemScript[k].type == gemScript[k - 2].type) { //checkGemOnGrid List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k - 1); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } if (k - 1 >= 0 && k + 1 < gemScript.Count && k % gridX != gridX - 1 && k % gridX != 0) { if (gemScript[k].type == gemScript[k - 1].type && gemScript[k].type == gemScript[k + 1].type) { List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k - 1); tempLI.Remove(k + 1); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } if (k + 1 < gemScript.Count && k + 2 < gemScript.Count && k % gridX != gridX - 1 && k % gridX != gridX - 2) { if (gemScript[k].type == gemScript[k + 1].type && gemScript[k].type == gemScript[k + 2].type) { List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k + 1); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } if (k - 1 * gridX >= 0 && k - 2 * gridX >= 0) { if (gemScript[k].type == gemScript[k - 1 * gridX].type && gemScript[k].type == gemScript[k - 2 * gridX].type) { List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k - gridX); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } if (k - 1 * gridX >= 0 && k + 1 * gridX < gemScript.Count) { if (gemScript[k].type == gemScript[k - 1 * gridX].type && gemScript[k].type == gemScript[k + 1 * gridX].type) { List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k - gridX); tempLI.Remove(k + gridX); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } if (k + 1 * gridX < gemScript.Count && k + 2 * gridX < gemScript.Count) { if (gemScript[k].type == gemScript[k + 1 * gridX].type && gemScript[k].type == gemScript[k + 2 * gridX].type) { List <int> tempLI = checkPossibleMovment(k); tempLI.Remove(k + gridX); int op = tempLI[Random.Range(0, tempLI.Count - 1)]; //Gem_Type gem = gemScript[op].type; gemScript[op].type = gemScript[k].type; gemScript[k].type = (Gem_Type)Random.Range(0, Gem_Type.GetNames(typeof(Gem_Type)).Length); } } } gemScript.Clear(); }