ReadyPoint.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. public struct stLocCfg
  5. {
  6. public Vector3 heroPos;
  7. public Vector3 petPos;
  8. public stLocCfg(Vector3 hPos,Vector3 pPos)
  9. {
  10. heroPos = hPos;
  11. petPos = pPos;
  12. }
  13. public void ResetHeight(float py)
  14. {
  15. heroPos.y = py;
  16. petPos.y = py;
  17. }
  18. }
  19. public class ReadyPoint : MonoBehaviour
  20. {
  21. [Serializable]
  22. public struct ReadyPointCfg
  23. {
  24. public int order;
  25. public Transform point;
  26. public Transform petPoint;
  27. }
  28. public ReadyPointCfg[] NoviceReadyPoints;
  29. public ReadyPointCfg[] SwordReadyPoints;
  30. public ReadyPointCfg[] ThiefReadyPoints;
  31. public ReadyPointCfg[] HunterReadyPoints;
  32. public ReadyPointCfg[] MagicReadyPoints;
  33. public ReadyPointCfg[] PriestReadyPoints;
  34. stLocCfg[] novicePosList = null;
  35. stLocCfg[] swordPosList = null;
  36. stLocCfg[] thiefPosList = null;
  37. stLocCfg[] hunterPosList = null;
  38. stLocCfg[] magicPosList = null;
  39. stLocCfg[] priestPosList = null;
  40. stLocCfg[] initedNovicePosList = null;
  41. stLocCfg[] initedSwordPosList = null;
  42. stLocCfg[] initedThiefPosList = null;
  43. stLocCfg[] initedHunterPosList = null;
  44. stLocCfg[] initedMagicPosList = null;
  45. stLocCfg[] initedPriestPosList = null;
  46. Vector3 mOffset = Vector3.zero;
  47. Vector3 mCenter = Vector3.zero;
  48. public Vector3 Center
  49. {
  50. get { return mCenter; }
  51. }
  52. private void Start()
  53. {
  54. InitPos();
  55. CalcRelativePos();
  56. }
  57. void InitPos()
  58. {
  59. if(NoviceReadyPoints != null)
  60. {
  61. novicePosList = new stLocCfg[NoviceReadyPoints.Length];
  62. for (int idx = 0; idx < novicePosList.Length; idx++)
  63. {
  64. ReadyPointCfg pointCfg = NoviceReadyPoints[idx];
  65. Vector3 hPos = pointCfg.point!=null? pointCfg.point.position : Vector3.zero;
  66. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  67. novicePosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  68. }
  69. }
  70. if (SwordReadyPoints != null)
  71. {
  72. swordPosList = new stLocCfg[SwordReadyPoints.Length];
  73. for(int idx =0; idx < SwordReadyPoints.Length; idx++)
  74. {
  75. ReadyPointCfg pointCfg = SwordReadyPoints[idx];
  76. Vector3 hPos = pointCfg.point != null ? pointCfg.point.position : Vector3.zero;
  77. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  78. swordPosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  79. }
  80. }
  81. if (ThiefReadyPoints != null)
  82. {
  83. thiefPosList = new stLocCfg[ThiefReadyPoints.Length];
  84. for(int idx =0; idx < ThiefReadyPoints.Length; idx++)
  85. {
  86. ReadyPointCfg pointCfg = ThiefReadyPoints[idx];
  87. Vector3 hPos = pointCfg.point != null ? pointCfg.point.position : Vector3.zero;
  88. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  89. thiefPosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  90. }
  91. }
  92. if (HunterReadyPoints != null)
  93. {
  94. hunterPosList = new stLocCfg[HunterReadyPoints.Length];
  95. for(int idx =0; idx < HunterReadyPoints.Length; idx++)
  96. {
  97. ReadyPointCfg pointCfg = HunterReadyPoints[idx];
  98. Vector3 hPos = pointCfg.point != null ? pointCfg.point.position : Vector3.zero;
  99. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  100. hunterPosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  101. }
  102. }
  103. if (MagicReadyPoints != null)
  104. {
  105. magicPosList = new stLocCfg[MagicReadyPoints.Length];
  106. for(int idx = 0; idx < MagicReadyPoints.Length; idx++)
  107. {
  108. ReadyPointCfg pointCfg = MagicReadyPoints[idx];
  109. Vector3 hPos = pointCfg.point != null ? pointCfg.point.position : Vector3.zero;
  110. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  111. magicPosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  112. }
  113. }
  114. if (PriestReadyPoints != null)
  115. {
  116. priestPosList = new stLocCfg[PriestReadyPoints.Length];
  117. for(int idx =0; idx < PriestReadyPoints.Length; idx++)
  118. {
  119. ReadyPointCfg pointCfg = PriestReadyPoints[idx];
  120. Vector3 hPos = pointCfg.point != null ? pointCfg.point.position : Vector3.zero;
  121. Vector3 pPos = pointCfg.petPoint != null ? pointCfg.petPoint.position : Vector3.zero;
  122. priestPosList[pointCfg.order - 1] = new stLocCfg(hPos, pPos);
  123. }
  124. }
  125. }
  126. void CalcRelativePos()
  127. {
  128. Vector3 hPos = Vector3.zero;;
  129. int cnt = novicePosList.Length;
  130. initedNovicePosList = new stLocCfg[novicePosList.Length];
  131. for (int idx = 0; idx < novicePosList.Length; idx++) {
  132. hPos += novicePosList[idx].heroPos;
  133. initedNovicePosList[idx] = novicePosList[idx];
  134. }
  135. cnt += swordPosList.Length;
  136. initedSwordPosList = new stLocCfg[swordPosList.Length];
  137. for (int idx = 0; idx < swordPosList.Length; idx++) {
  138. hPos += swordPosList[idx].heroPos;
  139. initedSwordPosList[idx] = swordPosList[idx];
  140. }
  141. cnt += thiefPosList.Length;
  142. initedThiefPosList = new stLocCfg[thiefPosList.Length];
  143. for (int idx = 0; idx < thiefPosList.Length; idx++) {
  144. hPos += thiefPosList[idx].heroPos;
  145. initedThiefPosList[idx] = thiefPosList[idx];
  146. }
  147. cnt += hunterPosList.Length;
  148. initedHunterPosList = new stLocCfg[hunterPosList.Length];
  149. for (int idx = 0; idx < hunterPosList.Length; idx++) {
  150. hPos += hunterPosList[idx].heroPos;
  151. initedHunterPosList[idx] = hunterPosList[idx];
  152. }
  153. cnt += magicPosList.Length;
  154. initedMagicPosList = new stLocCfg[magicPosList.Length];
  155. for (int idx = 0; idx < magicPosList.Length; idx++) {
  156. hPos += magicPosList[idx].heroPos;
  157. initedMagicPosList[idx] = magicPosList[idx];
  158. }
  159. cnt += priestPosList.Length;
  160. initedPriestPosList = new stLocCfg[priestPosList.Length];
  161. for (int idx = 0; idx < priestPosList.Length; idx++) {
  162. hPos += priestPosList[idx].heroPos;
  163. initedPriestPosList[idx] = priestPosList[idx];
  164. }
  165. mCenter = hPos / cnt;
  166. mOffset = this.transform.position - mCenter;
  167. mOffset.y = 0;
  168. }
  169. public stLocCfg GetBornPointPos(ProfessionType profession, int order)
  170. {
  171. stLocCfg[] posList = null;
  172. if (profession == ProfessionType.Pro_Type_0)
  173. posList = initedNovicePosList;
  174. else if (profession == ProfessionType.Pro_Type_Sword)
  175. posList = initedSwordPosList;
  176. else if (profession == ProfessionType.Pro_Type_Thief)
  177. posList = initedThiefPosList;
  178. else if (profession == ProfessionType.Pro_Type_Hunter)
  179. posList = initedHunterPosList;
  180. else if (profession == ProfessionType.Pro_Type_Magic)
  181. posList = initedMagicPosList;
  182. else if (profession == ProfessionType.Pro_Type_Priest)
  183. posList = initedPriestPosList;
  184. if (posList != null && order >= 1 && order <= posList.Length)
  185. {
  186. return posList[order - 1];
  187. }
  188. return default(stLocCfg);
  189. }
  190. public stLocCfg GetReadyPointPos(ProfessionType profession,int order)
  191. {
  192. stLocCfg[] posList = null;
  193. if(profession == ProfessionType.Pro_Type_0)
  194. posList = novicePosList;
  195. else if (profession == ProfessionType.Pro_Type_Sword)
  196. posList = swordPosList;
  197. else if (profession == ProfessionType.Pro_Type_Thief)
  198. posList = thiefPosList;
  199. else if (profession == ProfessionType.Pro_Type_Hunter)
  200. posList = hunterPosList;
  201. else if (profession == ProfessionType.Pro_Type_Magic)
  202. posList = magicPosList;
  203. else if (profession == ProfessionType.Pro_Type_Priest)
  204. posList = priestPosList;
  205. if (posList != null && order>=1 && order <= posList.Length)
  206. {
  207. return posList[order - 1];
  208. }
  209. return default(stLocCfg);
  210. }
  211. public void CalcNextBattleFieldPoints(Vector3 center,Vector3 rot)
  212. {
  213. //DebugHelper.LogError("CalcNextBattleFieldPoints:" + center);
  214. mCenter = center;
  215. this.transform.position = mCenter;
  216. this.transform.rotation = Quaternion.Euler(rot);
  217. InitPos();
  218. ResetHeight();
  219. }
  220. void ResetHeight()
  221. {
  222. for (int idx = 0; idx < novicePosList.Length; idx++)
  223. novicePosList[idx].ResetHeight(mCenter.y);
  224. for (int idx = 0; idx < swordPosList.Length; idx++)
  225. swordPosList[idx].ResetHeight(mCenter.y);
  226. for (int idx = 0; idx < thiefPosList.Length; idx++)
  227. thiefPosList[idx].ResetHeight(mCenter.y);
  228. for (int idx = 0; idx < hunterPosList.Length; idx++)
  229. hunterPosList[idx].ResetHeight(mCenter.y);
  230. for (int idx = 0; idx < magicPosList.Length; idx++)
  231. magicPosList[idx].ResetHeight(mCenter.y);
  232. for (int idx = 0; idx < priestPosList.Length; idx++)
  233. priestPosList[idx].ResetHeight(mCenter.y);
  234. }
  235. public void Dispose()
  236. {
  237. swordPosList = null;
  238. thiefPosList = null;
  239. hunterPosList = null;
  240. magicPosList = null;
  241. priestPosList = null;
  242. }
  243. #if UNITY_EDITOR
  244. private void OnDrawGizmos()
  245. {
  246. for (int jdx = 0; jdx < this.transform.childCount; jdx++)
  247. {
  248. Transform child = this.transform.GetChild(jdx);
  249. if(child.gameObject.activeInHierarchy)
  250. DrawPoint(child.position, child.rotation);
  251. }
  252. }
  253. void DrawPoint(Vector3 pos, Quaternion rot)
  254. {
  255. Gizmos.color = Color.red;
  256. Gizmos.DrawSphere(pos, 0.1f);
  257. UnityEditor.Handles.color = Color.green;
  258. UnityEditor.Handles.ArrowHandleCap(0, pos, rot, transform.localScale.z,EventType.MouseDown);
  259. UnityEditor.Handles.Disc(rot, pos, Vector3.up, transform.localScale.z * 0.5f, false, 1);
  260. }
  261. #endif
  262. }