InitCheckpoint.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { v3 } from "cc";
  2. import { AsyncQueue } from "../../../../../extensions/oops-plugin-framework/assets/libs/collection/AsyncQueue";
  3. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  4. import { Grid } from "../../grid/Grid";
  5. import { Checkpoint } from "../Checkpoint";
  6. import { CheckpointModelLevelComp } from "../model/CheckpointModelLevel";
  7. import { CheckpointViewComp } from "../view/CheckpointViewComp";
  8. import { CheckpointModelComp } from "../model/CheckpointModel";
  9. import { Station } from "../../station/Station";
  10. import { GridModelComp } from "../../grid/model/GridModelComp";
  11. import { Obstacle } from "../../obstacle/Obstacle";
  12. import { Puppet } from "../../puppet/puppet";
  13. import { PuppetModelComp } from "../../puppet/model/PuppetModelComp";
  14. import { ObstacleModelComp } from "../../obstacle/model/ObstacleModelComp";
  15. import { PathFindComp } from "../../common/ecs/path/PathFind";
  16. /** 初始化游戏公共资源 */
  17. @ecs.register('InitCheckpoint')
  18. export class InitCheckpointComp extends ecs.Comp {
  19. reset() { }
  20. }
  21. export class InitCheckpointSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
  22. filter(): ecs.IMatcher {
  23. return ecs.allOf(InitCheckpointComp, CheckpointModelComp, CheckpointModelLevelComp);
  24. }
  25. entityEnter(e: Checkpoint): void {
  26. const levelConfig = e.get(CheckpointModelLevelComp)?.rtluCurrent
  27. const { gridCount, levelColor, peopleCount, levelObstacle, obstacleCount } = levelConfig
  28. this.createGrid(e, gridCount, gridCount, levelColor, levelObstacle, peopleCount, obstacleCount)
  29. this.createStation(e, levelConfig.stationCount)
  30. e.remove(InitCheckpointComp)
  31. }
  32. // 生成棋盘相关
  33. createGrid(e: Checkpoint, row: number, col: number, colors: string[], obstacles: number[], peopleCount: number, obstacleCount: number) {
  34. const checkpoint_root = e.get(CheckpointViewComp).node
  35. const checkpoint_model = e.get(CheckpointModelComp)
  36. const grids = this.generateGridWithEmptySpaces(row, col, colors, obstacles, peopleCount, obstacleCount)
  37. checkpoint_model.grids = Array.from({ length: row }, () => new Array(col).fill(null));
  38. checkpoint_model.cells = Array.from({ length: row }, () => new Array(col).fill(null));
  39. checkpoint_model.path_grid = Array.from({ length: row }, () => new Array(col).fill(null));
  40. const start_point = v3(-1.706, 0, -1.718)
  41. for (let index = 0; index < grids.length; index++) {
  42. for (let j = 0; j < grids[index].length; j++) {
  43. const gridEnt = ecs.getEntity<Grid>(Grid);
  44. const gridModel = gridEnt.get(GridModelComp)
  45. // 添加关卡到场景
  46. const fill = grids[index][j]
  47. gridModel.color = fill
  48. const pos = v3(start_point.x + j * 0.6, 0, start_point.z + index * 0.6)
  49. gridEnt.load(checkpoint_root, pos);
  50. e.addChild(gridEnt)
  51. checkpoint_model.grids[index][j] = gridEnt
  52. if (typeof grids[index][j] === 'number') { // 生成障碍物
  53. const obstacle = ecs.getEntity<Obstacle>(Obstacle)
  54. const obstacleModel = obstacle.get(ObstacleModelComp)
  55. obstacleModel.type = fill
  56. obstacle.load(checkpoint_root, pos);
  57. checkpoint_model.cells[index][j] = obstacle
  58. checkpoint_model.path_grid[index][j] = {
  59. x: index,
  60. y: j,
  61. fill: fill,
  62. pos: pos
  63. }
  64. } else if (typeof grids[index][j] === 'string') { // 生成人物
  65. const puppet = ecs.getEntity<Puppet>(Puppet)
  66. const puppetModel = puppet.get(PuppetModelComp)
  67. puppetModel.color = fill
  68. puppetModel.x = index
  69. puppetModel.y = j
  70. puppet.load(checkpoint_root, fill, pos);
  71. checkpoint_model.cells[index][j] = puppet
  72. checkpoint_model.path_grid[index][j] = {
  73. x: index,
  74. y: j,
  75. fill: fill,
  76. pos: pos
  77. }
  78. const pathComp = puppet.add(PathFindComp)
  79. pathComp.x = index
  80. pathComp.y = j
  81. } else { // 空格
  82. checkpoint_model.path_grid[index][j] = {
  83. x: index,
  84. y: j,
  85. fill: null,
  86. pos: pos
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // // 生成棋盘格
  93. // createGrid(e: Checkpoint, row: number, col: number) {
  94. // const checkpoint_root = e.get(CheckpointViewComp).node
  95. // const checkpoint_model = e.get(CheckpointModelComp)
  96. // checkpoint_model.grids = Array.from({ length: row }, () => new Array(col).fill(null));
  97. // const start_point = v3(-1.706, 0, -1.718)
  98. // for (let index = 0; index < row; index++) {
  99. // for (let j = 0; j < col; j++) {
  100. // const grid = ecs.getEntity<Grid>(Grid);
  101. // // 添加关卡到场景
  102. // grid.load(checkpoint_root, v3(start_point.x + index * 0.6, 0, start_point.z + j * 0.6));
  103. // e.addChild(grid)
  104. // checkpoint_model.grids[index][j] = grid
  105. // }
  106. // }
  107. // }
  108. // 生成站台
  109. createStation(e: Checkpoint, count: number) {
  110. const checkpoint_root = e.get(CheckpointViewComp).node
  111. const checkpoint_model = e.get(CheckpointModelComp)
  112. const start_point = v3(-1, 0, -3)
  113. for (let index = 0; index < count; index++) {
  114. const station = ecs.getEntity<Station>(Station);
  115. // 添加关卡到场景
  116. station.load(checkpoint_root, v3(start_point.x + index * 0.6, 0, start_point.z));
  117. e.addChild(station)
  118. checkpoint_model.stations.push(station)
  119. }
  120. }
  121. // 生成交通工具
  122. createVehicle(e: Checkpoint) {
  123. }
  124. // 生成人物
  125. createMan(x: number, y: number, color: string) {
  126. }
  127. // 生成障碍物
  128. createObstacle() {
  129. }
  130. generateGridWithEmptySpaces(row: number, col: number, colors: string[], obstacles: number[], peopleCount: number, obstacleCount: number) {
  131. const totalCells = row * col;
  132. let grid = Array.from({ length: row }, () => new Array(col).fill(null));
  133. // 初始化颜色计数
  134. let counts = {};
  135. colors.forEach(color => {
  136. let maxCount = Math.floor((totalCells - 1) / 3) * 3; // 最大可能的 3 的倍数
  137. counts[color] = Math.min(maxCount / 3, peopleCount * 3); // 分配三分之一
  138. });
  139. // 创建所有可能的格子位置数组
  140. let availablePositions = [];
  141. for (let i = 0; i < row; i++) {
  142. for (let j = 0; j < col; j++) {
  143. availablePositions.push([i, j]);
  144. }
  145. }
  146. let obstacleNum = obstacleCount
  147. // 随机分配颜色到网格中
  148. while (availablePositions.length > 0) {
  149. let randomIndex = Math.floor(Math.random() * availablePositions.length);
  150. let position = availablePositions.splice(randomIndex, 1)[0];
  151. let x = position[0];
  152. let y = position[1];
  153. let colorIndex = Math.floor(Math.random() * colors.length);
  154. let color = colors[colorIndex];
  155. if (counts[color] > 0) {
  156. grid[x][y] = color;
  157. counts[color]--;
  158. } else if (obstacleNum > 0) {
  159. grid[x][y] = obstacles[Math.floor(Math.random() * obstacles.length)]
  160. obstacleNum--
  161. }
  162. }
  163. return grid;
  164. }
  165. // generateOptimizedGrid(colors:string[]) {
  166. // const gridSize = 7;
  167. // const totalCells = gridSize * gridSize;
  168. // // const colors = ['红色', '蓝色', '绿色'];
  169. // let grid = Array(gridSize).fill().map(() => Array(gridSize).fill(''));
  170. // // 初始化每种颜色的人数
  171. // let counts = { '红色': 0, '蓝色': 0, '绿色': 0 };
  172. // // 首先分配每种颜色的人数,使其尽可能接近总数且是 3 的倍数
  173. // for (let color of colors) {
  174. // let maxCount = Math.floor((totalCells - 1) / 3) * 3; // 最大可能的 3 的倍数
  175. // counts[color] = maxCount / 3; // 分配三分之一
  176. // }
  177. // // 在剩余的空格中随机分配颜色或保持为空
  178. // let remainingCells = totalCells - 3 * Math.floor((totalCells - 1) / 3);
  179. // let colorIndices = colors.map((_, index) => index);
  180. // while (remainingCells > 0) {
  181. // let randomIndex = Math.floor(Math.random() * colorIndices.length);
  182. // let color = colors[colorIndices[randomIndex]];
  183. // counts[color]++;
  184. // remainingCells--;
  185. // }
  186. // // 将颜色分配到网格中
  187. // let flatGrid = grid.flat();
  188. // colors.forEach(color => {
  189. // for (let i = 0; i < counts[color]; i++) {
  190. // let pos;
  191. // do {
  192. // pos = Math.floor(Math.random() * flatGrid.length);
  193. // } while (flatGrid[pos] !== '');
  194. // flatGrid[pos] = color;
  195. // }
  196. // });
  197. // // 重新构建二维网格
  198. // for (let i = 0; i < flatGrid.length; i++) {
  199. // grid[Math.floor(i / gridSize)][i % gridSize] = flatGrid[i];
  200. // }
  201. // return grid;
  202. // }
  203. }