TouchGame.ts 5.9 KB

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