TouchGame.ts 5.1 KB

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