Zone.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // имя, id, координаты углов, id локации
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class Zone
  7. {
  8. public uint id { get; set; }
  9. public uint location_id { get; set; }
  10. public string name = "";
  11. public float start_width { get; set; }
  12. public float start_height { get; set; }
  13. public float end_width { get; set; }
  14. public float end_height { get; set; }
  15. public GameObject go;
  16. public GameObject buttons;
  17. public void Save()
  18. {
  19. List<byte> list = new List<byte>();
  20. list.AddRange(BitConverter.GetBytes(id));
  21. list.AddRange(BitConverter.GetBytes(start_width));
  22. list.AddRange(BitConverter.GetBytes(start_height));
  23. list.AddRange(BitConverter.GetBytes(end_width));
  24. list.AddRange(BitConverter.GetBytes(end_height));
  25. list.AddRange(BitConverter.GetBytes(location_id));
  26. var bname = System.Text.Encoding.UTF8.GetBytes(name);
  27. var namelen = (ushort) bname.Length;
  28. list.AddRange(BitConverter.GetBytes(namelen));
  29. list.AddRange(bname);
  30. Debug.Log($"zone save id {id} x {start_width} z {start_height} x2 {end_width} z2 {end_height} location_id {location_id} namelen {namelen} name {name}");
  31. var tosend = Client.ConstructVariablePacket(53, list.ToArray());
  32. Client.SendEnqueue(tosend);
  33. }
  34. public static void SaveAll(uint lid)
  35. {
  36. if (WallCreate.Zones.Count > 0)
  37. {
  38. List<byte> list = new List<byte>();
  39. foreach (var z in WallCreate.Zones[lid])
  40. {
  41. list.AddRange(BitConverter.GetBytes(z.id));
  42. list.AddRange(BitConverter.GetBytes(z.start_width));
  43. list.AddRange(BitConverter.GetBytes(z.start_height));
  44. list.AddRange(BitConverter.GetBytes(z.end_width));
  45. list.AddRange(BitConverter.GetBytes(z.end_height));
  46. list.AddRange(BitConverter.GetBytes(z.location_id));
  47. var bname = System.Text.Encoding.UTF8.GetBytes(z.name);
  48. var namelen = (ushort)bname.Length;
  49. list.AddRange(BitConverter.GetBytes(namelen));
  50. list.AddRange(bname);
  51. Debug.Log($"zone save id {z.id} x {z.start_width} z {z.start_height} x2 {z.end_width} z2 {z.end_height}");
  52. }
  53. var tosend = Client.ConstructVariablePacket(53, list.ToArray());
  54. Client.SendEnqueue(tosend);
  55. }
  56. }
  57. public static void ReceiveZones(byte[] bytedata)
  58. {
  59. int read = 5;
  60. var company = CompanyController.instance;
  61. uint location_id = 0;
  62. while (read < bytedata.Length)
  63. {
  64. var id = BitConverter.ToUInt32(bytedata, read);
  65. var x1 = BitConverter.ToSingle(bytedata, read + 4);
  66. var z1 = BitConverter.ToSingle(bytedata, read + 8);
  67. var x2 = BitConverter.ToSingle(bytedata, read + 12);
  68. var z2 = BitConverter.ToSingle(bytedata, read + 16);
  69. var lid = BitConverter.ToUInt32(bytedata, read + 20);
  70. var namelen = BitConverter.ToUInt16(bytedata, read + 24);
  71. Console.WriteLine($"namelen {namelen}");
  72. //var name = "";
  73. var name = $"Зона {company.locations[lid].zones.Count + 1}";
  74. if (namelen > 0)
  75. {
  76. name = System.Text.Encoding.UTF8.GetString(bytedata, read + 26, namelen);
  77. }
  78. Debug.Log($"zone received id {id} x1 {x1} z1 {z1} x2 {x2} z2 {z2} location {lid} name {name}");
  79. read += 26+namelen;
  80. company.locations[lid].zones.Add(new Zone { id = id, location_id = lid, name = name, start_width = x1, start_height = z1, end_width= x2, end_height = z2});
  81. location_id = lid;
  82. }
  83. if (location_id != 0)
  84. {
  85. Debug.Log($"location {location_id} zones count {company.locations[location_id].zones.Count}");
  86. company.load_location_elements[location_id].zones = true;
  87. }
  88. }
  89. public static void LoadByLocationId(uint locId)
  90. {
  91. CompanyController.instance.locations[locId].zones = new List<Zone>();
  92. Client.SendFourByteParamPacket(52, locId);
  93. }
  94. /// <summary>
  95. /// Перевод позиции и масштаба в координаты левой нижней (x1;z1) и правой верхней (x2;z2) точек
  96. /// </summary>
  97. /// <returns></returns>
  98. public List<float> Coordinates()
  99. {
  100. var coord = new List<float>();
  101. //var wall = WallCreate.AddWall(this,
  102. // new Vector3(w.start_width, WallCreate.WallHeight / 2, w.start_height),
  103. // new Vector3(Mathf.Abs(w.end_width), WallCreate.WallHeight, Mathf.Abs(w.end_height)));
  104. var x1 = start_width - (end_width * 10 / 2);
  105. coord.Add(x1); //x1
  106. var x2 = start_height - (end_height * 10 / 2);
  107. coord.Add(x2); //z1
  108. var z1 = start_width + (end_width * 10 / 2);
  109. coord.Add( z1); //x2
  110. var z2 = start_height + (end_height * 10 / 2);
  111. coord.Add(z2); //z2
  112. return coord;
  113. }
  114. }