CompanyController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class CompanyController : MonoBehaviour
  7. {
  8. public static CompanyController instance;
  9. public Button ButtonStatus;
  10. public GameObject Player;
  11. public GameObject Editor;
  12. public Dropdown DropdownCompany;
  13. public Dropdown DropdownLocation;
  14. public GameObject Dialog;
  15. public GameObject ZonesScroll;
  16. public Toggle ToggleBeacons;
  17. public Toggle ToggleZones;
  18. public int active_company = -1;
  19. public int active_location = -1;
  20. public GameObject Locations;
  21. public Dictionary<uint, Load> load_location_elements = new Dictionary<uint, Load>(); // флаги загрузки элементов локации
  22. public bool load_location = false;
  23. public Dictionary<uint, Location> locations; // список локаций
  24. public List<uint> locations_index; // сопоставление id с dropdown
  25. //public List<Marker> markers;
  26. PlayerController player;
  27. EditorController editor;
  28. Client client;
  29. private void Awake()
  30. {
  31. instance = this;
  32. }
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. player = Player.GetComponent<PlayerController>();
  37. editor = Editor.GetComponent<EditorController>();
  38. client = Client.instance;
  39. DropdownCompany.options.Add(new Dropdown.OptionData("Тайшет"));
  40. DropdownCompany.options.Add(new Dropdown.OptionData("Тестовая"));
  41. DropdownCompany.options.Add(new Dropdown.OptionData("Братское"));
  42. locations = new Dictionary<uint, Location>();
  43. locations_index = new List<uint>();
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. if (active_company != DropdownCompany.value && AuthorizationController.success)
  49. ChangeCompany();
  50. if (active_location != DropdownLocation.value && AuthorizationController.success && locations.Any())
  51. ChangeLocation();
  52. if (active_location >= 0 && locations_index.Any())
  53. if (AuthorizationController.success && load_location_elements.ContainsKey(locations_index[active_location]))
  54. {
  55. if (load_location_elements[locations_index[active_location]].location)
  56. {
  57. var l = locations[locations_index[active_location]];
  58. LocationContents(l);
  59. }
  60. BeaconsOnOff();
  61. ZonesOnOff();
  62. }
  63. if (load_location)
  64. {
  65. load_location = false;
  66. DropdownLocation.options.Clear();
  67. DropdownLocation.options = new List<Dropdown.OptionData>();
  68. foreach (var l in locations)
  69. {
  70. l.Value.location.transform.SetParent(Locations.transform);
  71. l.Value.location.SetActive(false);
  72. DropdownLocation.options.Add(new Dropdown.OptionData($"{l.Key} {l.Value.name}"));
  73. load_location_elements[l.Key] = new Load();
  74. load_location_elements[l.Key].location = true;
  75. l.Value.Load();
  76. }
  77. if (ModeController.editor && editor.newLocation != null)
  78. {
  79. var l = editor.newLocation;
  80. //l.location.transform.SetParent(Locations.transform);
  81. l.location.transform.SetSiblingIndex(Locations.transform.childCount - 1);
  82. l.location.SetActive(false);
  83. DropdownLocation.options.Add(new Dropdown.OptionData($"Новая локация"));
  84. locations_index.Add(l.id);
  85. locations[l.id] = l;
  86. }
  87. DropdownLocation.value = 0;
  88. DropdownLocation.RefreshShownValue();
  89. active_location = DropdownLocation.value;
  90. //active_location = -1;
  91. ChangeLocation();
  92. }
  93. if(client.connected)
  94. ButtonStatus.GetComponent<Image>().color = Color.green;
  95. else
  96. ButtonStatus.GetComponent<Image>().color = Color.red;
  97. }
  98. /// <summary>
  99. /// Удаление сотрудников из-за смены компании
  100. /// Отчистка Locations
  101. ///
  102. /// </summary>
  103. public void ChangeCompany()
  104. {
  105. foreach (var l in locations)
  106. {
  107. if (l.Value.id < (uint)int.MaxValue)
  108. {
  109. Destroy(l.Value.location);
  110. foreach (var b in l.Value.beacons)
  111. Destroy(b.panel_button);
  112. foreach (var z in l.Value.zones)
  113. Destroy(z.buttons);
  114. load_location_elements.Remove(l.Value.id);
  115. }
  116. }
  117. active_company = DropdownCompany.value;
  118. Client.instance.company_id = (uint)(active_company + 1);
  119. if (player.markers != null)
  120. foreach (var m in player.markers)
  121. {
  122. Destroy(m.marker.gameObject);
  123. Destroy(m.marker_line.gameObject);
  124. Destroy(m.toggle_user);
  125. }
  126. Client.instance.SendGetUsers();
  127. player.markers = new List<Marker>();
  128. Location.LocationsRequest(0);
  129. }
  130. float sizex = 0;
  131. float sizey = 0;
  132. public void ChangeLocation()
  133. {
  134. Debug.Log("PlayerController locations count " + locations.Count + " active " + active_location);
  135. if (active_location >= 0)
  136. locations[locations_index[active_location]].LocationActive(false);
  137. active_location = DropdownLocation.value;
  138. var l = locations[locations_index[active_location]];
  139. l.LocationActive(true);
  140. if (!l.texture_url.Equals("")) StartCoroutine(SendingFormController.LoadImage(l.texture_url, l.plane, l.plane.transform.position, l.plane.transform.localScale));
  141. editor.Name.text = l.name;
  142. editor.Image.text = l.texture_url;
  143. editor.EditLocation = l;
  144. if (l.plane.transform.localScale.x != sizex)
  145. {
  146. editor.SizeX.text = $"{l.plane.transform.localScale.x}";
  147. sizex = l.plane.transform.localScale.x;
  148. }
  149. if (l.plane.transform.localScale.z != sizey)
  150. {
  151. editor.SizeY.text = $"{l.plane.transform.localScale.z}";
  152. sizey = l.plane.transform.localScale.z;
  153. }
  154. }
  155. public void LocationContents(Location location)
  156. {
  157. if (load_location_elements.ContainsKey(location.id))
  158. {
  159. if (load_location_elements[location.id].walls)
  160. {
  161. load_location_elements[location.id].walls = false;
  162. //var walls = location.Walls ?? new GameObject();
  163. //walls.name = "Walls";
  164. //walls.transform.SetParent(location.location.transform);
  165. var walls = location.location.transform.GetChild(1);
  166. foreach (var w in location.walls)
  167. {
  168. var wall = WallCreate.AddWall(this,
  169. new Vector3(w.start_width, WallCreate.WallHeight / 2, w.start_height),
  170. new Vector3(Mathf.Abs(w.end_width), WallCreate.WallHeight, Mathf.Abs(w.end_height)));
  171. wall.transform.SetParent(walls.transform);
  172. }
  173. }
  174. if (load_location_elements[location.id].beacons)
  175. {
  176. load_location_elements[location.id].beacons = false;
  177. //var beacons = location.Beacons ?? new GameObject();
  178. //beacons.name = "Beacons";
  179. //beacons.transform.SetParent(location.location.transform);
  180. foreach (var b in location.beacons)
  181. {
  182. editor.AddBeacon(this, b);
  183. if (ModeController.editor == true) b.beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
  184. }
  185. }
  186. if (load_location_elements[location.id].zones)
  187. {
  188. load_location_elements[location.id].zones = false;
  189. //var zones = location.Zones ?? new GameObject();
  190. //zones.name = "Zones";
  191. //zones.transform.SetParent(location.location.transform);
  192. foreach (var z in location.zones)
  193. z.go = WallCreate.AddZone(Dialog, this, ZonesScroll, z);
  194. }
  195. }
  196. }
  197. public Location GetActiveLocation()
  198. {
  199. //if (locations.Count > 0 && locations_index.Count > 0 && locations_index.Contains((uint)active_location))
  200. // if (locations.ContainsKey(locations_index[active_location]))
  201. return locations[locations_index[active_location]];
  202. //return null;
  203. }
  204. public Location GetLocation(uint id)
  205. {
  206. if (locations.Count > 0)
  207. if (locations.ContainsKey(id))
  208. return locations[id];
  209. return null;
  210. }
  211. public void ErrorDialogClose()
  212. {
  213. Dialog.SetActive(false);
  214. }
  215. bool beacons_view;
  216. public void BeaconsOnOff()
  217. {
  218. if (beacons_view != ToggleBeacons.isOn)
  219. {
  220. foreach (var b in locations[locations_index[active_location]].beacons)
  221. b.beacon.SetActive(ToggleBeacons.isOn);
  222. beacons_view = ToggleBeacons.isOn;
  223. }
  224. }
  225. bool zones_view;
  226. public void ZonesOnOff()
  227. {
  228. if (zones_view != ToggleZones.isOn)
  229. {
  230. foreach (var z in locations[locations_index[active_location]].zones)
  231. z.go.SetActive(ToggleZones.isOn);
  232. zones_view = ToggleZones.isOn;
  233. }
  234. }
  235. }