using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using System.Collections; public class DASH : MonoBehaviour { static public Action _OnRoomRefresh; //List ONLIST = new (); static Camera _mainCamera; static Camera MainCamera { get { if (_mainCamera == null) { _mainCamera = Camera.main; } return _mainCamera; } } public void SELECT() { //Clear list for (int i = GameObject.Find("Panel").transform.Find("LIST").transform.childCount - 1; i >= 0; i--) { GameObject.Destroy(GameObject.Find("Panel").transform.Find("LIST").transform.GetChild(i).gameObject); } Invoke("Addlikst", 0.5f); } public void Recalcul() { PRICE.CalpriceNow(); } public static void REFRESH() { //print("-----------Refresh------------"); GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(true); _G.HIT = ""; SceneModeManager.SelectedName = ""; _G.TARGET = ""; _G.SetAddOBJ(""); SceneModeManager.Instance.SetSceneMode(SceneModes.Navigate); List OBJS = DOLIST.OBJLIST(); List objectToDelete = new(); //List wallsToRefresh = new List(); float maxPositionDistance = HOLE.MaxPositionDistanceFromWall; float maxSideDistance = HOLE.MaxSideDistanceFromWall; float maxDegreeDelta = HOLE.MaxForwardDegreeFromWall; float maxForwardDelta = Mathf.Cos(maxDegreeDelta * Mathf.Deg2Rad); foreach (GameObject O in OBJS) { string objectName = O.name; if (!HOLE.IsHoleObject(objectName)) { O.GetComponent().material = _G.INV; } else { int objectIndex = Get.GetObjectIndex(objectName); if (objectIndex == 299) { Debug.Log("No Object Index found"); } string attachedTo = _G.OBJs[objectIndex][22]; if (string.IsNullOrEmpty(attachedTo)) { Debug.Log($"No Object attached to in the object data "); } string nearestWall = ""; float nearestSqrDistance = float.MaxValue; Vector3 nearestWallDirection = Vector2.zero; Vector2 nearestPointOnWall = Vector2.zero; Transform objectTransform = O.transform; Vector3 oldObjectPosition = DOIT.GetPosObj(objectName); Vector2 positionXZ = new(oldObjectPosition.x, oldObjectPosition.z); Vector3 openingNormal = -objectTransform.forward; foreach (RectTransform item in WallCreationManager.GetWallLineContainer()) { int currentWallIndex = item.GetSiblingIndex(); string currentWallName = $"w{currentWallIndex + 1}"; Transform currentWallTransform = SceneModeManager.GetObjectInRoom(currentWallName); if (currentWallTransform == null) { continue; } WallLine currentWallLine = item.GetComponent(); Vector2 startPoint = _G.WallsPointStart[currentWallIndex]; //Vector2 endPoint = startPoint; Vector2 line = (_G.WallsPointCenter[currentWallIndex] - startPoint) * 2.0f; Vector2 from = positionXZ - startPoint; Vector2 lineDirection = line.normalized; float dotDistanceOnWall = Vector2.Dot(from, lineDirection); float wallWidth = _G.WallsWidth[currentWallIndex]; float openWidthOffset = (objectTransform.lossyScale.x - 0.5f) / 2.0f; //bool isOutsideWallEdges = dotDistanceOnWall < openWidthOffset || dotDistanceOnWall > wallWidth - openWidthOffset; float maxOffsetFromWall = openWidthOffset - maxSideDistance; bool isOutsideMaxEdgeRange = dotDistanceOnWall < maxOffsetFromWall || dotDistanceOnWall > wallWidth - maxOffsetFromWall; if (isOutsideMaxEdgeRange) { continue; } Vector3 wallNormal = -currentWallTransform.forward; float dotHoleAndWallDirection = Vector3.Dot(wallNormal, openingNormal); bool isHoleAndWallForwardFacing = dotHoleAndWallDirection > maxForwardDelta; if (!isHoleAndWallForwardFacing) { continue; } //Debug.Log($"{objectName} ; dotDistanceOnWall : {dotDistanceOnWall} ; wallWidth : {wallWidth} ; scaleOffset : {openWidthOffset}"); //dotDistanceOnWall = Mathf.Clamp(dotDistanceOnWall, openWidthOffset, wallWidth - openWidthOffset); Vector2 closestPointOnWallLine = startPoint + lineDirection * dotDistanceOnWall; Vector2 projectionOnLine = closestPointOnWallLine - positionXZ; float currentSqrDistance = projectionOnLine.sqrMagnitude; if (currentSqrDistance > maxPositionDistance * maxPositionDistance || currentSqrDistance > nearestSqrDistance) { continue; } nearestSqrDistance = currentSqrDistance; nearestWallDirection = wallNormal; nearestPointOnWall = closestPointOnWallLine; nearestWall = currentWallName; } if (string.IsNullOrEmpty(nearestWall)) { // TODO : use delete command and save it, along with all modifications done on refresh Debug.Log($"(Object to destroy {objectName}"); objectToDelete.Add(O); continue; } _G.OBJs[objectIndex][22] = nearestWall; objectTransform.position = new Vector3(nearestPointOnWall.x, objectTransform.position.y, nearestPointOnWall.y); Transform nearestWallTransfrom = SceneModeManager.GetObjectInRoom(nearestWall); objectTransform.rotation = nearestWallTransfrom.rotation; DOIT.SaveObjectPositionInData(O); //Debug.Log($"object position : {objectTransform.position} ; nearestPointOnWall : {nearestPointOnWall} ; wall forward : {nearestWallDirection}"); //if (!wallsToRefresh.Contains(_G.OBJs[nO][22])) //{ // wallsToRefresh.Add(_G.OBJs[nO][22]); //} SceneModeManager.SelectedName = ""; // print("-----------Refresh-----2-------"+os); } } OBJS.Clear(); for (int i = objectToDelete.Count - 1; i >= 0; i--) { GameObject.DestroyImmediate(objectToDelete[i]); } objectToDelete.Clear(); DOIT.CloseALLPNL(); DOIT.ResetRoomSelect(); _G.NAVIGATE = true; DOIT.AllSelOff(); if (_G.Redflag == true) Collision.checkALL(); if (GameObject.Find("PS") != null && _G.PATH == "demo") GameObject.Find("PS").GetComponent().text = "---"; //_OnRoomRefresh?.Invoke(); StaticCoroutine.Start(RefreshCoroutine()); } private static IEnumerator RefreshCoroutine() { // Lancer les deux autres coroutines séquentiellement yield return StaticCoroutine.Instance.StartCoroutine(SetObjectOnWallsCoroutine()); yield return StaticCoroutine.Instance.StartCoroutine(RepositionObjectOnWallCoroutine()); // Puis rafraîchir l’affichage HOLE.RefreshAllWalls(); RefreshLite(); // Masquer le cercle de chargement GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(false); } public static void RefreshLite() { _MOL.SetMolding(); // UpdateCabAttachments(); } public static void GridAll() { List OBJS = DOLIST.OBJLIST(); foreach (GameObject O in OBJS) { Gridding.SnapOn(O.name); } } public static void UpdateCabAttachments() { List OBJS = DOLIST.OBJLIST(); foreach (GameObject O in OBJS) { int nO = Get.GetObjectIndex(O.name); if (HOLE.IsHoleObject(O.name)) { continue; } if(O.TryGetComponent(out var moveObject)) { string attachedName = moveObject.AttachedTo == null ? _G.OBJs[nO][22] : moveObject.AttachedTo.name; // null check because not sure if AttachedTo implemented fully Transform attachedTransform = SceneModeManager.GetObjectInRoom(attachedName); if (attachedTransform == null) { moveObject.AttachedTo = SceneModeManager.Floor; } float distanceDetach = 24.0f; float distanceAttach = 1.0f; Transform objectTransform = O.transform; if (attachedName != SceneModeManager.Floor.name && attachedName != SceneModeManager.Ceil.name) { bool isObjectInRangeOfWall; string wallLineName = attachedName.Replace("w", "WallLine"); if(WallCreationManager.GetWallLineContainer().Find(wallLineName)) { WallLine wallLine = WallCreationManager.GetWallLineContainer().Find(wallLineName).GetComponent(); isObjectInRangeOfWall = IsInRangeOfWall(objectTransform, wallLine, distanceDetach); if (!isObjectInRangeOfWall) { moveObject.AttachedTo = SceneModeManager.Floor; } else { RectTransform wallLineContainer = WallCreationManager.GetWallLineContainer(); foreach (RectTransform wallLineRectTransform in wallLineContainer) { wallLine = wallLineRectTransform.GetComponent(); isObjectInRangeOfWall = IsInRangeOfWall(objectTransform, wallLine, distanceAttach); if (isObjectInRangeOfWall) { string wallName = wallLineRectTransform.name.Replace("WallLine", "w"); moveObject.AttachedTo = SceneModeManager.GetObjectInRoom(wallName); break; } } } } } } } OBJS.Clear(); } private static bool IsInRangeOfWall(Transform objectTransform, WallLine wallLine, float maxDistance) { // find approximate closest point on wall and object Vector3 closestWallPosition = wallLine.GetNearestPositionOnWall(objectTransform.position); Vector3 closestObjectPosition = objectTransform.GetComponent().ClosestPoint(closestWallPosition); // compare distance float maxSqrDistance = maxDistance * maxDistance; bool isObjectInRangeOfWall = (closestObjectPosition - closestWallPosition).sqrMagnitude < maxSqrDistance; return isObjectInRangeOfWall; } public static void TOUCHWALL() { List OBJS = DOLIST.OBJLIST(); foreach (GameObject Ol in OBJS) { int nO = Get.GetObjectIndex(Ol.name); Vector3 A = Ol.transform.rotation.eulerAngles; Ol.transform.eulerAngles = new Vector3(0, 0, 0); Vector3 OB = Ol.GetComponent().bounds.size; Ol.transform.GetComponent().transform.localScale = new Vector3(OB.x - 3f, OB.y - 3f, OB.z - 3f); Ol.transform.eulerAngles = new Vector3(A.x, A.y, A.z); Bounds BoundO = Ol.GetComponent().bounds; for (int i = 1; i < 24; i++) { string ms = "m" + i.ToString(); if (GameObject.Find(ms)) { if (BoundO.Intersects(GameObject.Find(ms).GetComponent().bounds)) { if (_G.Redflag == true) { Ol.GetComponent().material = _G.GRT; } } } } } } public static bool CHECKI(string O) { bool BI = false; int nO = Get.GetObjectIndex(O); int ind = System.Array.IndexOf(_L.Cab, _G.OBJs[nO][1]); string SI = _L.Cab[ind + 7]; if (SI != "0") BI = true; return BI; } public void Refresh() { REFRESH(); } static public void RESETSCENE() { Camera currentCamera = MainCamera; //print("currentCamera.GetComponent().transform.position==="+currentCamera.GetComponent().transform.position); if(currentCamera.GetComponent().transform.position != new Vector3(0,0,-150)){ currentCamera.orthographic = false; ObjectFocus objF = currentCamera.GetComponent(); objF.FocusCamera(_G._DefaultCameraFocusPoint, _G._DefaultCameraPosition, _G._DefaultCameraRotation); Get.o2("DASH", "CAMERAS").SetActive(false); } ShowTopWall(false); } private static IEnumerator RepositionObjectOnWallCoroutine() { for (int i = 0; i < _G.OBJnum; i++) { if (DOIT.exist(i) && _G.OBJs[i][22].Contains("w") && !_G.OBJs[i][22].Contains("wind")&& !_G.OBJs[i][22].Contains("wine") ) { string[] ADN = _G.OBJs[i]; GameObject Object = Get.o2("SCENE", ADN[0]); print("ADN[22]=======" + ADN[22]+" "); if(Get.o2("ROOM", ADN[22]) != null){ Vector3 WallPosition = Get.o2("ROOM", ADN[22]).transform.position; string[] OffsetString = ADN[14].Split(","); float x = DOIT.ConvertStringToNumber(OffsetString[0]); float y = DOIT.ConvertStringToNumber(OffsetString[1]); float z = DOIT.ConvertStringToNumber(OffsetString[2]); Vector3 Offset = new(x, y, z); Object.transform.position = WallPosition + Offset; DOIT.SaveObjectPositionInData(Object); } } yield return null; } } private static IEnumerator SetObjectOnWallsCoroutine() { for (int i = 0; i < _G.OBJnum; i++) { if (DOIT.exist(i) && _G.OBJs[i][22].Contains("w")) { string[] ADN = _G.OBJs[i]; ADN[22] = GetNearestWallname(i); Debug.Log($"{ADN[0]} {ADN[22]}"); } yield return null; } } private static string GetNearestWallname(int ADNNumber) { string[] ADN = _G.OBJs[ADNNumber]; GameObject Object = Get.o2("SCENE", ADN[0]); string AttachedTo = ADN[22]; Vector3 Forward = Object.transform.forward; float projection = 0; if (new List { "open", "wind", "door", "pati" }.Contains(ADN[4])) projection = 12; Vector3 Position = Object.transform.position - projection * Forward; Ray ray = new(Position, Forward); if (Physics.Raycast(ray, out RaycastHit hit, 24)) { Transform ParentToHitObject = hit.transform.parent; float DistanceHit = Vector3.Distance(hit.point, Position); Object.transform.position += DistanceHit * Forward; if (ParentToHitObject != null && ParentToHitObject.name.StartsWith("w") && !ParentToHitObject.name.Contains("win") && DistanceHit < 24) return ParentToHitObject.name; } return AttachedTo; } public void CamTop() { CamTOP(); } public static void CamTOP() { print("----CamTOP-----"); Camera mainCamera = Camera.main; mainCamera.orthographic = false; mainCamera.transform.position = new Vector3(0, 165, 0); mainCamera.transform.localRotation = Quaternion.Euler(90, 0, 0); _G.CAMSET = CamSettings.Top; _G.CC = new Vector3(0, 0, 0); ShowTopWall(true); } public void CamORTHO() { CamOrtho(); } public static void CamOrtho() { Camera mainCamera = Camera.main; mainCamera.transform.position = new Vector3(0, 150, 0); mainCamera.transform.localRotation = Quaternion.Euler(90, 0, 0); mainCamera.orthographic = true; float scale = _G.DEPTH * 0.8f; if (_G.WIDE * 0.8 > 240) { scale = _G.WIDE * 0.4f; } mainCamera.orthographicSize = scale; _G.CAMSET = CamSettings.Ortho; _G.CC = new Vector3(0, 0, 0); ShowTopWall(true); } public static void ShowTopWall(bool isActive){ GameObject[] Tops= GameObject.FindGameObjectsWithTag("TopWall"); foreach(GameObject top in Tops) { top.SetActive(isActive); } } //HD public static void HDsetBTN() { HDsetBTNcolor(!_G.PDFon); } public static void HDsetBTNcolor(bool targetState) { //print("passer icitte===="+ONOFF); ColorBlock theColor; theColor = GameObject.Find("btnHD").GetComponent