Hero.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. import { BattleEventManager } from '../base/BattleEventManager';
  10. import { BattleEventHeroAction, BattleEventTarget, HeroActionType } from '../base/BattleEventUtil';
  11. import { BattleUtil } from '../data/BattleUtil';
  12. const { ccclass, property } = _decorator;
  13. //英雄基类
  14. @ccclass('Hero')
  15. export class Hero extends BattleNodeBase {
  16. @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
  17. modelSpine: sp.Skeleton = null;
  18. @property({ type: Label, tooltip: '等级' })
  19. levelLabel: Label = null;
  20. @property({ type: Node, tooltip: '枪' })
  21. gunNode: Node = null;
  22. @property({ type: UITransform, tooltip: '点击框' })
  23. clickBox: UITransform = null;
  24. //所属格子 -1表示暂无
  25. private _posID = -1;
  26. private _bAttackAnimation: boolean = false;
  27. attackNode = null;
  28. //
  29. attackBone1 = null;
  30. attackBone2 = null;
  31. attackBone3 = null;
  32. attackBone4 = null;
  33. attackAngle1 = 0;
  34. attackAngle2 = 0;
  35. attackAngle3 = 0;
  36. attackAngle4 = 0;
  37. attackSlot = null;
  38. typeID: number = -1;
  39. level: number = 1;
  40. radius = 1; //半径
  41. battleEventManager: BattleEventManager;
  42. set posID(posID:number){
  43. this._posID = posID;
  44. }
  45. get posID(){
  46. return this._posID;
  47. }
  48. protected onLoad(): void {
  49. this.modelSpine.setCompleteListener(this.actionComplete.bind(this))
  50. this.modelSpine.setEventListener(this.actionCallback.bind(this))
  51. this.battleEventManager = BattleEventManager.instance
  52. let bone1 = this.modelSpine.findBone("gun1")
  53. if(bone1){
  54. this.attackBone1= bone1
  55. let rot = this.attackBone1.rotation
  56. this.attackAngle1 = rot
  57. console.log("rot",rot)
  58. }
  59. let bone2 = this.modelSpine.findBone("gun2")
  60. if(bone2){
  61. this.attackBone2 = bone2
  62. let rot = this.attackBone2.rotation
  63. this.attackAngle2 = rot
  64. console.log("rot",rot)
  65. }
  66. let bone3 = this.modelSpine.findBone("gun3")
  67. if(bone3){
  68. this.attackBone3 = bone3
  69. let rot = this.attackBone3.rotation
  70. this.attackAngle3 = rot
  71. console.log("rot",rot)
  72. }
  73. let bone4 = this.modelSpine.findBone("gun")
  74. if(bone4){
  75. this.attackBone4 = bone4
  76. let rot = this.attackBone4.rotation
  77. this.attackAngle4 = rot
  78. console.log("rot",rot)
  79. }
  80. // this.clickBox.setContentSize(this.modelSpine.getComponent(UITransform).contentSize)
  81. }
  82. start() {
  83. }
  84. resetData(heroData:HeroData) {
  85. this.typeID = heroData.typeID;
  86. this.level = heroData.level;
  87. this.radius = heroData.attackRadius;
  88. this.levelLabel.string = heroData.level.toString();
  89. this.node.active = true;
  90. this.modelSpine.node.active = true;
  91. let conf = UptypeConf.data
  92. let speedScale = conf.HeroSpeedColArr[this.level]
  93. this.modelSpine.timeScale = speedScale;
  94. this.stand()
  95. // let id: string = heroData.heroID;
  96. }
  97. clearData() {
  98. this.node.active = false;
  99. this.modelSpine.node.active = false;
  100. this._posID = -1;
  101. this.typeID = -1;
  102. this.level = 0;
  103. this._isLock = false;
  104. }
  105. update(deltaTime: number) {
  106. super.update(deltaTime);
  107. if(this._bAttackAnimation && isValid(this.attackNode)){
  108. let pos = this.attackNode.position;
  109. // 获取子节点的世界坐标
  110. let worldPosition = this.gunNode.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
  111. // 将子节点的世界坐标转换为父节点的局部坐标
  112. let localPosition = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPosition);
  113. // 计算从p0到p1的向量
  114. let deltaX: number = pos.x - localPosition.x;
  115. let deltaY: number = pos.y - localPosition.y;
  116. // 计算旋转角度(弧度)
  117. let rotationAngleRadians: number = Math.atan2(deltaY, deltaX);
  118. // 将旋转角度转换为度数
  119. let rotationAngleDegrees: number = (rotationAngleRadians * (180 / Math.PI)+360)%360;
  120. // console.log("旋转角度:", rotationAngleDegrees);
  121. // let scaleX = this.modelSpine.node.scale.x
  122. // if(rotationAngleDegrees > 90 && rotationAngleDegrees < 270){
  123. // // if(scaleX > 0){
  124. // // scaleX= -scaleX;
  125. // // }
  126. // // this.gunNode.angle = 180 - rotationAngleDegrees;
  127. // this.attackBone.rotation = 180 - rotationAngleDegrees
  128. // }
  129. // else{
  130. // // if(scaleX < 0){
  131. // // scaleX= -scaleX;
  132. // // }
  133. // // this.gunNode.angle = rotationAngleDegrees;
  134. // this.attackBone.rotation = rotationAngleDegrees
  135. // }
  136. // this.modelSpine.node.scale = new Vec3(scaleX,this.modelSpine.node.scale.y,this.modelSpine.node.scale.z);
  137. // console.log("旋转角度:", rotationAngleDegrees);
  138. this.attackBone1.rotation = 0
  139. this.attackBone2.rotation = 0
  140. this.attackBone3.rotation = 0
  141. this.attackBone4.rotation = 0
  142. if(rotationAngleDegrees<67.5 || rotationAngleDegrees > 292.5){
  143. this.attackBone1.rotation = rotationAngleDegrees+this.attackAngle1
  144. if(this.modelSpine.animation != "attack_right")
  145. this.modelSpine.setAnimation(0, 'attack_right', true);
  146. }
  147. else if(rotationAngleDegrees < 112.5){
  148. this.attackBone3.rotation = rotationAngleDegrees+this.attackAngle3-90
  149. if(this.modelSpine.animation != "attack_up")
  150. this.modelSpine.setAnimation(0, 'attack_up', true);
  151. }
  152. else if(rotationAngleDegrees < 247.5){
  153. this.attackBone4.rotation = rotationAngleDegrees+this.attackAngle4-180
  154. if(this.modelSpine.animation != "attack_left")
  155. this.modelSpine.setAnimation(0, 'attack_left', true);
  156. }
  157. else if(rotationAngleDegrees < 292.5){
  158. this.attackBone2.rotation = rotationAngleDegrees+this.attackAngle2-270
  159. if(this.modelSpine.animation != "attack_down")
  160. this.modelSpine.setAnimation(0, 'attack_down', true);
  161. }
  162. }
  163. }
  164. //屏幕坐标
  165. bTouch(pos: Vec2){
  166. return this.node.getComponent(UITransform).hitTest(pos,0);
  167. }
  168. get bAttackAnimation(): boolean{
  169. return this._bAttackAnimation;
  170. }
  171. //通过目标坐标 来确定朝向
  172. attack(attackNode:Node){
  173. if(!this.modelSpine.node.active) return;
  174. // if(this._bAttackAnimation) return;
  175. this._bAttackAnimation = true;
  176. this.attackNode = attackNode;
  177. Framework.audio.playEffect(AudioID.Tututu);
  178. }
  179. stand() {
  180. if(!this.modelSpine.node.active) return;
  181. this._bAttackAnimation = false;
  182. // this.gunNode.angle = 0;
  183. this.gunNode.active = true;
  184. this.attackBone1.rotation = 0
  185. this.attackBone2.rotation = 0
  186. this.attackBone3.rotation = 0
  187. this.attackBone4.rotation = 0
  188. // this._attackCallback = null;
  189. if(this.modelSpine.animation == "stand_right") return;
  190. this.modelSpine.setAnimation(0, 'stand_right', true);
  191. // if(this.modelSpine.node.scale.x < 0)
  192. // this.modelSpine.node.scale = new Vec3(-this.modelSpine.node.scale.x,this.modelSpine.node.scale.y,this.modelSpine.node.scale.z);
  193. }
  194. levelUp() {
  195. this.level++;
  196. this.levelLabel.string = this.level.toString();
  197. let conf = UptypeConf.data
  198. let speedScale = conf.HeroSpeedColArr[this.level]
  199. this.modelSpine.timeScale = speedScale;
  200. }
  201. actionCallback(trackEntry){
  202. // console.log("动作回调:",trackEntry.animation.name)
  203. if (trackEntry.animation.name && trackEntry.animation.name.includes("attack")) {
  204. // if (this._attackCallback) {
  205. // this._attackCallback(trackEntry);
  206. // }
  207. // console.log("攻击动作完成:",trackEntry.animation.name)
  208. if(this.posID > BattleUtil.PosID_Init){
  209. let eventData:BattleEventHeroAction = {
  210. action: HeroActionType.Normal,
  211. posIndex: this._posID - BattleUtil.BagListSize
  212. }
  213. this.battleEventManager.fireEvent(BattleEventTarget.HeroAction,eventData)
  214. }
  215. }
  216. }
  217. actionComplete(trackEntry:sp.spine.TrackEntry){
  218. // console.log("动作完成:",trackEntry.animation.name,this._posID)
  219. if (trackEntry.animation.name && trackEntry.animation.name.includes("fire")) {
  220. // this._bAttackAnimation = false;
  221. // // this.gunNode.angle = 0;
  222. // // this.gunNode.active = false;
  223. // this.attackBone.rotation = 0
  224. // this._attackCallback = null;
  225. }
  226. }
  227. //
  228. flyTo(pos:Vec3, callback:Function){
  229. if(this._isLock){ return false; }
  230. this.isLock = true;
  231. let length = Vec3.distance(this.node.position,pos);
  232. if(length > 300){
  233. length = 300;
  234. }
  235. tween(this.node).to(length/500,{position:pos}).call(()=>{
  236. this.isLock = false;
  237. if(callback) callback();
  238. }).start();
  239. }
  240. hitTest(pos:Vec2){
  241. return this.clickBox.hitTest(pos) || this.node.getComponent(UITransform).hitTest(pos);
  242. }
  243. }