CalMap.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /*
  6. * 1. 场景中ABCD四个Cube 对应场景内的四个角
  7. * 2. SceneRoot 位置默认要与A点的位置相同
  8. * 3. 场景内尺寸比例根据图片比例进行动态的修改
  9. * 4. 所有的计算和同步都是在开头的沙盘播放结束后进行
  10. *
  11. */
  12. /// <summary>
  13. /// 计算人物在地图上的位置
  14. /// </summary>
  15. public class CalMap : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// 场景中的Player
  19. /// </summary>
  20. public Transform player;
  21. [SerializeField]
  22. private MinMap m_Map;
  23. private void LateUpdate()
  24. {
  25. if (OpenXRCamera.Instance.head != null)
  26. {
  27. if (player != null && GameManager.Instance.isStart)
  28. {
  29. float rot = player.localEulerAngles.y;
  30. var pos = new Vector3(Math.Abs(player.localPosition.x) / GameManager.Instance.MapSize.x * GameManager.Instance.MinMapSize.x,
  31. -(GameManager.Instance.MinMapSize.y- (Math.Abs(player.localPosition.z) / GameManager.Instance.MapSize.y * GameManager.Instance.MinMapSize.y)), 0);
  32. if (pos.x < 0)
  33. {
  34. pos.x = 0;
  35. }
  36. if (pos.x > GameManager.Instance.MinMapSize.x)
  37. {
  38. pos.x = GameManager.Instance.MinMapSize.x;
  39. }
  40. if (pos.y > 0)
  41. {
  42. pos.y = 0;
  43. }
  44. if (pos.y < -GameManager.Instance.MinMapSize.y)
  45. {
  46. pos.y = -GameManager.Instance.MinMapSize.y;
  47. }
  48. m_Map.ShowPlayer(pos, rot);
  49. }
  50. }
  51. }
  52. public void SettingMapPoint(List<Transform> list_Pos)
  53. {
  54. MapPoint(list_Pos);
  55. }
  56. /// <summary>
  57. /// 设置小地图路线
  58. /// </summary>
  59. /// <param name="data"></param>
  60. public void SetLines(NavLinesItem data)
  61. {
  62. if (data == null)
  63. return;
  64. List<Vector3> pos = new List<Vector3>();
  65. for (int j = 0; j < data.lines.Count; j++)
  66. {
  67. var line = data.lines[j];
  68. Vector3 startPos = new Vector3((float)line.x1 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MinMapSize.x, -(float)line.y1 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MinMapSize.y, 0);
  69. Vector3 endPos = new Vector3((float)line.x2 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MinMapSize.x, -(float)line.y2 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MinMapSize.y, 0);
  70. pos.Add(startPos);
  71. if (j == data.lines.Count - 1)
  72. {
  73. pos.Add(endPos);
  74. }
  75. }
  76. m_Map.SettingMapRoute(pos, data.name);
  77. }
  78. /// <summary>
  79. /// 计算场景水晶点位置
  80. /// </summary>
  81. private void MapPoint(List<Transform> listPoint)
  82. {
  83. if (listPoint.Count <= 0)
  84. return;
  85. List<Vector3> Points = new List<Vector3>();
  86. for (int i = 0; i < listPoint.Count; i++)
  87. {
  88. var pos = listPoint[i].localPosition;
  89. float x = Mathf.Abs(pos.x) / GameManager.Instance.MapSize.x * GameManager.Instance.MinMapSize.x;
  90. float y = Mathf.Abs(pos.z) / GameManager.Instance.MapSize.y * GameManager.Instance.MinMapSize.y;
  91. Points.Add(new Vector3(x, y, 0));
  92. }
  93. m_Map.ShowPoint(Points);
  94. }
  95. }