Wall.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. coord.Add(end_width - (start_width/2));
  89. coord.Add(end_height - (start_height / 2));
  90. coord.Add(end_width + (start_width/2));
  91. coord.Add(end_height + (start_height / 2));
  92. return coord;
  93. }
  94. /// <summary>
  95. /// Перевод координат левой нижней (x1;z1) и правой верхней (x2;z2) точек
  96. /// в позицию и масштаба для Vector3
  97. /// </summary>
  98. /// <param name="x1"></param>
  99. /// <param name="z1"></param>
  100. /// <param name="x2"></param>
  101. /// <param name="z2"></param>
  102. /// <returns></returns>
  103. public List<float> Coordinates(float x1, float z1, float x2, float z2)
  104. {
  105. var coord = new List<float>();
  106. // scale
  107. var x = Math.Abs(x2 - x1);
  108. var z = Math.Abs(z2 - z1);
  109. coord.Add(x);
  110. coord.Add(z);
  111. // pos
  112. coord.Add(x/2);
  113. coord.Add(z/2);
  114. return coord;
  115. }
  116. public List<float> Coordinates(Vector3 pos, Vector3 scale)
  117. {
  118. var coord = new List<float>();
  119. //var wall = WallCreate.AddWall(this,
  120. // new Vector3(w.start_width, WallCreate.WallHeight / 2, w.start_height),
  121. // new Vector3(Mathf.Abs(w.end_width), WallCreate.WallHeight, Mathf.Abs(w.end_height)));
  122. coord.Add(scale.x - (pos.x / 2));
  123. coord.Add(scale.z - (pos.z / 2));
  124. coord.Add(scale.x + (pos.x / 2));
  125. coord.Add(scale.z + (pos.z / 2));
  126. return coord;
  127. }
  128. }