InitCheckpoint.ts 12 KB

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