TouchSkill.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { _decorator, animation, Animation, Component, EventTouch, instantiate, Label, Node, Sprite, UITransform, Vec2, Vec3 } from 'cc';
  2. import { GameInfo } from '../../../GameInfo';
  3. import { resMgr } from '../../../Frames/ResourcesMgr';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('TouchSkill')
  6. export class TouchSkill extends Component {
  7. //skill放置范围
  8. private _skillRange: Node = null;
  9. private _dragNode: Node = null; // 拖拽节点
  10. private _isDragging: boolean = false; // 拖拽状态
  11. //高亮索引
  12. private _highLightIdx: number = 0;
  13. //高亮高度
  14. private _hight: number = 82;
  15. //skill名字
  16. private _skillName: string = null;
  17. //钻石拥有量
  18. private _ownDiamondCount: Node = null;
  19. protected onLoad(): void {
  20. this._skillRange = this.node.parent.parent.getChildByName("Skill_Range");
  21. this._ownDiamondCount = this.node.getChildByName("_ownNumber");
  22. this._hight = this._skillRange.getChildByName("Node").getComponent(UITransform).height;
  23. // 创建拖拽节点
  24. this._dragNode = instantiate(resMgr.getPrefab("DragNode"));
  25. this._dragNode.active = false; // 初始状态隐藏
  26. this._dragNode.parent = this._skillRange;
  27. }
  28. protected start(): void {
  29. this._skillName = GameInfo.Instance.getSkill();
  30. this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  31. this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  32. this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  33. this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); // 处理触摸取消事件
  34. }
  35. private onTouchStart(event: EventTouch) {
  36. //拥有的钻石小于需要的,退出
  37. if (GameInfo.Instance.getOwnDiamondNum() < GameInfo.Instance.getNeedDiamondNum()) {
  38. return;
  39. }
  40. if (this._isDragging) return;
  41. const pos = event.getUILocation();
  42. this._dragNode.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(this._skillName);
  43. this._dragNode.active = true;
  44. this._isDragging = true;
  45. this.setDragNodePosition(pos);
  46. }
  47. private onTouchMove(event: EventTouch) {
  48. if (!this._isDragging) return;
  49. if (this._isDragging) {
  50. this.setDragNodePosition(event.getUILocation());
  51. } else {
  52. return;
  53. }
  54. for (const element of this._skillRange.children) {
  55. const box = element.getComponent(UITransform).getBoundingBoxToWorld();
  56. if (box.contains(event.getUILocation())) {
  57. element.active = true;
  58. } else {
  59. element.active = false;
  60. }
  61. }
  62. }
  63. private onTouchEnd(event: EventTouch) {
  64. if (!this._isDragging) return;
  65. const pos: Vec3 = new Vec3(event.getUILocation().x, event.getUILocation().y, 0)
  66. this.stopDragging(pos);
  67. }
  68. //设置拖拽物体的坐标
  69. private setDragNodePosition(pos: Vec2) {
  70. this._dragNode.setWorldPosition(pos.x, pos.y, 0);
  71. }
  72. private stopDragging(pos: Vec3) {
  73. this._isDragging = false;
  74. for (const element of this._skillRange.children) {
  75. if (element.name !== "Roles") {
  76. const box = element.getComponent(UITransform).getBoundingBoxToWorld();
  77. if (box.contains(new Vec2(pos.x, pos.y))) {
  78. this._highLightIdx = element.getSiblingIndex();
  79. if (this._highLightIdx >= 6) {
  80. this._hideNode();
  81. return;
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. const skill = instantiate(resMgr.getPrefab("Skill"));
  88. skill.parent = this._skillRange.getChildByName("Roles");
  89. const y = 180 + (this._highLightIdx + 1) * this._hight - this._hight / 2;
  90. const skillPos: Vec3 = new Vec3(250, y, pos.z);
  91. skill.setWorldPosition(skillPos);
  92. skill.getComponent(Animation).play();
  93. skill.getComponent(Animation).once(Animation.EventType.FINISHED, () => {
  94. skill.destroy();
  95. })
  96. //使用了技能则消耗钻石
  97. this._onReduce();
  98. this._hideNode();
  99. }
  100. //减少拥有的钻石
  101. private _onReduce() {
  102. let own = GameInfo.Instance.getOwnDiamondNum();
  103. let need = GameInfo.Instance.getNeedDiamondNum();
  104. own -= need;
  105. GameInfo.Instance.setOwnDiamondNum(own);
  106. this._ownDiamondCount.getComponent(Label).string = String(own);
  107. }
  108. //相关节点隐藏
  109. private _hideNode() {
  110. this._dragNode.active = false;
  111. this._dragNode.getComponent(Sprite).spriteFrame = null;
  112. for (const element of this._skillRange.children) {
  113. if (element.name !== "Roles") {
  114. element.active = false;
  115. }
  116. }
  117. }
  118. protected onDestroy(): void {
  119. this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
  120. this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  121. this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  122. this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  123. }
  124. }