Hero.ts 11 KB

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