TouchGame.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, Node, EventTouch, Rect, UITransform, Sprite, SpriteFrame, Vec2, instantiate, Vec3, Component } from 'cc';
  2. import { resMgr } from '../../Frames/ResourcesMgr';
  3. import { Role } from './Role';
  4. import { dataMgr } from '../../Frames/DataManager';
  5. import { RoleData } from '../../DataItem/ItemData';
  6. import { ModulerBase } from '../GameFrameWork/ModulerBase';
  7. import { MyRole } from './GameScene/MyRole';
  8. import { Card } from './Card';
  9. import { Bottom } from './GameScene/Bottom';
  10. import { PoolManager } from '../GameFrameWork/NodePool/PoolManager';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('TouchGame')
  13. export class TouchGame extends ModulerBase {
  14. private _characterSlot: Node = null;
  15. private _load: Node = null;
  16. private _dragNode: Node = null; // 拖拽节点
  17. private _isDragging: boolean = false; // 拖拽状态
  18. private _imgName: string = null;
  19. private _roleData: RoleData[] = null;
  20. //高亮索引
  21. private _highLightIdx: number = 0;
  22. //高亮高度
  23. _hight: number = 82;
  24. private _clickedNode: Node = null;
  25. private _clickedNodeCom: any = null;
  26. protected onLoad(): void {
  27. this._characterSlot = this.node.parent.getChildByName("CharacterSlot");
  28. this._load = this.node.parent.parent.getChildByName("Road");
  29. this._hight = this._load.getChildByName("Node").getComponent(UITransform).height;
  30. this._roleData = dataMgr.getAllDataByName("RoleCardData");
  31. // 创建拖拽节点
  32. this._dragNode = instantiate(resMgr.getPrefab("DragNode"));
  33. this._dragNode.active = false; // 初始状态隐藏
  34. this._dragNode.parent = this._load;
  35. }
  36. protected start(): void {
  37. //创建6个预制体
  38. PoolManager.instance.registerPool(resMgr.getPrefab("Role"), 6);
  39. this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  40. this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  41. this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  42. this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); // 处理触摸取消事件
  43. }
  44. private onTouchStart(event: EventTouch) {
  45. if (this._isDragging) return;
  46. const pos = event.getUILocation();
  47. for (const element of this._characterSlot.children) {
  48. const box: Rect = element.getComponent(UITransform).getBoundingBoxToWorld();
  49. if (box.contains(pos)) {
  50. this._clickedNode = element;
  51. this._clickedNodeCom = this._clickedNode.getComponent(Card);
  52. if (!this._clickedNodeCom.disabled && this._clickedNodeCom.clickable) {
  53. const img: SpriteFrame = element.getChildByName("Sprite").getComponent(Sprite).spriteFrame;
  54. if (!img) {
  55. this._isDragging = false;
  56. return;
  57. }
  58. if (img) {
  59. this._isDragging = true;
  60. this._dragNode.getComponent(Sprite).spriteFrame = img;
  61. this._dragNode.active = true;
  62. this._imgName = img.name;
  63. this.setDragNodePosition(pos);
  64. }
  65. break; // 找到一个就停止遍历
  66. }
  67. }
  68. }
  69. }
  70. private onTouchMove(event: EventTouch) {
  71. if (!this._isDragging) return;
  72. if (this._isDragging) {
  73. this.setDragNodePosition(event.getUILocation());
  74. } else {
  75. return;
  76. }
  77. for (const element of this._load.children) {
  78. if (element.name !== "Roles") {
  79. const box = element.getComponent(UITransform).getBoundingBoxToWorld();
  80. if (box.contains(event.getUILocation())) {
  81. element.active = true;
  82. } else {
  83. element.active = false;
  84. }
  85. }
  86. }
  87. }
  88. private onTouchEnd(event: EventTouch) {
  89. if (!this._isDragging) return;
  90. const pos: Vec3 = new Vec3(event.getUILocation().x, event.getUILocation().y, 0)
  91. this.stopDragging(pos);
  92. }
  93. //设置拖拽物体的坐标
  94. private setDragNodePosition(pos: Vec2) {
  95. this._dragNode.setWorldPosition(pos.x, pos.y, 0);
  96. }
  97. private stopDragging(pos: Vec3) {
  98. this._isDragging = false;
  99. for (const element of this._load.children) {
  100. if (element.name !== "Roles") {
  101. const box = element.getComponent(UITransform).getBoundingBoxToWorld();
  102. if (box.contains(new Vec2(pos.x, pos.y))) {
  103. this._highLightIdx = element.getSiblingIndex();
  104. if (this._highLightIdx >= 6) {
  105. this._hideNode();
  106. return;
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. //从NodePool里获取一个Node
  113. const role: Node = PoolManager.instance.get(resMgr.getPrefab("Role"));
  114. //const role: Node = instantiate(resMgr.getPrefab("Role"));
  115. role.parent = this._load.getChildByName("Roles");
  116. const roleTS: Role = role.getComponent(MyRole);
  117. const y = 180 + (this._highLightIdx + 1) * this._hight - this._hight / 2;
  118. const rolePos: Vec3 = new Vec3(50, y, pos.z)
  119. roleTS.init(this._imgName, rolePos, this._roleData, 1);
  120. //消耗矿石
  121. this.node.parent.getComponent(Bottom).onReduce(this._clickedNodeCom.consumeCount);
  122. //冷却当中
  123. this._clickedNode.getComponent(Card).aniPlay();
  124. this._hideNode();
  125. }
  126. private _hideNode() {
  127. this._dragNode.active = false;
  128. this._dragNode.getComponent(Sprite).spriteFrame = null;
  129. for (const element of this._load.children) {
  130. if (element.name !== "Roles") {
  131. element.active = false;
  132. }
  133. }
  134. }
  135. protected onDestroy(): void {
  136. this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
  137. this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  138. this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  139. this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  140. }
  141. }