Hero.ts 11 KB

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