using System.Collections.Generic; using System.Linq; //using System.Numerics; using Unity.VisualScripting; using UnityEngine; public class UnifyWalls : MonoBehaviour { static GameObject WallContainer; static List[] NewListRecipe; public static void FollowOnSameDirection() { Get.o2("Canvas","Panel_DRAWPlan_NEW").SetActive(true); WallContainer=Get.o2("Plan2D_UI","WallLineContainer"); List ListRecipe = new (){}; foreach (Transform child in WallContainer.transform) { ListRecipe.Add(child); } int WallQty = WallContainer.transform.childCount; NewListRecipe = new List[WallQty]; _G.ExteriorWallPoints = new List[WallQty]; int IndNR=0; for (int i=0; i< WallQty; i++ ) { NewListRecipe[i]=new(){}; int Next=i+1; if(Next>WallQty-1)Next=0; Transform L =WallContainer.transform.GetChild(i); print("i====="+i+" ListRecipe count==="+ListRecipe.Count); if( ListRecipe.Contains(L)) { print("L======================"+L.gameObject.name); if(!MatchPointFollow(i, Next)) { NewListRecipe[IndNR].Add(L); ListRecipe.Remove(L); } List RemoveList= new (); if (ListRecipe.Contains(L)) { Transform NewL= Instantiate(L); for(int n=1; nWallQty-1){Next=0;} //print("Next=====1=="+Next+" ListRecipe.Count==="+ListRecipe.Count); Transform NewNest= Instantiate(WallContainer.transform.GetChild(Next)); if(NewNest!= null && MatchPointFollow(i, Next)) { NewL.GetComponent()._pointEnd = NewNest.GetComponent()._pointEnd; //print("Next=====2=="+Next); ListRecipe.Remove(WallContainer.transform.GetChild(Next)); } } NewListRecipe[IndNR].Add(NewL); } IndNR+=1; } } RecreateWallRecipe(); Get.o2("Canvas","Panel_DRAWPlan_NEW").SetActive(false); } public static void RecreateWallRecipe(){ _G.NWExterior=0; for(int i=0; i Listvectors=new(){}; Vector3 NewPoint; foreach(Transform Line in NewListRecipe[i]){ Vector2 PointStart=Line.GetComponent()._pointStart.GetComponent().GetWorldPosition(); NewPoint=new(PointStart.x,-_G.HEIGHT*0.5f,PointStart.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); } //Point End Vector2 PointEnd=Line.GetComponent()._pointEnd.GetComponent().GetWorldPosition(); NewPoint=new(PointEnd.x,-_G.HEIGHT*0.5f,PointEnd.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); } //Point Height start float PointStartHeight=Line.GetComponent()._pointStart.GetComponent()._PointHeight; NewPoint=new(PointStart.x,-_G.HEIGHT*0.5f+PointStartHeight,PointStart.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); } //Point Height End float PointEndHeight=Line.GetComponent()._pointEnd.GetComponent()._PointHeight; NewPoint=new(PointEnd.x,-_G.HEIGHT*0.5f+PointEndHeight,PointEnd.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); //print(i.ToString()+ " Add point End height"+NewPoint); } print("Point start====="+PointStart+ "Point end====="+PointEnd); } List ListVectorOffset =new(){}; foreach(Vector3 v in Listvectors){ Vector3 Poffset=v+_G.RoomOffset; if(!ListVectorOffset.Contains(Poffset))ListVectorOffset.Add(Poffset); //print("Point from Unify==="+Poffset); } _G.ExteriorWallPoints[i]=ListVectorOffset; Vector2 PointS=new(ListVectorOffset[0].x,ListVectorOffset[0].z); Vector2 PointE=new(ListVectorOffset[1].x,ListVectorOffset[1].z); _G.WallsAngle[i]=GetWallAngleByDirection(PointS,PointE); _G.WallsWidth[i]=Vector2.Distance(PointS,PointE); _G.WallsPointCenter[i] = Vector2.Lerp(PointS, PointE, 0.5f); foreach(Vector3 v in ListVectorOffset){ } } } public static bool MatchPointFollow(int i, int o){ Transform Line = WallContainer.transform.GetChild(i); Transform other = WallContainer.transform.GetChild(o); Vector2 LineStart=Line.GetComponent()._pointStart.GetAnchoredPosition(); Vector2 LineEnd=Line.GetComponent()._pointEnd.GetAnchoredPosition(); Vector2 otherStart=other.GetComponent()._pointStart.GetAnchoredPosition(); Vector2 otherEnd=other.GetComponent()._pointEnd.GetAnchoredPosition(); Vector2 DirectionLine = LineEnd-LineStart; Vector2 Directionother = otherEnd-otherStart; bool isDirection=false; if( DirectionLine.normalized == Directionother.normalized ){isDirection=true;} bool isPointOverLap=true; if( LineStart == otherEnd || otherStart == LineEnd ){isPointOverLap=true;} if(isDirection && isPointOverLap)return true; return false; } public static void CreateFloorPoints(){ _G.FloorPoints = new List[1]; _G.FloorPoints[0]=new(){}; for(int i=0; i<_G.NWExterior; i++){ foreach(Vector3 V in _G.ExteriorWallPoints[i]) { if(V.y==-_G.HEIGHT*0.5f) { Vector3 NewPoint=new(V.x,0,V.z); if(!_G.FloorPoints[0].Contains(NewPoint)) { _G.FloorPoints[0].Add(NewPoint); } } } } } public static float GetWallAngleByDirection(Vector2 Point1,Vector2 Point2){ Vector2 direction=Point2-Point1; direction=direction.normalized; Vector2 xAxis = Vector2.right; // (1, 0) float angle = Vector2.Angle(direction, xAxis); float cross = direction.x * xAxis.y - direction.y * xAxis.x; if (cross < 0) // Negative cross means clockwise { angle = 360f - angle; } //print("Angle from unify===="+angle); return angle; } }