TouchGame.ts 5.6 KB

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