CheckpointModel.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. type Cell = Puppet | Obstacle
  11. export interface MapInfo {
  12. x: number,
  13. y: number,
  14. fill?: string | number
  15. pos: Vec3
  16. }
  17. @ecs.register('CheckpointModel')
  18. export class CheckpointModelComp extends ecs.Comp {
  19. private vm: any = {};
  20. private _grid: Grid[][] = []
  21. private _path_grid: MapInfo[][] = []
  22. private _cells: Cell[][] = []
  23. private _station: Station[] = []
  24. private _vehicles: Vehicle[] = []
  25. private _curVehicle: Vehicle = null
  26. get curVehicle(): Vehicle {
  27. return this._curVehicle
  28. }
  29. set curVehicle(_v: Vehicle) {
  30. this._curVehicle = _v
  31. }
  32. /** 格子 */
  33. get grids(): Grid[][] {
  34. return this._grid;
  35. }
  36. set grids(value: Grid[][]) {
  37. this._grid = value;
  38. }
  39. /** 寻路数据 */
  40. get path_grid(): MapInfo[][] {
  41. return this._path_grid;
  42. }
  43. set path_grid(value: MapInfo[][]) {
  44. this._path_grid = value;
  45. }
  46. /** 站台 */
  47. get stations(): Station[] {
  48. return this._station;
  49. }
  50. set stations(value: Station[]) {
  51. this._station = value;
  52. }
  53. /** 车辆 */
  54. get vehicles(): Vehicle[] {
  55. return this._vehicles;
  56. }
  57. set vehicles(value: Vehicle[]) {
  58. this._vehicles = value;
  59. }
  60. /** 棋盘格中物体 */
  61. get cells(): Cell[][] {
  62. return this._cells;
  63. }
  64. set cells(value: Cell[][]) {
  65. this._cells = value;
  66. }
  67. get emptyStation(): Station {
  68. return this._station.find(val => val.StationModel.isEmpty && !val.StationModel.isLock)
  69. }
  70. get emptyStationCount(): number {
  71. return this._station.filter(val => val.StationModel.isEmpty && !val.StationModel.isLock).length
  72. }
  73. get puppets(): Puppet[] {
  74. const flatArr = this.flattenArray(this.cells)
  75. return flatArr.filter(val => val instanceof Puppet && val.PuppetModel)
  76. }
  77. flattenArray(arr) {
  78. const result = [];
  79. arr.forEach((item) => {
  80. if (Array.isArray(item)) {
  81. result.push(...this.flattenArray(item)); // 如果是数组,则递归调用
  82. } else {
  83. result.push(item); // 否则直接加入到结果数组中
  84. }
  85. });
  86. return result;
  87. }
  88. vmAdd() {
  89. VM.add(this.vm, "Checkpoint");
  90. }
  91. vmRemove() {
  92. VM.remove("Checkpoint");
  93. }
  94. reset() {
  95. this.grids = []
  96. this.path_grid = []
  97. this.cells = []
  98. this.stations = []
  99. this.vehicles = []
  100. this.curVehicle = null
  101. this.vmRemove();
  102. }
  103. }