GMap.ts 8.5 KB

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