Enemy.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { _decorator, Component, Node, sp, UITransform, Vec2, Vec3 ,Animation, Color, tween} from 'cc';
  2. import { ObjectUtil } from '../../../../framework/util/ObjectUtil';
  3. import { BattleNodeBase } from './BattleNodeBase';
  4. import { RenderPriority } from '../data/BattleEnum';
  5. const { ccclass, property } = _decorator;
  6. // 敌人基类
  7. @ccclass('Enemy')
  8. export class Enemy extends BattleNodeBase {
  9. @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
  10. modelSpine: sp.Skeleton = null;
  11. @property({ type: Node, tooltip: '血条' })
  12. lifeNode: Node = null;
  13. private _bHurtAnimation: boolean = false;
  14. life: number = 0;
  15. lifeMax: number = 0;
  16. speedVector: Vec2 = new Vec2(0, 0);
  17. hurtColor = new Color(255,100,100,255)
  18. hurtTime = 0
  19. bStand: boolean = false;
  20. dieCallback: Function = null;
  21. protected onLoad(): void {
  22. this.priority = RenderPriority.Enemy;
  23. this.modelSpine.setCompleteListener(this.actionComplete.bind(this))
  24. }
  25. start() {
  26. }
  27. //用自定义更新,不用系统更新
  28. battleUpdate(deltaTime: number) {
  29. if(this.hurtTime > 0){
  30. this.hurtTime -= deltaTime
  31. if(this.hurtTime <= 0){
  32. this.modelSpine.color = Color.WHITE;
  33. this._bHurtAnimation = false
  34. }
  35. }
  36. if((!this.bStand) && (this.life > 0)){
  37. this.node.position = new Vec3(this.node.position.x+this.speedVector.x*deltaTime,this.node.position.y+this.speedVector.y*deltaTime,0)
  38. }
  39. }
  40. resetData(typeID:number,ID:number,pos:Vec3,life:number,lifeMax:number) {
  41. this.typeID = typeID;
  42. this.ID = ID;
  43. this.node.position = pos
  44. this.lifeNode.scale = new Vec3(1,1,1)
  45. this.life = life;
  46. this.lifeMax = lifeMax;
  47. this.bStand = false;
  48. this.dieCallback = null;
  49. this.updateLife()
  50. }
  51. startMove(animationName:string){
  52. this.node.active = true;
  53. this.modelSpine.setAnimation(0, 'idle', true);
  54. this.modelSpine.color = Color.WHITE;
  55. // this.lineAnimation.play(animationName);
  56. }
  57. hurt(value:number) {
  58. this.life -= value;
  59. this.hurtTime = 0.5
  60. this.updateLife()
  61. if(this._bHurtAnimation) return
  62. this._bHurtAnimation = true
  63. // this.modelSpine.setAnimation(0, 'hurt', false);
  64. this.modelSpine.color = this.hurtColor;
  65. }
  66. stand() {
  67. this.bStand = true;
  68. if(!this.modelSpine.node.active) return;
  69. this._bHurtAnimation = false;
  70. this.modelSpine.setAnimation(0, 'idle', true);
  71. this.modelSpine.color = Color.WHITE;
  72. // this._attackCallback = null;
  73. }
  74. updateLife(){
  75. if(this.life < 0) {
  76. this.lifeNode.scale = new Vec3(0,1,1);
  77. }
  78. else {
  79. this.lifeNode.scale = new Vec3(this.life / this.lifeMax,1,1);
  80. }
  81. }
  82. clearData() {
  83. this.node.active = false
  84. }
  85. die(callback?:Function) {
  86. this.dieCallback = callback;
  87. tween(this.node).delay(0.5).call(() => {
  88. this.node.active = false
  89. this.modelSpine.color = Color.WHITE;
  90. this.dieCallback && this.dieCallback()
  91. }).start()
  92. }
  93. actionComplete(trackEntry:sp.spine.TrackEntry){
  94. // console.log("动作完成:",trackEntry.animation.name,this._posID)
  95. if (trackEntry.animation.name && trackEntry.animation.name.includes("hurt")) {
  96. this._bHurtAnimation = false;
  97. this.modelSpine.addAnimation(0, 'idle', true);
  98. this.modelSpine.color = Color.WHITE;
  99. }
  100. }
  101. }