CheckpointModel.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 _peopleCount:number = 0
  28. get peopleCount(): number {
  29. return this._peopleCount
  30. }
  31. set peopleCount(_v: number) {
  32. this.vm.peopleCount = _v
  33. this._peopleCount = _v
  34. }
  35. get curVehicle(): Vehicle {
  36. return this._curVehicle
  37. }
  38. set curVehicle(_v: Vehicle) {
  39. this._curVehicle = _v
  40. }
  41. /** 格子 */
  42. get grids(): Grid[][] {
  43. return this._grid;
  44. }
  45. set grids(value: Grid[][]) {
  46. this._grid = value;
  47. }
  48. /** 寻路数据 */
  49. get path_grid(): MapInfo[][] {
  50. return this._path_grid;
  51. }
  52. set path_grid(value: MapInfo[][]) {
  53. this._path_grid = value;
  54. }
  55. /** 站台 */
  56. get stations(): Station[] {
  57. return this._station;
  58. }
  59. set stations(value: Station[]) {
  60. this._station = value;
  61. }
  62. /** 车辆 */
  63. get vehicles(): Vehicle[] {
  64. return this._vehicles;
  65. }
  66. set vehicles(value: Vehicle[]) {
  67. this._vehicles = value;
  68. }
  69. /** 棋盘格中物体 */
  70. get cells(): Cell[][] {
  71. return this._cells;
  72. }
  73. set cells(value: Cell[][]) {
  74. this._cells = value;
  75. }
  76. get emptyStation(): Station {
  77. return this._station.find(val => val.StationModel.isEmpty && !val.StationModel.isLock)
  78. }
  79. get emptyStationCount(): number {
  80. return this._station.filter(val => val.StationModel.isEmpty && !val.StationModel.isLock).length
  81. }
  82. get puppets(): Puppet[] {
  83. const flatArr = this.flattenArray(this.cells)
  84. return flatArr.filter(val => val instanceof Puppet && val.PuppetModel)
  85. }
  86. get subways(): Subway[] {
  87. const flatArr = this.flattenArray(this.cells)
  88. return flatArr.filter(val => val instanceof Subway && val.SubwayModel)
  89. }
  90. flattenArray(arr) {
  91. const result = [];
  92. arr.forEach((item) => {
  93. if (Array.isArray(item)) {
  94. result.push(...this.flattenArray(item)); // 如果是数组,则递归调用
  95. } else {
  96. result.push(item); // 否则直接加入到结果数组中
  97. }
  98. });
  99. return result;
  100. }
  101. vmAdd() {
  102. VM.add(this.vm, "Checkpoint");
  103. }
  104. vmRemove() {
  105. VM.remove("Checkpoint");
  106. }
  107. reset() {
  108. this.grids = []
  109. this.path_grid = []
  110. this.cells = []
  111. this.stations = []
  112. this.vehicles = []
  113. this.curVehicle = null
  114. this.peopleCount = 0
  115. this.vmRemove();
  116. }
  117. }