InitCheckpoint.ts 11 KB

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