WallCreate.cs 14 KB

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