Board.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { _decorator, Color, Mask, v3, Vec2, Vec3 } from 'cc';
  2. import { Container } from '../container/Container';
  3. import { ContainerView } from '../container/ContainerView';
  4. import { BoardState, CellState, ObstacleType, PatternType } from '../../util/Enum';
  5. export interface evt {
  6. }
  7. // 格子数据结构
  8. interface CellData {
  9. x: number;
  10. y: number;
  11. state: CellState;
  12. patternType?: PatternType;
  13. isStartPoint?: boolean;
  14. isEndPoint?: boolean;
  15. obstacleType?: ObstacleType; // 障碍物类型
  16. isBreakable?: boolean; // 是否可破坏
  17. isMovable?: boolean; // 是否可移动
  18. }
  19. export class Board {
  20. state: BoardState
  21. selectedContainer: Container
  22. containerView: ContainerView
  23. isWaitingSwap: boolean = false;//是否可用
  24. private _evt = chsdk.get_new_event<evt>();
  25. get evt() {
  26. return this._evt;
  27. }
  28. private _boardSize: number = 49;
  29. private _boardRows: number = 7;
  30. private _boardCols: number = 7;
  31. private _boardData: CellData[][] = [];
  32. private _lines: { type: PatternType, points: Vec2[] }[] = [];
  33. private _selectedPoint: Vec2 = null;
  34. private _currentPatternType: PatternType = null;
  35. private _pathCells: Vec2[] = []; // 当前路径经过的格子
  36. // 初始化棋盘数据
  37. initBoard(rows: number, cols: number) {
  38. this._boardRows = rows;
  39. this._boardCols = cols;
  40. this._boardData = [];
  41. for (let y = 0; y < this._boardRows; y++) {
  42. this._boardData[y] = [];
  43. for (let x = 0; x < this._boardCols; x++) {
  44. this._boardData[y][x] = { x, y, state: CellState.EMPTY };
  45. }
  46. }
  47. }
  48. // Getter 和 Setter 方法
  49. get boardData(): CellData[][] {
  50. return this._boardData;
  51. }
  52. set boardData(value: CellData[][]) {
  53. this._boardData = value;
  54. }
  55. get lines() {
  56. return this._lines;
  57. }
  58. set lines(value) {
  59. this._lines = value;
  60. }
  61. get selectedPoint() {
  62. return this._selectedPoint;
  63. }
  64. set selectedPoint(value) {
  65. this._selectedPoint = value;
  66. }
  67. get currentPatternType() {
  68. return this._currentPatternType;
  69. }
  70. set currentPatternType(value) {
  71. this._currentPatternType = value;
  72. }
  73. get boardRows() {
  74. return this._boardRows;
  75. }
  76. set boardRows(value) {
  77. this._boardRows = value;
  78. }
  79. get boardCols() {
  80. return this._boardCols;
  81. }
  82. set boardCols(value) {
  83. this._boardCols = value;
  84. }
  85. get boardSize() {
  86. return this._boardSize;
  87. }
  88. set boardSize(value) {
  89. this._boardSize = value;
  90. }
  91. // 获取路径经过的格子
  92. get pathCells() {
  93. return this._pathCells;
  94. }
  95. set pathCells(value) {
  96. this._pathCells = value;
  97. }
  98. // 清除当前路径
  99. clearCurrentPath() {
  100. this._pathCells = [];
  101. this._selectedPoint = null;
  102. this._currentPatternType = null;
  103. }
  104. }