Wall.cs 5.3 KB

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