InitCheckpoint.ts 12 KB

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