WallCreate.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class WallCreate : MonoBehaviour
  8. {
  9. public static WallCreate instance;
  10. public static float WallHeight = 1f;
  11. public float WallThickness = 0.05f;
  12. public int Rounding = 1;
  13. public bool move = false;
  14. public bool BoxColliderEnabled = true;
  15. public GameObject Dialog;
  16. public GameObject ZonesScroll;
  17. public GameObject Player;
  18. public enum Mode { Wall, Room, Zone, Pole, Cursor };
  19. public static Mode mode = Mode.Cursor;
  20. GameObject wall;
  21. Wall currentWall;
  22. //public Dictionary<uint, GameObject> WallObjects = new Dictionary<uint, GameObject>();
  23. public static Dictionary<uint, List<Wall>> Walls = new Dictionary<uint, List<Wall>>();
  24. //public static List<Wall> Rooms = new List<Wall>();
  25. public static Dictionary<uint, List<Zone>> Zones = new Dictionary<uint, List<Zone>>();
  26. private Vector3 screenPoint;
  27. private Vector3 offset;
  28. private Vector3 curScreenPoint;
  29. private Vector3 curPosition;
  30. private Vector3 start_pos;
  31. Vector3 Point;
  32. GameObject canvas;
  33. Text text;
  34. CompanyController company;
  35. private void Awake()
  36. {
  37. instance = this;
  38. }
  39. // Start is called before the first frame update
  40. void Start()
  41. {
  42. // gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;
  43. //cursor_end = transform.GetChild(6).gameObject;
  44. Point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  45. canvas = transform.GetChild(0).gameObject;
  46. text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();
  47. company = CompanyController.instance;
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. // if (gameObject.GetComponent<BoxCollider>().enabled != BoxColliderEnabled) gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;
  53. if (!Input.GetMouseButtonDown(0))
  54. {
  55. transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Point.z));
  56. if (transform.position.y != 0.5f) transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
  57. }
  58. }
  59. void OnMouseDown()
  60. {
  61. if (!EventSystem.current.IsPointerOverGameObject())
  62. {
  63. //Debug.Log(StagesEditorController.indexCursor);
  64. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  65. //s_point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  66. start_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  67. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  68. switch (mode)
  69. {
  70. case Mode.Wall:
  71. wall = AddWall(company, Vector3.zero);
  72. break;
  73. case Mode.Room:
  74. wall = AddRoom();
  75. break;
  76. case Mode.Zone:
  77. wall = AddZone(Dialog, company, ZonesScroll);
  78. break;
  79. }
  80. }
  81. canvas.SetActive(true);
  82. }
  83. float scale_x;
  84. float scale_z;
  85. void OnMouseDrag()
  86. {
  87. if (!EventSystem.current.IsPointerOverGameObject())
  88. {
  89. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  90. // e_point = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  91. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  92. var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  93. //Debug.Log("start_pos.x "+start_pos.x);
  94. scale_x = (float)Math.Round(end_pos.x - start_pos.x, Rounding);
  95. scale_z = (float)Math.Round(end_pos.z - start_pos.z, Rounding);
  96. text.text = $"x={scale_x} y={scale_z}";
  97. switch (mode)
  98. {
  99. case Mode.Wall:
  100. if (Math.Abs(scale_x) > Math.Abs(scale_z)/* && x_mod >= 0.4f*/)
  101. {
  102. wall.transform.localScale = new Vector3(scale_x, WallHeight, WallThickness);
  103. wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, WallHeight / 2, start_pos.z);
  104. }
  105. if (Math.Abs(scale_z) > Math.Abs(scale_x)/* && z_mod >= 0.4f*/)
  106. {
  107. wall.transform.localScale = new Vector3(WallThickness, WallHeight, scale_z);
  108. wall.transform.position = new Vector3(start_pos.x, WallHeight / 2, scale_z / 2 + start_pos.z);
  109. }
  110. break;
  111. case Mode.Room:
  112. /*if (scale_x > 0 && scale_z > 0) */
  113. LocationController.Cube(wall, new Vector3(scale_x, scale_z, WallHeight),
  114. new Vector2((float)Math.Round(start_pos.x, Rounding), (float)Math.Round(start_pos.z, Rounding)), WallThickness);
  115. break;
  116. //case Mode.Pole:
  117. // AddWall(pos);
  118. // break;
  119. case Mode.Zone:
  120. wall.transform.localScale = new Vector3(scale_x/10, 1, scale_z/10);
  121. wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, 0.05f, scale_z / 2 + start_pos.z);
  122. break;
  123. }
  124. float offsetPosY = transform.position.y + 1.5f;
  125. // Final position of marker above GO in world space
  126. canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
  127. }
  128. }
  129. private void OnMouseUp()
  130. {
  131. //var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  132. //Debug.Log($"end_pos = {end_pos}");
  133. var location_id = company.locations_index[company.active_location];
  134. if (!Walls.ContainsKey(location_id))
  135. Walls[location_id] = new List<Wall>();
  136. if (mode == Mode.Room)
  137. {
  138. for (int i = 0; i < wall.transform.childCount; i++)
  139. {
  140. var w = wall.transform.GetChild(i);
  141. if(w.gameObject.activeSelf == true)
  142. {
  143. currentWall = new Wall { id = Convert.ToUInt32(4000000000 + Walls[location_id].Count),location_id = location_id, start_width = w.position.x, start_height = w.position.z, end_width = w.localScale.x, end_height = w.localScale.z, go = wall };
  144. Walls[location_id].Add(currentWall);
  145. }
  146. }
  147. }
  148. //if (mode == Mode.Room) Rooms.Add(currentWall);
  149. if (mode == Mode.Wall)
  150. {
  151. currentWall = new Wall { id =Convert.ToUInt32(4000000000 + Walls[location_id].Count), start_width = wall.transform.position.x, start_height = wall.transform.position.z, end_width = wall.transform.localScale.x, end_height = wall.transform.localScale.z, go = wall, location_id=location_id };
  152. Debug.Log($"new wall save id {currentWall.id} x {currentWall.start_width} z {currentWall.start_height} x2 {currentWall.end_width} z2 {currentWall.end_height} location_id {currentWall.location_id}");
  153. Walls[location_id].Add(currentWall);
  154. }
  155. /*if (mode == Mode.Wall || mode == Mode.Room)*/ /*Walls.Add(currentWall);*/
  156. canvas.SetActive(false);
  157. if (mode == Mode.Zone && !EventSystem.current.IsPointerOverGameObject())
  158. {
  159. Dialog.GetComponent<Dialog>().Text.gameObject.SetActive(false);
  160. Dialog.GetComponent<Dialog>().InputField.gameObject.SetActive(true);
  161. Dialog.GetComponent<Dialog>().Button.onClick.AddListener(() =>
  162. {
  163. wall.GetComponent<ZoneController>().TextMesh.text=Dialog.GetComponent<Dialog>().InputField.text;
  164. });
  165. var z = Zones[location_id][last_zone];
  166. z.start_width = wall.transform.position.x;
  167. z.start_height = wall.transform.position.z;
  168. z.end_width = wall.transform.localScale.x;
  169. z.end_height = wall.transform.localScale.z;
  170. Debug.Log($"z {z.id} z.start_width {z.start_width} z.start_height {z.start_height}");
  171. //Zones[location_id].Insert(last_zone, z);
  172. }
  173. }
  174. public static GameObject AddWall(CompanyController company, Vector3 pos, Vector3 scale = default)
  175. {
  176. var wall = Instantiate(Resources.Load("GameObjects/Pole", typeof(GameObject))) as GameObject;
  177. wall.transform.position = pos;
  178. if(scale != null) wall.transform.localScale = scale;
  179. var location = company.locations[company.locations_index[company.active_location]].location;
  180. wall.transform.SetParent(location.transform.GetChild(1).transform);
  181. //WallObjects.Add(wall);
  182. return wall;
  183. }
  184. GameObject AddRoom()
  185. {
  186. var room = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject))) as GameObject;
  187. var location = company.locations[company.locations_index[company.active_location]].location;
  188. room.transform.SetParent(location.transform.GetChild(1).transform);
  189. LocationController.Cube(room, new Vector3(0.1f, 0.1f, 1));
  190. //for (var i = 0; i < room.transform.childCount; i++)
  191. // if (room.transform.GetChild(i).gameObject.activeSelf == true)
  192. // WallObjects.Add(room.transform.GetChild(i).gameObject);
  193. return room;
  194. }
  195. static int last_zone;
  196. public static GameObject AddZone(GameObject Dialog, CompanyController company, GameObject ZonesScroll, Zone z = null)
  197. {
  198. var location = company.locations[company.locations_index[company.active_location]].location;
  199. var location_id = company.locations_index[company.active_location];
  200. if (!Zones.ContainsKey(location_id)) Zones[location_id] = new List<Zone>();
  201. var zname = $"Зона {Zones[location_id].Count + 1}";
  202. if (z != null)
  203. zname = z.name;
  204. var zone = Instantiate(Resources.Load("GameObjects/Zone", typeof(GameObject))) as GameObject;
  205. zone.name= $"Zone_{Zones[location_id].Count + 1}";
  206. //zone.transform.position = pos;
  207. zone.transform.SetParent(location.transform.GetChild(2).transform);
  208. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  209. panel_button.transform.SetParent(ZonesScroll.transform);
  210. panel_button.name = $"Zone_{Zones.Count}";
  211. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  212. button.transform.GetChild(0).GetComponent<Text>().text = zname;
  213. zone.GetComponent<ZoneController>().TextMesh.text = zname;
  214. var active_zone = Zones[location_id].Count;
  215. if(z == null) z = new Zone { id= Convert.ToUInt32(4000000000 + Zones[location_id].Count), go=zone, buttons = panel_button, location_id= location_id };
  216. else
  217. {
  218. zone.transform.position = new Vector3(Mathf.Abs(z.start_width), 0.05f, Mathf.Abs(z.start_height));
  219. zone.transform.localScale = new Vector3(Mathf.Abs(z.end_width), 1, Mathf.Abs(z.end_height));
  220. }
  221. var button_ok = Dialog.transform.GetChild(2).GetComponent<Button>();
  222. button.onClick.AddListener(() =>
  223. {
  224. Dialog.SetActive(true);
  225. //active_zone = Zones[location_id].Count;
  226. button_ok.onClick.RemoveAllListeners();
  227. Dialog.GetComponent<Dialog>().Text.gameObject.SetActive(false);
  228. Dialog.GetComponent<Dialog>().InputField.gameObject.SetActive(true);
  229. button_ok.onClick.AddListener(() =>
  230. {
  231. var name = !Dialog.GetComponent<Dialog>().InputField.text.Equals("") ? Dialog.GetComponent<Dialog>().InputField.text : $"Зона {active_zone + 1}";
  232. button.transform.GetChild(0).GetComponent<Text>().text = name;
  233. zone.GetComponent<ZoneController>().TextMesh.text = name;
  234. Dialog.SetActive(false);
  235. z.name = name;
  236. //Zones[location_id].Insert(last_zone, z);
  237. });
  238. });
  239. var button_del = panel_button.transform.GetChild(2).GetComponent<Button>();
  240. button_del.onClick.AddListener(() =>
  241. {
  242. Destroy(zone);
  243. Destroy(panel_button);
  244. Zones[location_id].Remove(z);
  245. });
  246. Zones[location_id].Add(z);
  247. last_zone = Zones[location_id].Count - 1;
  248. return zone;
  249. }
  250. //public void SaveZones()
  251. //{
  252. // foreach(var z in Zones)
  253. // {
  254. // z.Save();
  255. // }
  256. //}
  257. static uint WallGetInnerID(uint location_id, int num)
  258. {
  259. var wall = Walls[location_id][num];
  260. return wall.id;
  261. }
  262. static uint ZoneGetInnerID(uint location_id, int num)
  263. {
  264. var zone = Zones[location_id][num];
  265. return (uint)zone.id;
  266. }
  267. }