BlockController.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { _decorator, Color, Component, Node, Sprite, UIOpacity, Vec3 } from 'cc';
  2. import { BlockConfig, BlockOffset } from './BlockData';
  3. import { GridBlockMgr } from '../GridBlockMgr';
  4. import ResSprite from '../../ResSprite';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('BlockController')
  7. export class BlockController extends Component {
  8. @property(Node)
  9. blockSet: Node = null; // 25个子节点的父节点
  10. public curBlockConfig: BlockConfig = null;
  11. /**
  12. * 根据二维数组配置显示/隐藏子节点
  13. * @param detect 是否检测
  14. * @param blockConfig 数据
  15. * @param opacity 透明度
  16. * @returns
  17. */
  18. updateBlockSet(detect: boolean, blockConfig?: BlockConfig, opacity?: number) {
  19. if (blockConfig) {
  20. this.curBlockConfig = blockConfig;
  21. }
  22. const curbBlockArr = this.curBlockConfig.block_arr
  23. const children = this.blockSet.children;
  24. // 确保有25个子节点
  25. if (children.length !== 25) {
  26. console.error('blockSet should have exactly 25 children');
  27. return;
  28. }
  29. for (let i = 0; i < 5; i++) {
  30. for (let j = 0; j < 5; j++) {
  31. const index = i * 5 + j;
  32. const node = children[index];
  33. if (curbBlockArr[i][j] === 1) {
  34. node.getComponent(ResSprite).setSpriteFrame(blockConfig.block_color + '/spriteFrame');
  35. node.active = true; // 显示节点
  36. if (opacity) {
  37. node.getComponent(UIOpacity).opacity = opacity;
  38. node.getComponent(Sprite).color = new Color().fromHEX('#B9B9B9');
  39. } else {
  40. node.getComponent(UIOpacity).opacity = 255;
  41. }
  42. } else {
  43. node.active = false; // 隐藏节点
  44. // node.getComponent(UIOpacity).opacity = 100;
  45. }
  46. }
  47. }
  48. if (detect) {
  49. this.checkGray();
  50. }
  51. }
  52. /**
  53. * 检测是否可以放置
  54. * @param blockConfig 块配置
  55. */
  56. checkGray() {
  57. GridBlockMgr.ins.detectIsCanPlaced(this.curBlockConfig.block_arr, this.node)
  58. }
  59. /**
  60. * 根据数据调整偏移
  61. * @param scaleZoom 缩放比例
  62. */
  63. addBlockOffset(scaleZoom: number) {
  64. if (!this.curBlockConfig) {
  65. // console.error("curBlockConfig is null or undefined");
  66. return;
  67. }
  68. const blockOffset = this.curBlockConfig.block_offset;
  69. // console.log("Block offset:", blockOffset);
  70. // console.log("Original Position:", this.node.position);
  71. let offset = new Vec3();
  72. if (blockOffset === BlockOffset.UP) {
  73. offset = new Vec3(0, 122.5 / scaleZoom, 0);
  74. } else if (blockOffset === BlockOffset.DOWN) {
  75. offset = new Vec3(0, -122.5 / scaleZoom, 0);
  76. } else if (blockOffset === BlockOffset.LEFT) {
  77. offset = new Vec3(-122.5 / scaleZoom, 0, 0);
  78. } else if (blockOffset === BlockOffset.RIGHT) {
  79. offset = new Vec3(122.5 / scaleZoom, 0, 0);
  80. } else if (blockOffset === BlockOffset.LEFTANDDOWN) {
  81. offset = new Vec3(-122.5 / scaleZoom, -122.5 / scaleZoom, 0);
  82. } else if (blockOffset === BlockOffset.LEFTANDUP) {
  83. offset = new Vec3(-122.5 / scaleZoom, 122.5 / scaleZoom, 0);
  84. } else if (blockOffset === BlockOffset.RIGHTANDDOWN) {
  85. offset = new Vec3(122.5 / scaleZoom, -122.5 / scaleZoom, 0);
  86. } else if (blockOffset === BlockOffset.RIGHTANDUP) {
  87. offset = new Vec3(122.5 / scaleZoom, 122.5 / scaleZoom, 0);
  88. }
  89. // console.log("Offset to be added:", offset);
  90. this.node.position = this.node.position.add(offset);
  91. // console.log("New Position:", this.node.position);
  92. }
  93. start() {
  94. // 示例图块配置
  95. // const blockConfig = [
  96. // [0, 0, 0, 0, 0],
  97. // [0, 0, 1, 0, 0],
  98. // [0, 0, 1, 1, 0],
  99. // [0, 0, 0, 0, 0],
  100. // [0, 0, 0, 0, 0]
  101. // ];
  102. // 更新 blockSet
  103. // this.updateBlockSet(blockConfig);
  104. }
  105. }