CompanyController.cs 9.9 KB

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