UECell.ts 4.2 KB

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