Hero.ts 10 KB

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