TouchSkill.ts 5.9 KB

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