Hero.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { _decorator, Label,sp, UITransform, Vec2, Vec3 ,Node, isValid, tween} from 'cc';
  2. import { ObjectUtil } from '../../../../framework/util/ObjectUtil';
  3. import { BattleNodeBase } from './BattleNodeBase';
  4. import { HeroData } from '../data/HeroData';
  5. import { AudioID } from '../../../../framework/config/AudioConf';
  6. import { Framework } from '../../../../framework/Framework';
  7. import { UptypeConf } from '../conf/UptypeConf';
  8. import { HeroConf } from '../conf/HeroConf';
  9. const { ccclass, property } = _decorator;
  10. //英雄基类
  11. @ccclass('Hero')
  12. export class Hero extends BattleNodeBase {
  13. @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
  14. modelSpine: sp.Skeleton = null;
  15. @property({ type: Label, tooltip: '等级' })
  16. levelLabel: Label = null;
  17. @property({ type: Node, tooltip: '枪' })
  18. gunNode: Node = null;
  19. //所属格子 -1表示暂无
  20. private _posID = -1;
  21. private _bAttackAnimation: boolean = false;
  22. attackNode = null;
  23. attackBone = null;
  24. attackSlot = null;
  25. typeID: number = -1;
  26. level: number = 1;
  27. radius = 1; //半径
  28. set posID(posID:number){
  29. this._posID = posID;
  30. }
  31. get posID(){
  32. return this._posID;
  33. }
  34. protected onLoad(): void {
  35. this.modelSpine.setCompleteListener(this.actionComplete.bind(this))
  36. this.modelSpine.setEventListener(this.actionCallback.bind(this))
  37. let bone = this.modelSpine.findBone("gun")
  38. if(bone){
  39. this.attackBone = bone
  40. let rot = this.attackBone.rotation
  41. }
  42. }
  43. start() {
  44. }
  45. resetData(heroData:HeroData) {
  46. this.typeID = heroData.typeID;
  47. this.level = heroData.level;
  48. this.radius = heroData.attackRadius;
  49. this.levelLabel.string = heroData.level.toString();
  50. this.node.active = true;
  51. this.modelSpine.node.active = true;
  52. let conf = UptypeConf.data
  53. let speedScale = conf.HeroSpeedColArr[this.level]
  54. this.modelSpine.timeScale = speedScale;
  55. this.stand()
  56. // let id: string = heroData.heroID;
  57. }
  58. clearData() {
  59. this.node.active = false;
  60. this.modelSpine.node.active = false;
  61. this._posID = -1;
  62. this.typeID = -1;
  63. this.level = 0;
  64. this._isLock = false;
  65. }
  66. update(deltaTime: number) {
  67. super.update(deltaTime);
  68. if(this._bAttackAnimation && isValid(this.attackNode)){
  69. let pos = this.attackNode.position;
  70. // 获取子节点的世界坐标
  71. let worldPosition = this.gunNode.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
  72. // 将子节点的世界坐标转换为父节点的局部坐标
  73. let localPosition = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPosition);
  74. // 计算从p0到p1的向量
  75. let deltaX: number = pos.x - localPosition.x;
  76. let deltaY: number = pos.y - localPosition.y;
  77. // 计算旋转角度(弧度)
  78. let rotationAngleRadians: number = Math.atan2(deltaY, deltaX);
  79. // 将旋转角度转换为度数
  80. let rotationAngleDegrees: number = (rotationAngleRadians * (180 / Math.PI)+360)%360;
  81. // console.log("旋转角度:", rotationAngleDegrees);
  82. // let scaleX = this.modelSpine.node.scale.x
  83. // if(rotationAngleDegrees > 90 && rotationAngleDegrees < 270){
  84. // // if(scaleX > 0){
  85. // // scaleX= -scaleX;
  86. // // }
  87. // // this.gunNode.angle = 180 - rotationAngleDegrees;
  88. // this.attackBone.rotation = 180 - rotationAngleDegrees
  89. // }
  90. // else{
  91. // // if(scaleX < 0){
  92. // // scaleX= -scaleX;
  93. // // }
  94. // // this.gunNode.angle = rotationAngleDegrees;
  95. // this.attackBone.rotation = rotationAngleDegrees
  96. // }
  97. // this.modelSpine.node.scale = new Vec3(scaleX,this.modelSpine.node.scale.y,this.modelSpine.node.scale.z);
  98. // console.log("旋转角度:", rotationAngleDegrees);
  99. if(rotationAngleDegrees<67.5 || rotationAngleDegrees > 292.5){
  100. this.attackBone.rotation = rotationAngleDegrees
  101. if(this.modelSpine.animation != "fire_0")
  102. this.modelSpine.setAnimation(0, 'fire_0', true);
  103. }
  104. else if(rotationAngleDegrees < 112.5){
  105. this.attackBone.rotation = rotationAngleDegrees
  106. if(this.modelSpine.animation != "fire_90")
  107. this.modelSpine.setAnimation(0, 'fire_90', true);
  108. }
  109. else if(rotationAngleDegrees < 247.5){
  110. this.attackBone.rotation = 180-rotationAngleDegrees
  111. if(this.modelSpine.animation != "fire_180")
  112. this.modelSpine.setAnimation(0, 'fire_180', true);
  113. }
  114. else if(rotationAngleDegrees < 292.5){
  115. this.attackBone.rotation = rotationAngleDegrees
  116. if(this.modelSpine.animation != "fire_270")
  117. this.modelSpine.setAnimation(0, 'fire_270', true);
  118. }
  119. }
  120. }
  121. //屏幕坐标
  122. bTouch(pos: Vec2){
  123. return this.node.getComponent(UITransform).hitTest(pos,0);
  124. }
  125. get bAttackAnimation(): boolean{
  126. return this._bAttackAnimation;
  127. }
  128. //通过目标坐标 来确定朝向
  129. attack(attackNode:Node){
  130. if(!this.modelSpine.node.active) return;
  131. // if(this._bAttackAnimation) return;
  132. this._bAttackAnimation = true;
  133. this.attackNode = attackNode;
  134. Framework.audio.playEffect(AudioID.Tututu);
  135. }
  136. stand() {
  137. if(!this.modelSpine.node.active) return;
  138. this._bAttackAnimation = false;
  139. // this.gunNode.angle = 0;
  140. this.gunNode.active = true;
  141. this.attackBone.rotation = 0
  142. // this._attackCallback = null;
  143. if(this.modelSpine.animation == "stand_0") return;
  144. this.modelSpine.setAnimation(0, 'stand_0', true);
  145. // if(this.modelSpine.node.scale.x < 0)
  146. // this.modelSpine.node.scale = new Vec3(-this.modelSpine.node.scale.x,this.modelSpine.node.scale.y,this.modelSpine.node.scale.z);
  147. }
  148. levelUp() {
  149. this.level++;
  150. this.levelLabel.string = this.level.toString();
  151. let conf = UptypeConf.data
  152. let speedScale = conf.HeroSpeedColArr[this.level]
  153. this.modelSpine.timeScale = speedScale;
  154. }
  155. actionCallback(trackEntry){
  156. // console.log("动作回调:",trackEntry.animation.name)
  157. if (trackEntry.animation.name && trackEntry.animation.name.includes("fire")) {
  158. // if (this._attackCallback) {
  159. // this._attackCallback(trackEntry);
  160. // }
  161. }
  162. }
  163. actionComplete(trackEntry:sp.spine.TrackEntry){
  164. // console.log("动作完成:",trackEntry.animation.name,this._posID)
  165. if (trackEntry.animation.name && trackEntry.animation.name.includes("fire")) {
  166. // this._bAttackAnimation = false;
  167. // // this.gunNode.angle = 0;
  168. // // this.gunNode.active = false;
  169. // this.attackBone.rotation = 0
  170. // this._attackCallback = null;
  171. }
  172. }
  173. //
  174. flyTo(pos:Vec3, callback:Function){
  175. if(this._isLock){ return false; }
  176. this.isLock = true;
  177. let length = Vec3.distance(this.node.position,pos);
  178. if(length > 300){
  179. length = 300;
  180. }
  181. tween(this.node).to(length/500,{position:pos}).call(()=>{
  182. this.isLock = false;
  183. if(callback) callback();
  184. }).start();
  185. }
  186. hitTest(pos:Vec2){
  187. return this.modelSpine.getComponent(UITransform).hitTest(pos);
  188. }
  189. }