UECell.ts 3.8 KB

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