Location.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using UnityEngine;
  8. public class Location
  9. {
  10. public uint company_id;
  11. public uint id { get; set; }
  12. public string name { get; set; }
  13. public List<Wall> walls { get; set; }
  14. public List<Beacon> beacons { get; set; }
  15. public GameObject plane { get; set; }
  16. public string texture_url { get; set; }
  17. public List<Zone> zones { get; set; }
  18. public GameObject location { get; set; }
  19. public static List<string> contents = new List<string> { "Walls", "Zones", "Beacons" };
  20. public void Save()
  21. {
  22. MemoryStream ms = new MemoryStream();
  23. BinaryWriter bw = new BinaryWriter(ms);
  24. var urlb = Encoding.UTF8.GetBytes(texture_url);
  25. ushort urllen = (ushort)urlb.Length;
  26. bw.Write(id);
  27. bw.Write(plane.transform.localScale.x);
  28. bw.Write(plane.transform.localScale.z);
  29. bw.Write(plane.transform.localPosition.x);
  30. bw.Write(plane.transform.localPosition.z);
  31. bw.Write(company_id);
  32. bw.Write(urllen);
  33. bw.Write(urlb);
  34. //ms.Write(bdata, 0, bdata.Length);
  35. //Debug.Log("imagesend bytes "+ms.ToArray().Length);
  36. var data = Client.ConstructVariablePacket(56, ms.ToArray());
  37. Client.SendEnqueue(data);
  38. }
  39. public static void TextureGetURL(byte[] bytedata)
  40. {
  41. var loc_id = BitConverter.ToUInt32(bytedata, 5);
  42. var urllen = bytedata.Length - 9;
  43. var loc = CompanyController.instance.GetLocation(loc_id);
  44. var url = Encoding.UTF8.GetString(bytedata, 9, urllen);
  45. loc.texture_url = url;
  46. loc.Save();
  47. //Debug.Log("get url " + url+" loc_id "+loc.id);
  48. }
  49. public void SaveTexture(byte[] img)
  50. {
  51. Debug.Log("SaveTexture");
  52. MemoryStream ms = new MemoryStream();
  53. BinaryWriter bw = new BinaryWriter(ms);
  54. bw.Write(id);
  55. bw.Write(img);
  56. Client.SendEnqueue(Client.ConstructVariablePacket(57, ms.ToArray()));
  57. }
  58. public void SaveContents()
  59. {
  60. foreach (Wall wall in walls)
  61. {
  62. wall.Save();
  63. }
  64. foreach (var z in zones)
  65. {
  66. z.Save();
  67. }
  68. Beacon.Save(beacons);
  69. }
  70. public void Load()
  71. {
  72. Debug.Log("Location.Load");
  73. Zone.LoadByLocationId(id);
  74. Wall.LoadByLocationId(id);
  75. Beacon.Request(id);
  76. //Client.instance.SendGetUsers();
  77. }
  78. public static void LocationsRequest(uint companyId=0)
  79. {
  80. if (companyId == 0)
  81. companyId = Client.instance.company_id;
  82. Client.SendFourByteParamPacket(54, companyId);
  83. }
  84. public static void ReceiveLocations(byte[] bytedata)
  85. {
  86. var company = CompanyController.instance;
  87. company.locations = new Dictionary<uint, Location>();
  88. company.locations_index = new List<uint>();
  89. int read = 5;
  90. while (read < bytedata.Length)
  91. {
  92. var id = BitConverter.ToUInt32(bytedata, read);
  93. var x = BitConverter.ToSingle(bytedata, read + 4);
  94. var y = BitConverter.ToSingle(bytedata, read + 8);
  95. var scalex = BitConverter.ToSingle(bytedata, read + 12);
  96. var scalez = BitConverter.ToSingle(bytedata, read + 16);
  97. var posx = BitConverter.ToSingle(bytedata, read + 20);
  98. var posz = BitConverter.ToSingle(bytedata, read + 24);
  99. var lenimg = BitConverter.ToUInt16(bytedata, read + 28);
  100. var img = Encoding.UTF8.GetString(bytedata, read + 30, lenimg);
  101. var lenname = BitConverter.ToUInt16(bytedata, read + 30 + lenimg);
  102. var name = Encoding.UTF8.GetString(bytedata, read + 32 + lenimg, lenname);
  103. var company_id = BitConverter.ToUInt32(bytedata, read + 32 + lenimg + lenname);
  104. read += (36 + lenimg + lenname);
  105. Debug.Log($"location received company_id {company_id} read {read} id {id} x {x} y {y} scalex {scalex} scalez {scalez} posx {posx} posz {posz} lenimg {lenimg} img {img} lenname {lenname} name {name}");
  106. var plane = UnityEngine.Object.Instantiate(Resources.Load("GameObjects/Plane", typeof(GameObject))) as GameObject;
  107. if (scalex == 0) scalex = 1; if (scalez == 0) scalez = 1;
  108. plane.transform.localScale = new Vector3(scalex, WallCreate.WallHeight, scalez);
  109. if (posx == 0) posx = scalex / 2; if (posz == 0) posz = scalez / 2;
  110. plane.transform.position = new Vector3(posx, 0.01f, posz);
  111. var Location = new GameObject();
  112. Location.name = name;
  113. plane.transform.SetParent(Location.transform);
  114. foreach (var g in contents)
  115. {
  116. var go = new GameObject();
  117. go.name = g;
  118. go.transform.SetParent(Location.transform);
  119. }
  120. company.locations[id] =new Location { id = id, name = name, texture_url = img, location = Location, plane = plane, company_id = company_id };
  121. company.locations_index.Add(id);
  122. Debug.Log("locations count "+ company.locations.Count);
  123. }
  124. company.load_location = true;
  125. }
  126. public void LocationActive(bool active)
  127. {
  128. location.SetActive(active);
  129. if (beacons != null)
  130. foreach (var b in beacons)
  131. if (b.panel_button != null)
  132. b.panel_button.SetActive(active);
  133. if (zones != null)
  134. foreach (var z in zones)
  135. if (z.buttons != null)
  136. z.buttons.SetActive(active);
  137. }
  138. }