UECell.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import GameDataCenter from "../../data/GameDataCenter";
  2. import ResSprite from "../../frameWork/compment/ResSprite";
  3. import UEBase from "../../frameWork/compment/UEBase";
  4. import EventMng from "../../manager/EventMng";
  5. import { HcInfoGeziInfo, HcType, HcUnlock } from "../../shared/hc/PtlHcInfo";
  6. import { GridConstant } from "./GridConstant";
  7. import { GridEvent } from "./GridEvent";
  8. import UECube from "./UECube";
  9. const { ccclass, property } = cc._decorator;
  10. export interface I_CellData {
  11. idx: number;
  12. ueCube: UECube;
  13. unlock: HcUnlock;
  14. }
  15. @ccclass
  16. export default class UECell extends UEBase {
  17. static readonly BundleKey: string = "gridMap";
  18. static readonly PrefabUrl: string = "UECell";
  19. static readonly CLS: string = "UECell";
  20. @property(cc.Node)
  21. private node_select: cc.Node = null!;
  22. @property(ResSprite)
  23. private sp_quality: ResSprite = null;
  24. @property(cc.Node)
  25. private node_lock: cc.Node = null;
  26. cellData: I_CellData = null!;
  27. Init(cellData: I_CellData) {
  28. this.cellData = cellData;
  29. this.node.zIndex = cellData.idx;
  30. this.SetQuality();
  31. this.SetLock();
  32. EventMng.on(GridEvent.HC_CELL_CHANGE, this.OnHcCellChange, this);
  33. EventMng.on(GridEvent.HC_CELL_SELECT, this.OnHcCellSelect, this);
  34. }
  35. /** 监听格子选中 */
  36. OnHcCellSelect(idx: number) {
  37. if (!idx) {
  38. this.SetSelect(false);
  39. } else if (idx == this.cellData.idx) {
  40. this.SetSelect(true);
  41. }
  42. }
  43. /** 监听格子变化 */
  44. OnHcCellChange(data: { idx: number, item: HcInfoGeziInfo }) {
  45. if (data.idx == this.cellData.idx) {
  46. if (data.item.type == HcType.wu) {
  47. this.ClearCube();
  48. } else {
  49. }
  50. }
  51. }
  52. /** 创建格子 */
  53. CreateCell(i: number, j: number) {
  54. // let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  55. // this.cellLayer.addChild(cell.node);
  56. // cell.node.width = GridConstant.CELL_WIDTH;
  57. // cell.node.height = GridConstant.CELL_WIDTH;
  58. // let pos = GameDataCenter.gridMap.GetPosByVec(i, j);
  59. // cell.node.setPosition(pos);
  60. // return cell;
  61. }
  62. SetCube(cube: UECube) {
  63. this.cellData.ueCube = cube;
  64. this.SetQuality();
  65. }
  66. SetQuality() {
  67. if (this.cellData.ueCube && !this.IsLock()) {
  68. this.cellData.ueCube.SetZIndex(this.cellData.idx);
  69. let quality = this.cellData.ueCube.GetQuality();
  70. this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban0${quality}`);
  71. } else {
  72. this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban00`);
  73. }
  74. }
  75. IsLock(): boolean {
  76. return this.cellData.unlock == HcUnlock.off;
  77. }
  78. ChangeLockState(unlock: HcUnlock) {
  79. this.cellData.unlock = unlock;
  80. this.SetLock();
  81. }
  82. SetLock() {
  83. if (this.cellData.unlock == HcUnlock.off) {
  84. this.node_lock.active = true;
  85. if (this.cellData.ueCube) {
  86. this.cellData.ueCube.node.active = false;
  87. }
  88. } else {
  89. this.node_lock.active = false;
  90. if (this.cellData.ueCube) {
  91. this.cellData.ueCube.node.active = true;
  92. }
  93. }
  94. }
  95. GetZIndex() {
  96. return this.cellData.idx;
  97. }
  98. SetSelect(isSelect: boolean) {
  99. this.node_select.active = isSelect;
  100. }
  101. /** 是否有物品可以拖动 */
  102. CanDrag(): boolean {
  103. return !this.IsEmpty() && this.cellData.ueCube.CanDrag() && !this.IsLock();
  104. }
  105. GetCube(): UECube {
  106. return this.cellData.ueCube;
  107. }
  108. /** 该棋格是否是空的 */
  109. IsEmpty(): boolean {
  110. return this.cellData.ueCube == null;
  111. }
  112. /** 清空棋格里的棋子 */
  113. ClearCube() {
  114. this.cellData.ueCube.unUse();
  115. this.cellData.ueCube = null;
  116. this.SetQuality();
  117. }
  118. MoveCubeToCell(toCell: UECell) {
  119. this.cellData.ueCube.MoveToNewPos(toCell.node.position.clone());
  120. }
  121. }