SpawnPoint.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class SpawnPoint : MonoBehaviour
  4. {
  5. public List<MonsterSpawnLoc> spawnLocs = new List<MonsterSpawnLoc>();
  6. [SerializeField]
  7. public SpawnPointType type = SpawnPointType.Monster; //出生点类型:0
  8. private GameObject transferGo = null;
  9. public GameObject TransferGo
  10. {
  11. get { return transferGo; }
  12. }
  13. public float fRandomValue = 1;
  14. public float fHorSpace = 1.0f;
  15. public float fVerSpace = 1.0f;
  16. public float fActorReadyDist = 3;
  17. public Vector3 transferPos;
  18. public Vector3 transferRot;
  19. public Vector3 camPos;
  20. public Vector3 camRot;
  21. public float camFar;
  22. public List<int> npcIds = new List<int>();
  23. public void SavePoint()
  24. {
  25. //DebugHelper.LogError("npcIds:" + npcIds.Count);
  26. int pointCnt = transform.childCount;
  27. spawnLocs = new List<MonsterSpawnLoc>();
  28. for (int idx = 0; idx < pointCnt; idx++)
  29. {
  30. Transform child = transform.Find("p" + (idx + 1));
  31. if (child != null)
  32. {
  33. MonsterSpawnLoc loc = new MonsterSpawnLoc();
  34. loc.pos = child.position;
  35. loc.rot = child.eulerAngles;
  36. if (idx <= npcIds.Count - 1)
  37. loc.npcId = npcIds[idx];
  38. else
  39. loc.npcId = 0;
  40. spawnLocs.Add(loc);
  41. }
  42. }
  43. Transform transferTrans = transform.Find("transfer");
  44. if (transferTrans != null)
  45. {
  46. if (type == SpawnPointType.Boss)
  47. {
  48. transferPos = transferTrans.position;
  49. transferRot = transferTrans.eulerAngles;
  50. }
  51. else
  52. {
  53. GameObject.Destroy(transferTrans.gameObject);
  54. transferGo = null;
  55. }
  56. }
  57. }
  58. public void RandomPos()
  59. {
  60. if (fRandomValue < 1) return;
  61. for (int idx = 0; idx < 2; idx++)
  62. {
  63. int step = idx == 0 ? 1 : -1;
  64. for (int jdx = 0; jdx < 3; jdx++)
  65. {
  66. int cursor = (idx * 3 + jdx) + 1;
  67. Vector3 pos = Vector3.forward * step * fVerSpace + Vector3.right * (jdx - 1) * fHorSpace;
  68. if (cursor != 2)
  69. {
  70. pos.x = pos.x + Random.Range(-fRandomValue, fRandomValue);
  71. pos.z = pos.z + Random.Range(-fRandomValue, fRandomValue);
  72. }
  73. Transform child = transform.Find("p" + cursor);
  74. if (child != null)
  75. child.localPosition = pos;
  76. }
  77. }
  78. SavePoint();
  79. }
  80. public void CalcPointPos()
  81. {
  82. for(int idx =0; idx < 2; idx++)
  83. {
  84. int step = idx == 0 ? 1 : -1;
  85. for(int jdx =0; jdx < 3; jdx++)
  86. {
  87. Vector3 pos = Vector3.forward * step * fVerSpace + Vector3.right* (jdx -1) * fHorSpace;
  88. int cursor = (idx * 3 + jdx)+1;
  89. Transform child = transform.Find("p" + cursor);
  90. if (child != null)
  91. child.localPosition = pos;
  92. }
  93. }
  94. SavePoint();
  95. }
  96. public void AddTransferPoint()
  97. {
  98. Vector3 transferPos = this.transform.position + this.transform.forward * 5;
  99. if (transferGo == null)
  100. {
  101. transferGo = new GameObject("transfer");
  102. transferGo.transform.SetParent(this.transform);
  103. }
  104. transferGo.transform.position = transferPos;
  105. }
  106. public void DeleteTransferPoint()
  107. {
  108. Transform transferTrans = transform.Find("transfer");
  109. if (transferTrans != null)
  110. {
  111. GameObject.DestroyImmediate(transferTrans.gameObject);
  112. transferGo = null;
  113. }
  114. }
  115. #if UNITY_EDITOR
  116. private void OnDrawGizmos()
  117. {
  118. for (int jdx = 0; jdx < this.transform.childCount; jdx++)
  119. {
  120. Transform child = this.transform.GetChild(jdx);
  121. DrawPoint(child.position,child.rotation);
  122. }
  123. }
  124. void DrawPoint(Vector3 pos,Quaternion rot)
  125. {
  126. Gizmos.color = Color.red;
  127. Gizmos.DrawSphere(pos,0.1f);
  128. UnityEditor.Handles.color = Color.blue;
  129. UnityEditor.Handles.ArrowHandleCap(0, pos, rot, transform.localScale.z,EventType.MouseDown);
  130. UnityEditor.Handles.Disc(rot, pos, Vector3.up, transform.localScale.z * 0.5f, false, 1);
  131. }
  132. #endif
  133. }