CalMap.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, -Math.Abs(player.localPosition.z) / GameManager.Instance.MapSize.y * GameManager.Instance.MinMapSize.y, 0);
  31. if (player.localPosition.x < 0 || player.localPosition.z > 0)
  32. {
  33. return;
  34. }
  35. // Debug.Log(pos);
  36. m_Map.ShowPlayer(pos, rot);
  37. }
  38. }
  39. }
  40. public void SettingMapPoint(List<Transform> list_Pos)
  41. {
  42. MapPoint(list_Pos);
  43. }
  44. /// <summary>
  45. /// 设置小地图路线
  46. /// </summary>
  47. /// <param name="data"></param>
  48. public void SetLines(NavLinesItem data)
  49. {
  50. if (data == null)
  51. return;
  52. List<Vector3> pos = new List<Vector3>();
  53. for (int j = 0; j < data.lines.Count; j++)
  54. {
  55. var line = data.lines[j];
  56. 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);
  57. 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);
  58. pos.Add(startPos);
  59. if (j == data.lines.Count - 1)
  60. {
  61. pos.Add(endPos);
  62. }
  63. }
  64. m_Map.SettingMapRoute(pos, data.name);
  65. }
  66. /// <summary>
  67. /// 计算场景水晶点位置
  68. /// </summary>
  69. private void MapPoint(List<Transform> listPoint)
  70. {
  71. if (listPoint.Count <= 0)
  72. return;
  73. List<Vector3> Points = new List<Vector3>();
  74. for (int i = 0; i < listPoint.Count; i++)
  75. {
  76. var pos = listPoint[i].localPosition;
  77. float x = Mathf.Abs(pos.x) / GameManager.Instance.MapSize.x * GameManager.Instance.MinMapSize.x;
  78. float y = Mathf.Abs(pos.z) / GameManager.Instance.MapSize.y * GameManager.Instance.MinMapSize.y;
  79. Points.Add(new Vector3(x, y, 0));
  80. }
  81. m_Map.ShowPoint(Points);
  82. }
  83. }