UECube.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import Gamecfg from "../../common/gameCfg";
  2. import ResSprite from "../../frameWork/compment/ResSprite";
  3. import UEBase from "../../frameWork/compment/UEBase";
  4. import EventMng from "../../manager/EventMng";
  5. import { GridEvent } from "./GridEvent";
  6. const { ccclass, property } = cc._decorator;
  7. enum E_CubeType {
  8. None = 0,//无物品
  9. Emitter = 1,//发射器
  10. Monster = 2,//怪物
  11. MergeProp = 3,//合成物品
  12. }
  13. export interface I_CubeData {
  14. /** 物品类型 */
  15. type: E_CubeType;
  16. /** 物品ID */
  17. id: number;
  18. /** 层级 */
  19. zIndex: number;
  20. /** 解锁条件 */
  21. unlock: number
  22. }
  23. @ccclass
  24. export default class UECube extends UEBase {
  25. static readonly BundleKey: string = "gridMap";
  26. static readonly PrefabUrl: string = "UECube";
  27. static readonly CLS: string = "UECube";
  28. @property(cc.Node)
  29. private itemNode: cc.Node = null!;
  30. @property(cc.Label)
  31. private lbt_num: cc.Label = null!;
  32. @property(ResSprite)
  33. private sp_item: ResSprite = null!;
  34. originalPos: cc.Vec3 = cc.Vec3.ZERO;
  35. cubeData: I_CubeData = null!;
  36. Init(cubeData: I_CubeData) {
  37. this.cubeData = cubeData;
  38. this.SetZIndex(cubeData.zIndex);
  39. this.originalPos = cc.v3(this.node.x, this.node.y);
  40. if (cubeData.type == E_CubeType.Emitter) {
  41. let mergePropCfg = Gamecfg.emitterInfo.getItem(cubeData.id.toString());
  42. this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
  43. } else if (cubeData.type == E_CubeType.MergeProp) {
  44. let mergePropCfg = Gamecfg.mergePropInfo.getItem(cubeData.id.toString());
  45. this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
  46. }
  47. }
  48. /** 触发点击 */
  49. TriggerClick() {
  50. if (this.cubeData.type == E_CubeType.Emitter) {
  51. CC_PREVIEW && console.log("触发点击发射器");
  52. EventMng.emit(GridEvent.TRIGGER_EMITTER, this.cubeData.zIndex, 10009);
  53. } else if (this.cubeData.type == E_CubeType.Monster) {
  54. CC_PREVIEW && console.log("点击怪物开始战斗");
  55. } else if (this.cubeData.type == E_CubeType.MergeProp) {
  56. }
  57. }
  58. GetCubeData(): I_CubeData {
  59. return this.cubeData;
  60. }
  61. SetZIndex(zIndex: number) {
  62. this.node.zIndex = zIndex;
  63. this.cubeData.zIndex = zIndex;
  64. this.lbt_num.string = zIndex.toString();
  65. }
  66. /** 是否有物品可以拖动 */
  67. CanDrag(): boolean {
  68. return true;
  69. }
  70. StartDrag(): void {
  71. this.node.zIndex = 1000;
  72. }
  73. UpdateDragPosition(pos: cc.Vec3): void {
  74. this.node.position = pos.sub(this.node.position);
  75. }
  76. EndDrag(): void {
  77. this.node.zIndex = this.cubeData.zIndex;
  78. }
  79. ClearCell() {
  80. }
  81. /** 回到原来位置 */
  82. BackToOriginalPos(needMove: boolean) {
  83. if (needMove) {
  84. cc.tween(this.node)
  85. .to(0.15, { position: this.originalPos })
  86. .call(() => {
  87. this.EndDrag();
  88. this.PlayJellyAnim();
  89. })
  90. .start();
  91. } else {
  92. this.itemNode.setPosition(this.originalPos.clone());
  93. this.EndDrag();
  94. this.PlayJellyAnim();
  95. }
  96. }
  97. /** 播放果冻效果 */
  98. PlayJellyAnim() {
  99. cc.tween(this.itemNode)
  100. .to(0.1, { scale: 1.3 })
  101. .to(0.1, { scale: 0.9 })
  102. .to(0.08, { scale: 1 })
  103. .start();
  104. }
  105. /** 播放合成动画 */
  106. PlayMergeAnim() {
  107. cc.tween(this.itemNode)
  108. .by(0.1, { position: cc.v3(0, 40) })
  109. .by(0.1, { position: cc.v3(0, -40) })
  110. .by(0.1, { position: cc.v3(0, 10) })
  111. .by(0.1, { position: cc.v3(0, -10) })
  112. .start();
  113. }
  114. /** 移动到新的位置 */
  115. MoveToNewPos(newPos: cc.Vec3, duration: number = 0.2) {
  116. cc.tween(this.node)
  117. .to(duration, { position: newPos })
  118. .call(() => {
  119. this.EndDrag();
  120. this.PlayJellyAnim();
  121. })
  122. .start();
  123. this.originalPos = newPos.clone();
  124. }
  125. private UpdateItemDisplay(): void {
  126. // 根据 itemType 和 itemLevel 更新物品显示
  127. if (this.itemNode) {
  128. this.itemNode.active = true;
  129. // TODO: 更新物品的具体显示效果
  130. }
  131. }
  132. public unUse() {
  133. this.node.destroy();
  134. }
  135. }