GMap.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { _decorator, MeshRenderer, Node, SkeletalAnimation, UITransform, Vec3 } from 'cc';
  2. import { BaseExp } from '../core/base/BaseExp';
  3. import { autoBind } from '../extend/AutoBind';
  4. import { Utils } from '../utils/Utils';
  5. import { ResUtil } from '../utils/ResUtil';
  6. import { EAnimType } from './Enemy';
  7. const { ccclass, property } = _decorator;
  8. //埋伏点接口类
  9. export class AmbushPoint {
  10. //埋伏点位置节点数组
  11. public standPosArr: Vec3[] = [];
  12. //中间路径节点
  13. public roadPosArr: Vec3[] = [];
  14. //掩体点位置节点
  15. public ambushPosArr: Vec3[] = [];
  16. //逃跑点位置节点
  17. public escapePosArr: Vec3[] = [];
  18. //是否已经有人在此站位了
  19. public isVacancy: boolean = false;
  20. }
  21. @ccclass('GMap')
  22. export class GMap extends BaseExp {
  23. @autoBind({ type: Node, tooltip: "埋伏点配置节点" })
  24. public ambush_points: Node = null;
  25. @autoBind({type:Node,tooltip:"敌人出生区域"})
  26. public enemy_born_area: Node = null;
  27. @autoBind({type:Node,tooltip:"进门内线,用于敌人是否在门内"})
  28. public miidle_inline: Node = null;
  29. @autoBind({type:Node,tooltip:"进门外线,用于敌人是否在门外"})
  30. public miidle_outline: Node = null;
  31. @autoBind({type:Node,tooltip:"进去的大门"})
  32. public doors: Node = null;
  33. //埋伏点配置数组
  34. public ambushPoints: AmbushPoint[] = [];
  35. //游戏速度
  36. public game_speed: number = 1;
  37. start() {
  38. /*测试时使用 玩家坐标转换
  39. let n: Node = this.node.getChildByName('player_points');
  40. let pwww:Vec3 = Utils.convertPosition(this.node.getChildByName("player"),n);
  41. console.log("GGGGGGG = " + pwww);*/
  42. //区域隐藏
  43. const nodes:Node[] = this.ambush_points.children.flatMap(child => child.children);
  44. [this.enemy_born_area]
  45. .concat(nodes).forEach(e => {
  46. let m: MeshRenderer = e.getComponent(MeshRenderer);
  47. if(m){m.enabled = false}
  48. });
  49. }
  50. /**
  51. * 游戏速度
  52. */
  53. public multiplySpeed(): number{
  54. return this.game_speed;
  55. }
  56. /**
  57. * 解析埋伏点
  58. */
  59. private parseAmbushPoints() {
  60. this.ambushPoints = this.ambush_points.children
  61. .filter(e=>e.active)
  62. .map(ambushNode => {
  63. const point = new AmbushPoint();
  64. point.isVacancy = false;
  65. //从节点结构获取坐标配置
  66. point.standPosArr = ambushNode.children
  67. .filter(e => e.name.startsWith('stand_'))
  68. .map(node => node.worldPosition.clone());
  69. point.roadPosArr = ambushNode.children
  70. .filter(e => e.name.startsWith('road_'))
  71. .sort((a, b) => {
  72. //提取数字部分(兼容 road_1 / road_01 / road_001 等所有情况)
  73. const getNumber = (name: string) => {
  74. const numPart = name.split('_').pop(); // 取最后一段
  75. return parseInt(numPart || '0', 10); // 自动忽略前导零
  76. };
  77. return getNumber(a.name) - getNumber(b.name); // 按数值升序
  78. })
  79. .map(node => node.worldPosition.clone());
  80. point.ambushPosArr = ambushNode.children
  81. .filter(e => e.name.startsWith('bunker_'))
  82. .map(node => node.worldPosition.clone());
  83. point.escapePosArr = ambushNode.children
  84. .filter(e => e.name.startsWith('escape_'))
  85. .map(node => node.worldPosition.clone());
  86. return point;
  87. });
  88. }
  89. /**
  90. * 根据敌人信息得到敌人行走路径
  91. * @param eData 敌人数据
  92. * @param size 预制体大小
  93. * @returns
  94. */
  95. public getPaths(eData: any){
  96. let bornPos: Vec3 = this.getRandomBornPosition();
  97. //进入外门时的坐标
  98. const outerPos: Vec3 = this.getRandomPos(eData,this.miidle_outline);
  99. //进入内门时的坐标
  100. const innerPos: Vec3 = this.getRandomPos(eData,this.miidle_inline);
  101. //行走路径
  102. let paths: Array<Vec3> = [bornPos,outerPos,innerPos];
  103. //最后一个点特殊处理 包含(站位点、埋伏点、逃跑点) 随机一个埋伏点
  104. const ambush: AmbushPoint = this.getAmbushClas();
  105. ambush.isVacancy = true;
  106. eData.ambush = ambush;
  107. //查找中间路径点
  108. paths = paths.concat(ambush.roadPosArr);
  109. //随机一个站立点到最后坐标
  110. const endPos:Vec3 = Utils.randomArray(ambush.standPosArr)[0];
  111. endPos.x += Utils.getRandomFloat(1,0.2);
  112. paths.push(endPos);
  113. //测试时使用
  114. /*paths.forEach(e => {
  115. console.log("FFFFFF = " + this.node.getComponent(UITransform).convertToNodeSpaceAR(e));
  116. });*/
  117. return paths;
  118. }
  119. /**
  120. * 得到一个站位点
  121. */
  122. private getAmbushClas(){
  123. if(!this.ambushPoints
  124. || this.ambushPoints.length <= 0){
  125. //埋伏点类解析
  126. this.parseAmbushPoints();
  127. }
  128. //获取所有可用的埋伏点(未被占用的)
  129. const availablePoints = this.ambushPoints.filter(p => !p.isVacancy);
  130. if (availablePoints.length > 0) {
  131. //随机返回一个可用埋伏点
  132. return Utils.randomArray(availablePoints)[0];
  133. }
  134. //当所有点位都被占用时,随机返回任意一个
  135. if(this.ambushPoints.length > 0) {
  136. return Utils.randomArray(this.ambushPoints)[0];
  137. }
  138. return null;
  139. }
  140. /**
  141. * 根据两个坐标点产生一个两个坐标点之前的随机点
  142. */
  143. public getRandomPos(eData:any,target: Node): Vec3 {
  144. //只有坦克走中间
  145. const isTan: boolean = eData.id == 20003;
  146. if(isTan){
  147. return target.worldPosition.clone();
  148. }
  149. let basePos: Vec3 = target.children[0].worldPosition.clone();
  150. if(target.children.length >= 2){
  151. let pos1: Vec3 = target.children[0].worldPosition.clone();
  152. let pos2: Vec3 = target.children[1].worldPosition.clone();
  153. //生成0-1的随机比例因子
  154. const ratio = Utils.getRandomFloat(0, 1);
  155. //分别在x和z轴进行线性插值 这儿为了看起来暂时保留x轴上的距离随机
  156. basePos.x = pos1.x + (pos2.x - pos1.x) * ratio;
  157. //y轴就是左边的间隔的距离
  158. basePos.z = pos1.z + (pos2.z - pos1.z) * ratio;
  159. }
  160. return basePos;
  161. }
  162. /**
  163. * 获取随机出生点位置
  164. */
  165. public getRandomBornPosition(): Vec3 {
  166. if (!this.enemy_born_area) return Vec3.ZERO;
  167. //获取3D节点的世界缩放
  168. const worldScale = this.enemy_born_area.getWorldScale();
  169. //使用X和Z轴缩放作为区域尺寸
  170. const areaWidth = worldScale.x;
  171. const areaDepth = worldScale.z;
  172. //在XZ平面生成随机坐标(Y轴保持地面高度)
  173. const randomX = (Math.random() - 0.5) * areaWidth;
  174. const randomZ = (Math.random() - 0.5) * areaDepth;
  175. //基于节点原点位置生成世界坐标 保持与出生区域相同高度
  176. const basePos = this.enemy_born_area.worldPosition;
  177. return new Vec3(
  178. basePos.x + randomX,
  179. basePos.y,
  180. basePos.z + randomZ
  181. );
  182. }
  183. /**
  184. * 还原数据
  185. */
  186. public resetData(){
  187. this.ambushPoints.forEach(e => {
  188. e.isVacancy = false;
  189. });
  190. }
  191. }