CheckpointModel.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { Vec3 } from "cc";
  2. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  3. import { ECSEntity } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECSEntity";
  4. import { VM } from "../../../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel";
  5. import { Grid } from "../../grid/Grid";
  6. import { Obstacle } from "../../obstacle/Obstacle";
  7. import { Puppet } from "../../puppet/puppet";
  8. import { Station } from "../../station/Station";
  9. import { Vehicle } from "../../vehicle/Vehicle";
  10. import { Subway } from "../../subway/Subway";
  11. type Cell = Puppet | Obstacle | Subway
  12. export interface MapInfo {
  13. x: number,
  14. y: number,
  15. fill?: string | number
  16. pos: Vec3
  17. }
  18. @ecs.register('CheckpointModel')
  19. export class CheckpointModelComp extends ecs.Comp {
  20. private vm: any = {};
  21. private _grid: Grid[][] = []
  22. private _path_grid: MapInfo[][] = []
  23. private _cells: Cell[][] = []
  24. private _station: Station[] = []
  25. private _vehicles: Vehicle[] = []
  26. private _curVehicle: Vehicle = null
  27. private _nextVehicle: Vehicle = null
  28. private _peopleCount:number = 0
  29. get peopleCount(): number {
  30. return this._peopleCount
  31. }
  32. set peopleCount(_v: number) {
  33. this.vm.peopleCount = _v
  34. this._peopleCount = _v
  35. }
  36. get curVehicle(): Vehicle {
  37. return this._curVehicle
  38. }
  39. set curVehicle(_v: Vehicle) {
  40. this._curVehicle = _v
  41. }
  42. get nextVehicle(): Vehicle {
  43. return this._nextVehicle
  44. }
  45. set nextVehicle(_v: Vehicle) {
  46. this._nextVehicle = _v
  47. }
  48. /** 格子 */
  49. get grids(): Grid[][] {
  50. return this._grid;
  51. }
  52. set grids(value: Grid[][]) {
  53. this._grid = value;
  54. }
  55. /** 寻路数据 */
  56. get path_grid(): MapInfo[][] {
  57. return this._path_grid;
  58. }
  59. set path_grid(value: MapInfo[][]) {
  60. this._path_grid = value;
  61. }
  62. /** 站台 */
  63. get stations(): Station[] {
  64. return this._station;
  65. }
  66. set stations(value: Station[]) {
  67. this._station = value;
  68. }
  69. /** 车辆 */
  70. get vehicles(): Vehicle[] {
  71. return this._vehicles;
  72. }
  73. set vehicles(value: Vehicle[]) {
  74. this._vehicles = value;
  75. }
  76. /** 棋盘格中物体 */
  77. get cells(): Cell[][] {
  78. return this._cells;
  79. }
  80. set cells(value: Cell[][]) {
  81. this._cells = value;
  82. }
  83. get emptyStation(): Station {
  84. return this._station.find(val => val.StationModel.isEmpty && !val.StationModel.isLock)
  85. }
  86. get emptyStationCount(): number {
  87. return this._station.filter(val => val.StationModel.isEmpty && !val.StationModel.isLock).length
  88. }
  89. get puppets(): Puppet[] {
  90. const flatArr = this.flattenArray(this.cells)
  91. return flatArr.filter(val => val instanceof Puppet && val.PuppetModel)
  92. }
  93. // get canReachPuppets(): Puppet[] {
  94. // return this.puppets.filter(val => val.PathFind?.canReach===true)
  95. // }
  96. get subways(): Subway[] {
  97. const flatArr = this.flattenArray(this.cells)
  98. return flatArr.filter(val => val instanceof Subway && val.SubwayModel)
  99. }
  100. flattenArray(arr) {
  101. const result = [];
  102. arr.forEach((item) => {
  103. if (Array.isArray(item)) {
  104. result.push(...this.flattenArray(item)); // 如果是数组,则递归调用
  105. } else {
  106. result.push(item); // 否则直接加入到结果数组中
  107. }
  108. });
  109. return result;
  110. }
  111. vmAdd() {
  112. VM.add(this.vm, "Checkpoint");
  113. }
  114. vmRemove() {
  115. VM.remove("Checkpoint");
  116. }
  117. reset() {
  118. this.grids = []
  119. this.path_grid = []
  120. this.cells = []
  121. this.stations = []
  122. this.vehicles = []
  123. this.curVehicle = null
  124. this.peopleCount = 0
  125. this.vmRemove();
  126. }
  127. }