InitCheckpoint.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import { color, 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. import { Subway } from "../../subway/Subway";
  25. import { SubwayModelComp } from "../../subway/model/SubwayModelComp";
  26. import { SubwayEnum } from "../../subway/model/SubwayEnum";
  27. import { CheckpointPathTriggerComp } from "./CheckpointPathTrigger";
  28. /** 初始化游戏公共资源 */
  29. @ecs.register('InitCheckpoint')
  30. export class InitCheckpointComp extends ecs.Comp {
  31. reset() { }
  32. }
  33. export class InitCheckpointSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
  34. filter(): ecs.IMatcher {
  35. return ecs.allOf(InitCheckpointComp, CheckpointModelComp, CheckpointModelLevelComp, CheckpointModelBaseComp);
  36. }
  37. entityEnter(e: Checkpoint): void {
  38. const levelConfig = e.get(CheckpointModelLevelComp)?.rtluCurrent
  39. const { gridRowCount, gridColCount, levelColor, peopleCount, vehicleColor, vehicleType,
  40. obstaclePosition, stationCount, peopleColor, peoplePosition, subwayPosition, subwayDir, subwayInfo } = levelConfig
  41. this.resetCheckpoint(e)
  42. // 关卡数据
  43. oops.audio.volumeMusic = 1
  44. e.CheckpointModel.vmAdd();
  45. e.CheckpointModelBase.vmAdd();
  46. !e.CheckpointModelLevel && e.CheckpointModelLevel.vmAdd();
  47. // 添加关卡到场景
  48. e.load(oops.game.root, v3(0, 0, 0), () => {
  49. this.createGrid(e, gridRowCount, gridColCount)
  50. if (peopleCount) { // 自动生成
  51. e.CheckpointModel.peopleCount = peopleCount
  52. const count = Math.ceil(peopleCount / 3)
  53. const colorCount = this.distributePeopleToColorsByArray(peopleCount, levelColor)
  54. const { gridPositions, gridColors, subwayPositions, subwayColors, subwayOrientations } = this.distributeToGridAndSubwayWithOptimizedPlacement(gridRowCount, gridColCount, colorCount, levelColor, 24)
  55. this.createMan(e, gridPositions, gridColors, levelColor)
  56. this.createSubway(e, subwayPositions, subwayOrientations, subwayColors, levelColor)
  57. this.createStation(e, stationCount)
  58. this.createVehicle(e, count, [], colorCount, levelColor)
  59. } else { // 依据配置生成
  60. this.createObstacle(e, obstaclePosition)
  61. this.createMan(e, peoplePosition, peopleColor, levelColor)
  62. this.createSubway(e, subwayPosition, subwayDir, subwayInfo, levelColor)
  63. this.createStation(e, stationCount)
  64. let subwayPeople = 0
  65. let flatSubwayArr = []
  66. if (subwayInfo) {
  67. flatSubwayArr = subwayInfo.flat()
  68. subwayPeople = flatSubwayArr.length
  69. }
  70. e.CheckpointModel.peopleCount = peopleColor.length + subwayPeople
  71. const count = Math.ceil((peopleColor.length + subwayPeople) / 3)
  72. const colorCount = {}
  73. const colorArr = levelColor as number[]
  74. colorArr.forEach((val, index) => {
  75. if (colorCount[val] == null) {
  76. colorCount[val] = 0
  77. }
  78. colorCount[val] = peopleColor.filter(v => v === index + 1).length + flatSubwayArr.filter(v => v === index + 1).length
  79. })
  80. this.createVehicle(e, count, vehicleColor, colorCount, levelColor)
  81. }
  82. e.add(VehicleOperationComp)
  83. e.add(CheckpointPathTriggerComp)
  84. e.remove(InitCheckpointComp)
  85. // 关闭加载界面
  86. oops.gui.remove(UIID.Loading);
  87. });
  88. oops.gui.open(UIID.Game)
  89. if(e.CheckpointModelLevel.vm.lv===1){ // 一级新手引导
  90. oops.gui.open(UIID.Guide)
  91. }
  92. }
  93. resetCheckpoint(e: Checkpoint) {
  94. e.remove(CheckpointViewComp);
  95. e.add(CheckpointModelComp, true)
  96. e.add(CheckpointModelBaseComp, true)
  97. // e.add(CheckpointModelLevelComp, true)
  98. e.children.forEach(child => {
  99. e.removeChild(child);
  100. child.destroy();
  101. });
  102. }
  103. distributePeopleToColorsByArray(totalPeople: number, colors: string[]) {
  104. // 验证输入
  105. if (totalPeople % 3 !== 0) {
  106. return '给定的人数必须是3的倍数';
  107. }
  108. const colorCount = colors.length;
  109. let baseDistribution = Math.floor(totalPeople / colorCount);
  110. let remainder = totalPeople % colorCount;
  111. // 确保基础分配是3的倍数
  112. if (baseDistribution % 3 !== 0) {
  113. baseDistribution -= baseDistribution % 3;
  114. remainder += totalPeople - (baseDistribution * colorCount);
  115. }
  116. // 初始化颜色分配对象
  117. const colorDistribution = colors.reduce((acc, color) => {
  118. acc[color] = baseDistribution;
  119. return acc;
  120. }, {});
  121. // 处理余数,尽可能平均分配
  122. let index = 0;
  123. while (remainder > 0) {
  124. colorDistribution[colors[index]] += 3; // 每次增加3人以保持总数为3的倍数
  125. remainder -= 3;
  126. index = (index + 1) % colorCount; // 环形处理颜色索引
  127. }
  128. return colorDistribution;
  129. }
  130. distributeToGridAndSubwayWithOptimizedPlacement(row: number, col: number, colorDistribution: any, colors: string[], subwayCapacity = 24) {
  131. const grid = Array.from({ length: row }, () => Array(col).fill(null));
  132. let freePositions = [];
  133. let freePositionsCopy = [];
  134. for (let i = 0; i < row; i++) {
  135. for (let j = 0; j < col; j++) {
  136. freePositions.push([i, j]);
  137. freePositionsCopy.push([i, j]);
  138. }
  139. }
  140. const gridColors: number[] = []
  141. const gridPositions: [x: number, y: number][] = []
  142. const subwayPositions: [x: number, y: number][] = [];
  143. const subwayColors: number[][] = [];
  144. const subwayOrientations: number[] = [];
  145. const cellCount = row * col
  146. // 创建颜色池
  147. let colorPool: number[] = [];
  148. colors.forEach((color, index) => {
  149. for (let i = 0; i < colorDistribution[color]; i++) {
  150. colorPool.push(index + 1);
  151. }
  152. });
  153. const getSubwayOrientation = () => Math.random() < 0.5 ? SubwayEnum.LEFT : SubwayEnum.RIGHT;
  154. // // 随机选择颜色函数
  155. // const selectRandomColor = () => {
  156. // if (colorPool.length === 0) return null;
  157. // let index = Math.floor(Math.random() * colorPool.length);
  158. // return colorPool.splice(index, 1)[0] as number; // 从颜色池中移除并返回选中的颜色
  159. // };
  160. const randColorPool = this.shuffleArray(colorPool)
  161. randColorPool.forEach((colorIndex) => {
  162. // let people = colorDistribution[colors[colorIndex-1]];
  163. if (freePositions.length > 0) {
  164. let randomIndex = Math.floor(Math.random() * freePositions.length);
  165. let [x, y] = freePositions[randomIndex];
  166. grid[x][y] = colorIndex;
  167. gridColors.push(colorIndex)
  168. gridPositions.push([x, y])
  169. freePositions.splice(randomIndex, 1);
  170. } else {
  171. if (!subwayColors[subwayColors.length - 1] || subwayColors[subwayColors.length - 1].length >= subwayCapacity) {
  172. const orientation = getSubwayOrientation();
  173. const filteredPositions = freePositionsCopy.filter(([x, y]) =>
  174. ((orientation === SubwayEnum.LEFT && y > 0) || (orientation === SubwayEnum.RIGHT && y < col - 1)) && grid[x][y] !== 'Subway');
  175. if (filteredPositions.length > 0) {
  176. const index = Math.floor(Math.random() * filteredPositions.length);
  177. const [subwayX, subwayY] = filteredPositions[index];
  178. const gridColor = grid[subwayX][subwayY]
  179. subwayPositions.push([subwayX, subwayY]);
  180. const existIndex = gridPositions.findIndex(val => {
  181. const [x, y] = val
  182. return x === subwayX && y === subwayY
  183. })
  184. if (existIndex > -1) {
  185. gridPositions.splice(existIndex, 1)
  186. gridColors.splice(existIndex, 1)
  187. }
  188. subwayColors.push([gridColor, colorIndex]); // 新地铁的颜色
  189. subwayOrientations.push(orientation);
  190. grid[subwayX][subwayY] = 'Subway';
  191. freePositionsCopy.splice(index, 1);
  192. }
  193. } else {
  194. subwayColors[subwayColors.length - 1].push(colorIndex); // 添加到最后一个地铁的颜色
  195. }
  196. }
  197. })
  198. return {
  199. gridPositions: gridPositions.map(([x, y]) => [x + 1, y + 1]) as [x: number, y: number][],
  200. gridColors,
  201. subwayPositions: subwayPositions.map(([x, y]) => [x + 1, y + 1]) as [x: number, y: number][],
  202. subwayColors,
  203. subwayOrientations
  204. };
  205. }
  206. // 洗牌算法打乱颜色
  207. shuffleArray(array) {
  208. for (let i = array.length - 1; i > 0; i--) {
  209. // 生成从 0 到 i 的随机数
  210. const j = Math.floor(Math.random() * (i + 1));
  211. // 交换元素 array[i] 和 array[j]
  212. [array[i], array[j]] = [array[j], array[i]];
  213. }
  214. return array;
  215. }
  216. // 生成棋盘相关
  217. createGrid(e: Checkpoint, row: number, col: number) {
  218. const checkpoint_root = e.get(CheckpointViewComp).node
  219. const checkpoint_model = e.get(CheckpointModelComp)
  220. checkpoint_model.grids = Array.from({ length: row }, () => new Array(col).fill(null));
  221. checkpoint_model.cells = Array.from({ length: row }, () => new Array(col).fill(null));
  222. checkpoint_model.path_grid = Array.from({ length: row }, () => new Array(col).fill(null));
  223. const start_point = row % 2 === 0 ? v3(-row / 2 * 0.5+0.1, 0, 2.7) : v3(-row / 2 * 0.75, 0, 2.7)
  224. for (let index = 0; index < row; index++) {
  225. for (let j = 0; j < col; j++) {
  226. const gridEnt = ecs.getEntity<Grid>(Grid);
  227. // 添加关卡到场景
  228. const pos = v3(start_point.x + j * 0.5, 0, start_point.z + index * 0.5)
  229. gridEnt.load(checkpoint_root, pos);
  230. e.addChild(gridEnt)
  231. checkpoint_model.grids[index][j] = gridEnt
  232. // if (typeof grids[index][j] === 'number') { // 生成障碍物
  233. // const obstacle = ecs.getEntity<Obstacle>(Obstacle)
  234. // const obstacleModel = obstacle.get(ObstacleModelComp)
  235. // obstacleModel.type = fill
  236. // obstacle.load(checkpoint_root, pos);
  237. // checkpoint_model.cells[index][j] = obstacle
  238. // checkpoint_model.path_grid[index][j] = {
  239. // x: index,
  240. // y: j,
  241. // fill: fill,
  242. // pos: pos
  243. // }
  244. // } else if (typeof grids[index][j] === 'string') { // 生成人物
  245. // const puppet = ecs.getEntity<Puppet>(Puppet)
  246. // const puppetModel = puppet.get(PuppetModelComp)
  247. // puppetModel.color = fill
  248. // puppetModel.x = index
  249. // puppetModel.y = j
  250. // puppet.load(checkpoint_root, fill, pos, smc.initialize.account.AccountModel.skin);
  251. // checkpoint_model.cells[index][j] = puppet
  252. // checkpoint_model.path_grid[index][j] = {
  253. // x: index,
  254. // y: j,
  255. // fill: fill,
  256. // pos: pos
  257. // }
  258. // const pathComp = puppet.add(PathFindComp)
  259. // pathComp.x = index
  260. // pathComp.y = j
  261. // } else { // 空格
  262. // checkpoint_model.path_grid[index][j] = {
  263. // x: index,
  264. // y: j,
  265. // fill: null,
  266. // pos: pos
  267. // }
  268. // }
  269. }
  270. }
  271. }
  272. // 生成人物
  273. createMan(e: Checkpoint, position: [x: number, y: number][], color: number[], colorArr: string[]) {
  274. const checkpoint_root = e.get(CheckpointViewComp).node
  275. const checkpoint_model = e.get(CheckpointModelComp)
  276. position && position.forEach((val, index) => {
  277. const [x, y] = val
  278. const xIndex = x - 1
  279. const yIndex = y - 1
  280. const puppet = ecs.getEntity<Puppet>(Puppet)
  281. const puppetModel = puppet.get(PuppetModelComp)
  282. const fillColor = colorArr[color[index] - 1]
  283. puppetModel.color = fillColor
  284. puppetModel.x = xIndex
  285. puppetModel.y = yIndex
  286. const pos = checkpoint_model.grids[xIndex][yIndex].GridView.node.position.clone()
  287. puppet.load(checkpoint_root, fillColor, pos, smc.initialize.account.AccountModel.skin);
  288. checkpoint_model.cells[xIndex][yIndex] = puppet
  289. checkpoint_model.path_grid[xIndex][yIndex] = {
  290. x: xIndex,
  291. y: yIndex,
  292. fill: fillColor,
  293. pos: pos
  294. }
  295. const pathComp = puppet.add(PathFindComp)
  296. pathComp.x = xIndex
  297. pathComp.y = yIndex
  298. })
  299. }
  300. // 生成障碍物
  301. createObstacle(e: Checkpoint, position: [x: number, y: number][]) {
  302. const checkpoint_root = e.get(CheckpointViewComp).node
  303. const checkpoint_model = e.get(CheckpointModelComp)
  304. position && position.forEach((val, index) => {
  305. const obstacle = ecs.getEntity<Obstacle>(Obstacle)
  306. const [x, y] = val
  307. const xIndex = x - 1
  308. const yIndex = y - 1
  309. const pos = checkpoint_model.grids[xIndex][yIndex].GridView.node.position.clone()
  310. obstacle.load(checkpoint_root, pos);
  311. checkpoint_model.cells[xIndex][yIndex] = obstacle
  312. checkpoint_model.path_grid[xIndex][yIndex] = {
  313. x: xIndex,
  314. y: yIndex,
  315. fill: 'obstacle',
  316. pos: pos
  317. }
  318. })
  319. }
  320. // 生成地铁
  321. createSubway(e: Checkpoint, position: [x: number, y: number][], dir: number[], info: number[][], colorArr: string[]) {
  322. const checkpoint_root = e.get(CheckpointViewComp).node
  323. const checkpoint_model = e.get(CheckpointModelComp)
  324. position && position.forEach((val, index) => {
  325. const [x, y] = val
  326. const xIndex = x - 1
  327. const yIndex = y - 1
  328. const pos = checkpoint_model.grids[xIndex][yIndex].GridView.node.position.clone()
  329. const d = dir[index]
  330. const subway = ecs.getEntity<Subway>(Subway)
  331. const subwayModel = subway.SubwayModel
  332. subwayModel.colorArr = info[index].map(v => colorArr[v - 1])
  333. subwayModel.type = d
  334. subwayModel.x = xIndex
  335. subwayModel.y = yIndex
  336. subway.load(checkpoint_root, pos, d);
  337. checkpoint_model.cells[xIndex][yIndex] = subway
  338. checkpoint_model.path_grid[xIndex][yIndex] = {
  339. x: xIndex,
  340. y: yIndex,
  341. fill: 'subway',
  342. pos: pos
  343. }
  344. })
  345. }
  346. // 生成站台
  347. createStation(e: Checkpoint, count: number) {
  348. const checkpoint_root = e.get(CheckpointViewComp).node
  349. const checkpoint_model = e.get(CheckpointModelComp)
  350. const start_point = v3(-1.2, 0, 1.2)
  351. for (let index = 0; index < count; index++) {
  352. const station = ecs.getEntity<Station>(Station);
  353. // 添加关卡到场景
  354. const h_index = index % 5
  355. // const v_index = Math.floor(index / 5)
  356. const v_index = checkpoint_model.stations.length >= 5 ? 1 : 0
  357. station.load(checkpoint_root, v3(start_point.x + h_index * 0.5, 0, start_point.z + v_index * 0.5));
  358. e.addChild(station)
  359. checkpoint_model.stations.push(station)
  360. }
  361. }
  362. // 生成交通工具
  363. createVehicle(e: Checkpoint, count: number, pcolor: number[], colorCount: any, colors: string[]) {
  364. const copyColorCount = {...colorCount}
  365. const checkpoint_root = e.CheckpointView.node
  366. const checkpoint_model = e.CheckpointModel
  367. const availablePositions = Array.from({ length: count }, (_, i) => i)
  368. checkpoint_model.vehicles = Array(count).fill(null)
  369. const start_point = v3(-6, 0, 0)
  370. pcolor?.forEach((v,i)=>{
  371. const color = colors[pcolor[i] - 1]
  372. if (copyColorCount[color] > 0) {
  373. const vehicle = ecs.getEntity<Vehicle>(Vehicle);
  374. availablePositions.shift()
  375. copyColorCount[color]-=3;
  376. // 添加车到场景
  377. vehicle.VehicleModel.color = color
  378. const vehicleType = e.CheckpointModelLevel.rtluCurrent.vehicleType
  379. vehicle.load(checkpoint_root, color, start_point, vehicleType);
  380. e.addChild(vehicle)
  381. checkpoint_model.vehicles[i] = vehicle
  382. }
  383. })
  384. while (availablePositions.length > 0) {
  385. let randomIndex = Math.floor(Math.random() * availablePositions.length);
  386. let colorIndex = Math.floor(Math.random() * colors.length);
  387. let color = colors[colorIndex];
  388. if (copyColorCount[color] > 0) {
  389. // if (randomIndex < pcolor.length) {
  390. // availablePositions.splice(randomIndex, 1);
  391. // }
  392. const vehicle = ecs.getEntity<Vehicle>(Vehicle);
  393. let index = availablePositions.splice(randomIndex, 1)[0];
  394. copyColorCount[color]-=3;
  395. // 添加车到场景
  396. vehicle.VehicleModel.color = color
  397. const vehicleType = e.CheckpointModelLevel.rtluCurrent.vehicleType
  398. vehicle.load(checkpoint_root, color, start_point, vehicleType);
  399. e.addChild(vehicle)
  400. checkpoint_model.vehicles[index] = vehicle
  401. }
  402. }
  403. // for (let index = 0; index < count; index++) {
  404. // const vehicle = ecs.getEntity<Vehicle>(Vehicle);
  405. // const vehicleModel = vehicle.get(VehicleModelComp)
  406. // if (index < color.length) {
  407. // const v_color = colors[color[index] - 1]
  408. // vehicleModel.color = v_color
  409. // colorCount[v_color] = colorCount[v_color] - 1
  410. // } else {
  411. // const v_color = colors[index % 3]
  412. // vehicleModel.color
  413. // }
  414. // // 添加车到场景
  415. // const vehicleType = e.CheckpointModelLevel.rtluCurrent.vehicleType
  416. // vehicle.load(checkpoint_root, vehicleModel.color, start_point, vehicleType);
  417. // e.addChild(vehicle)
  418. // checkpoint_model.vehicles.push(vehicle)
  419. // }
  420. }
  421. // // 生成棋盘相关
  422. // createGrid(e: Checkpoint, row: number, col: number, colors: string[], obstacles: number[], peopleCount: number, obstacleCount: number) {
  423. // const checkpoint_root = e.get(CheckpointViewComp).node
  424. // const checkpoint_model = e.get(CheckpointModelComp)
  425. // const grids = this.generateGridWithEmptySpaces(row, col, colors, obstacles, peopleCount, obstacleCount)
  426. // checkpoint_model.grids = Array.from({ length: row }, () => new Array(col).fill(null));
  427. // checkpoint_model.cells = Array.from({ length: row }, () => new Array(col).fill(null));
  428. // checkpoint_model.path_grid = Array.from({ length: row }, () => new Array(col).fill(null));
  429. // const start_point = v3(-1.9, 0, 2.7)
  430. // for (let index = 0; index < grids.length; index++) {
  431. // for (let j = 0; j < grids[index].length; j++) {
  432. // const gridEnt = ecs.getEntity<Grid>(Grid);
  433. // const gridModel = gridEnt.get(GridModelComp)
  434. // // 添加关卡到场景
  435. // const fill = grids[index][j]
  436. // gridModel.color = fill
  437. // const pos = v3(start_point.x + j * 0.5, 0, start_point.z + index * 0.5)
  438. // gridEnt.load(checkpoint_root, pos);
  439. // e.addChild(gridEnt)
  440. // checkpoint_model.grids[index][j] = gridEnt
  441. // if (typeof grids[index][j] === 'number') { // 生成障碍物
  442. // const obstacle = ecs.getEntity<Obstacle>(Obstacle)
  443. // const obstacleModel = obstacle.get(ObstacleModelComp)
  444. // obstacleModel.type = fill
  445. // obstacle.load(checkpoint_root, pos);
  446. // checkpoint_model.cells[index][j] = obstacle
  447. // checkpoint_model.path_grid[index][j] = {
  448. // x: index,
  449. // y: j,
  450. // fill: fill,
  451. // pos: pos
  452. // }
  453. // } else if (typeof grids[index][j] === 'string') { // 生成人物
  454. // const puppet = ecs.getEntity<Puppet>(Puppet)
  455. // const puppetModel = puppet.get(PuppetModelComp)
  456. // puppetModel.color = fill
  457. // puppetModel.x = index
  458. // puppetModel.y = j
  459. // puppet.load(checkpoint_root, fill, pos, smc.initialize.account.AccountModel.skin);
  460. // checkpoint_model.cells[index][j] = puppet
  461. // checkpoint_model.path_grid[index][j] = {
  462. // x: index,
  463. // y: j,
  464. // fill: fill,
  465. // pos: pos
  466. // }
  467. // const pathComp = puppet.add(PathFindComp)
  468. // pathComp.x = index
  469. // pathComp.y = j
  470. // } else { // 空格
  471. // checkpoint_model.path_grid[index][j] = {
  472. // x: index,
  473. // y: j,
  474. // fill: null,
  475. // pos: pos
  476. // }
  477. // }
  478. // }
  479. // }
  480. // }
  481. // // 生成棋盘格
  482. // createGrid(e: Checkpoint, row: number, col: number) {
  483. // const checkpoint_root = e.get(CheckpointViewComp).node
  484. // const checkpoint_model = e.get(CheckpointModelComp)
  485. // checkpoint_model.grids = Array.from({ length: row }, () => new Array(col).fill(null));
  486. // const start_point = v3(-1.706, 0, -1.718)
  487. // for (let index = 0; index < row; index++) {
  488. // for (let j = 0; j < col; j++) {
  489. // const grid = ecs.getEntity<Grid>(Grid);
  490. // // 添加关卡到场景
  491. // grid.load(checkpoint_root, v3(start_point.x + index * 0.6, 0, start_point.z + j * 0.6));
  492. // e.addChild(grid)
  493. // checkpoint_model.grids[index][j] = grid
  494. // }
  495. // }
  496. // }
  497. calcMaxCount(row: number, col: number, peopleCount: number) {
  498. const totalCells = row * col;
  499. let maxCount = Math.floor((totalCells - 1) / 3) * 3; // 最大可能的 3 的倍数
  500. return Math.min(maxCount / 3, peopleCount * 3); // 分配三分之一
  501. }
  502. generateGridWithEmptySpaces(row: number, col: number, colors: string[], obstacles: number[], peopleCount: number, obstacleCount: number) {
  503. let grid = Array.from({ length: row }, () => new Array(col).fill(null));
  504. // 初始化颜色计数
  505. let counts = {};
  506. colors.forEach(color => {
  507. counts[color] = this.calcMaxCount(row, col, peopleCount); // 分配三分之一
  508. });
  509. // 创建所有可能的格子位置数组
  510. let availablePositions = [];
  511. for (let i = 0; i < row; i++) {
  512. for (let j = 0; j < col; j++) {
  513. availablePositions.push([i, j]);
  514. }
  515. }
  516. let obstacleNum = obstacleCount
  517. // 随机分配颜色到网格中
  518. while (availablePositions.length > 0) {
  519. let randomIndex = Math.floor(Math.random() * availablePositions.length);
  520. let position = availablePositions.splice(randomIndex, 1)[0];
  521. let x = position[0];
  522. let y = position[1];
  523. let colorIndex = Math.floor(Math.random() * colors.length);
  524. let color = colors[colorIndex];
  525. if (counts[color] > 0) {
  526. grid[x][y] = color;
  527. counts[color]--;
  528. } else if (obstacleNum > 0) {
  529. grid[x][y] = obstacles[Math.floor(Math.random() * obstacles.length)]
  530. obstacleNum--
  531. }
  532. }
  533. return grid;
  534. }
  535. // generateOptimizedGrid(colors:string[]) {
  536. // const gridSize = 7;
  537. // const totalCells = gridSize * gridSize;
  538. // // const colors = ['红色', '蓝色', '绿色'];
  539. // let grid = Array(gridSize).fill().map(() => Array(gridSize).fill(''));
  540. // // 初始化每种颜色的人数
  541. // let counts = { '红色': 0, '蓝色': 0, '绿色': 0 };
  542. // // 首先分配每种颜色的人数,使其尽可能接近总数且是 3 的倍数
  543. // for (let color of colors) {
  544. // let maxCount = Math.floor((totalCells - 1) / 3) * 3; // 最大可能的 3 的倍数
  545. // counts[color] = maxCount / 3; // 分配三分之一
  546. // }
  547. // // 在剩余的空格中随机分配颜色或保持为空
  548. // let remainingCells = totalCells - 3 * Math.floor((totalCells - 1) / 3);
  549. // let colorIndices = colors.map((_, index) => index);
  550. // while (remainingCells > 0) {
  551. // let randomIndex = Math.floor(Math.random() * colorIndices.length);
  552. // let color = colors[colorIndices[randomIndex]];
  553. // counts[color]++;
  554. // remainingCells--;
  555. // }
  556. // // 将颜色分配到网格中
  557. // let flatGrid = grid.flat();
  558. // colors.forEach(color => {
  559. // for (let i = 0; i < counts[color]; i++) {
  560. // let pos;
  561. // do {
  562. // pos = Math.floor(Math.random() * flatGrid.length);
  563. // } while (flatGrid[pos] !== '');
  564. // flatGrid[pos] = color;
  565. // }
  566. // });
  567. // // 重新构建二维网格
  568. // for (let i = 0; i < flatGrid.length; i++) {
  569. // grid[Math.floor(i / gridSize)][i % gridSize] = flatGrid[i];
  570. // }
  571. // return grid;
  572. // }
  573. }