IEnumerator coroutineRecreateTerrain(Ferr2DT_PathTerrain pathTerrain, GameObject dust1, GameObject dust2) { yield return(new WaitForSeconds(0.1F)); pathTerrain.GetComponent <Ferr2DT_PathTerrain>().Build(); pathTerrain.GetComponent <Ferr2DT_PathTerrain>().RecreateCollider(); //ветки start if (twigs != null) { for (int i = 0; i < twigs.childCount; i++) { LayerMask mask = LayerMask.GetMask("terrains"); //RaycastHit2D hit = Physics2D.Raycast(twigs.GetChild(i).position, Vector2.up, 0.05F, mask); RaycastHit2D hit = Physics2D.CircleCast(twigs.GetChild(i).position, 0.005F, Vector2.down, 0.07F, mask); if (hit.collider == null) { twigs.GetChild(i).gameObject.SetActive(false); } } } //ветки end //отсоединяем все линии грута for (int i = 0; i < gGrootClass.terrainGrootChains.Count; i++) { gGrootClass.terrainGrootChain terr = gGrootClass.terrainGrootChains[i]; if (terr.terrain == pathTerrain.gameObject) { if (!pathTerrain.GetComponent <Collider2D>().OverlapPoint(terr.chain.transform.position)) { terr.chain.transform.parent.SendMessage("OnPress", true); gGrootClass.terrainGrootChains.Remove(terr); i--; } } } // yield return(new WaitForSeconds(0.5F)); Destroy(dust1); Destroy(dust2); }
void OnTriggerExit2D(Collider2D collider) { if (collider.tag == "terrain") { if (terrainsTransform.Contains(collider.transform.position)) { return; } //for (int i = 0; i < terrainsTransform.Count; i++) { // // if (collider.transform == terrainsTransform[i]) Debug.Log(123); //} terrainsTransform.Add(collider.transform.position); exitPoint = transform.position; Vector2 diff = exitPoint - enterPoint; float pointBDiffC = Mathf.Sqrt(diff.x * diff.x + diff.y * diff.y); float diffX = 15 / pointBDiffC * diff.x; float diffY = 15 / pointBDiffC * diff.y; exitPoint = new Vector2(diffX, diffY) + enterPoint; //Debug.Log ("exit: " + collider.name + exitPoint); Ferr2D_Path terrain = collider.GetComponent <Ferr2D_Path>(); List <Vector2> pos = new List <Vector2>(); List <int> point = new List <int>(); Vector2 firstPoint = new Vector2(0, 0); Vector2 secondPoint = new Vector2(0, 0); int firstPointA = -1, secondPointA = -1; Vector2 posTemp, posA, posB; int terrainCount = terrain.Count; for (int i = 0; i < terrainCount; i++) { if (i == terrainCount - 1) { posA = terrain.pathVerts[0] + new Vector2(collider.transform.position.x, collider.transform.position.y); posB = terrain.pathVerts[i] + new Vector2(collider.transform.position.x, collider.transform.position.y); posTemp = lineIntersectPos(enterPoint, exitPoint, posA, posB); } else { posA = terrain.pathVerts[i + 1] + new Vector2(collider.transform.position.x, collider.transform.position.y); posB = terrain.pathVerts[i] + new Vector2(collider.transform.position.x, collider.transform.position.y); posTemp = lineIntersectPos(enterPoint, exitPoint, posA, posB); } if (posTemp.x != 10000) { if (Mathf.Abs(posTemp.x - posA.x) <= 0.02F && Mathf.Abs(posTemp.y - posA.y) <= 0.02F) { posTemp = posA; } if (Mathf.Abs(posTemp.x - posB.x) <= 0.02F && Mathf.Abs(posTemp.y - posB.y) <= 0.02F) { posTemp = posB; } //Debug.Log("enterPoint: " + enterPoint); //Debug.Log("exitPoint: " + exitPoint); //Debug.Log("posA: " + posA); //Debug.Log("posB: " + posB); //Debug.Log("pos: " + posTemp); pos.Add(posTemp); point.Add(i); } //Debug.Log("pos.x: " + pos.x); } //sorting float minLength = 100; List <Vector2> posSort = new List <Vector2>(); List <int> pointSort = new List <int>(); int j = -1; for (int i = 0; i < pos.Count; i++) { for (int y = 0; y < pos.Count; y++) { if ((pos[y] - enterPoint).magnitude < minLength) { minLength = (pos[y] - enterPoint).magnitude; j = y; } } minLength = 100; posSort.Add(pos[j]); pos[j] = new Vector2(100, 100); pointSort.Add(point[j]); } for (int y = 0; y < posSort.Count - 1; y += 2) { terrain = collider.GetComponent <Ferr2D_Path>(); int g = 0; if (y > 0) { //Debug.Log("terrain.pathVerts[1]: " + terrain.pathVerts[1]); //Debug.Log("posSort[y - 1]: " + posSort[y - 1]); if (terrain.pathVerts[1] == posSort[y - 1] - new Vector2(collider.transform.position.x, collider.transform.position.y)) { g = y - 1; } else { g = y - 2; } for (int q = y; q < posSort.Count; q++) { //Debug.Log ("pointSort[q]: " + pointSort[q]); //Debug.Log ("pointSort[g]: " + pointSort[g]); //Debug.Log ("g: " + g); pointSort[q] = pointSort[q] - pointSort[g] + 1; if (pointSort[q] < 0) { pointSort[q] += terrainCount; } } } firstPointA = pointSort[y]; secondPointA = pointSort[y + 1]; firstPoint = posSort[y] - new Vector2(collider.transform.position.x, collider.transform.position.y); secondPoint = posSort[y + 1] - new Vector2(collider.transform.position.x, collider.transform.position.y); //Debug.Log ("fp: " + firstPoint); //Debug.Log ("sp: " + secondPoint); bool flag = true; int i = -1; List <Vector2> firstFigure = new List <Vector2>(); List <Vector2> secondFigure = new List <Vector2>(); terrainCount = terrain.Count; //Debug.Log("pathVerts: "); //for (int e = 0; e < terrainCount; e++) Debug.Log(terrain.pathVerts[e]); while (flag) { if (i == -1) { //i = firstPointA; //Debug.Log (firstPointA); firstFigure.Add(firstPoint); //terrain.pathVerts.Add(firstPoint); //Debug.Log (secondPointA); firstFigure.Add(secondPoint); i = secondPointA + 1; } if (i >= terrainCount) { i = 0; } if (i == firstPointA) { flag = false; } //Debug.Log (i); if (terrain.pathVerts[i] != firstPoint && terrain.pathVerts[i] != secondPoint) { firstFigure.Add(terrain.pathVerts[i]); } i++; } flag = true; i = -1; while (flag) { if (i == -1) { secondFigure.Add(secondPoint); secondFigure.Add(firstPoint); i = firstPointA + 1; } if (i >= terrainCount) { i = 0; } if (i == secondPointA) { flag = false; } //Debug.Log(i); if (terrain.pathVerts[i] != firstPoint && terrain.pathVerts[i] != secondPoint) { secondFigure.Add(terrain.pathVerts[i]); } i++; } terrain.pathVerts.Clear(); if (getSq(firstFigure) >= getSq(secondFigure)) { terrain.pathVerts.AddRange(firstFigure); } else { terrain.pathVerts.AddRange(secondFigure); } //Debug.Log (terrain.pathVerts[0]); //terrain.UpdateDependants(); Ferr2DT_PathTerrain pathTerrain = collider.GetComponent <Ferr2DT_PathTerrain>(); pathTerrain.RecreatePath(); pathTerrain.RecreateCollider(); } for (int i = 0; i < gGrootClass.terrainGrootChains.Count; i++) { gGrootClass.terrainGrootChain terr = gGrootClass.terrainGrootChains[i]; if (terr.terrain == collider.gameObject) { if (!terr.terrain.GetComponent <Collider2D>().OverlapPoint(terr.chain.transform.position)) { terr.chain.transform.parent.SendMessage("OnMouseDown"); gGrootClass.terrainGrootChains.Remove(terr); i--; } } } } }