using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SelectDrag : MonoBehaviour { public GameObject O; bool drag = false; GameObject Oclone; public void Select() { if (O.name.IndexOf("_") == -1 ) { GameObject Oc = Instantiate(O); Oc.name = O.name + "_" + _G.OBJnum; _G.OBJnum += 1; Oc.transform.SetParent(Get.o2("Canvas", "OBSTACLELIST").transform, false); Oc.transform.localPosition = O.transform.localPosition; //Oc.tag = "C1"; //Oc.transform.Find("icon").tag = "C2"; _G.SELECTED=Oc.name; startDrag(Oc); } else { _G.SELECTED=name; print("_G.SELECTED="+_G.SELECTED); O=Get.o1(_G.SELECTED); if (_G.PNL != "POSITIONING")startDrag(O); if (_G.PNL == "POSITIONING") {RepositionPNL.StartPNL(O.name, O);} } } public void startDrag(GameObject Oc) { print("O.name="+Oc.name); foreach (Transform go in Get.o2("Canvas", "OBSTACLELIST").transform) { go.gameObject.GetComponent().enabled = false; } Oc.gameObject.GetComponent().enabled = true; foreach (Transform child in Oc.transform) if (child.name.StartsWith("T_")) child.gameObject.SetActive(false); DELETE.HideButtons(); Oclone = Oc; drag = true; int On=int.Parse(_G.SELECTED.Split("_")[1]); _G.OBJs[On] = null; } public void stopDrag() { foreach (Transform go in Get.o2("Canvas", "OBSTACLELIST").transform) { go.tag = "Obstacle"; } drag = false; if (_G.PNL != "POSITIONING")Oclone.transform.Find("Image").gameObject.SetActive(false); float posY = Input.mousePosition.y / Screen.height; if (_G.PNL != "POSITIONING")Oclone.transform.Find("Circle").GetComponent().color = new Color(1f, 0f, 0f, 0f); if (posY <= 0.1f || posY >= 0.9f) {Discard(); } if(_G.PNL=="OBSTACLES"){ RepositionPNL.CheckWallHit(Oclone,"OBSTACLES"); if (!IsInsidePlan(Oclone)) { Discard(); return; } if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("DOOR")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("WIND")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("OPEN")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("PATI")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("PLUM")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("GAZ")!=-1){Discard(); } else if(_G.WallSELECTED=="floor" && Oclone.name.IndexOf("DOOR")!=-1){Discard(); } } DELETE.Refresh();// contour vert + corbeille sur l'objet qui vient d'etre pose } // Le depot est refuse : l'objet disparait, donc plus rien n'est selectionne. void Discard() { Destroy(Oclone); _G.SELECTED = ""; DELETE.Refresh(); } void Update() { if (drag == true) { if (Input.GetMouseButtonUp(0)) { stopDrag(); return; } if(Oclone!=null){ Oclone.transform.position = Input.mousePosition; if(_G.PNL=="OBSTACLES")RepositionPNL.CheckWallHit(Oclone,"OBSTACLES"); } } } public void DeSelecttWall() { if (_G.PNL == "OBSTACLES") { foreach(Transform Wall in Get.o2("OBSTACLES","Plan").transform ) { Wall.transform.GetComponent().color=Color.black; } } } static bool IsInsidePlan(GameObject obj) { Vector3[] c = new Vector3[4]; Bounds allWalls = new(Vector3.zero, Vector3.zero); bool hasWalls = false; for (int i = 1; i <= _G.NW; i++) { GameObject wall = Get.o3("OBSTACLES", "Plan", "w" + i); if (wall == null) continue; wall.GetComponent().GetWorldCorners(c); if (!hasWalls) { allWalls = new(c[0], Vector3.zero); hasWalls = true; } for (int j = 0; j < 4; j++) allWalls.Encapsulate(c[j]); } if (!hasWalls) return true; obj.GetComponent().GetWorldCorners(c); Bounds objB = new(c[0], Vector3.zero); for (int i = 1; i < 4; i++) objB.Encapsulate(c[i]); return allWalls.Intersects(objB); } void OnTriggerEnter(Collider hit) { if (hit.gameObject.CompareTag("Obstacle")) { drag = false; } } void OnTriggerExit(Collider hit) { if (hit.gameObject.CompareTag("Obstacle")) { drag = true; } } }