FenceBase.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { _decorator, Component, Node, AnimationComponent } from 'cc';
  2. import CCSAnimationUtils from '../../../../engines/utils/CCSAnimationUtils';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('FenceBase')
  5. export class FenceBase extends Component {
  6. hp:number;
  7. maxHp:number;
  8. private animation:AnimationComponent;
  9. /**
  10. * 状态 0 站立 1 受伤 2 死亡
  11. */
  12. private state:number=0;
  13. start () {
  14. // Your initialization goes here.
  15. this.animation=this.getComponent(AnimationComponent);
  16. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.config.idleAnimation,this.config.idleAnimationSpeed);
  17. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.config.damageAnimation,this.config.damageAnimationSpeed);
  18. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.config.dieAnimation,this.config.dieAnimationSpeed);
  19. }
  20. set config(value:any){
  21. this.__config=value;
  22. this.hp=this.maxHp=this.config.hp;
  23. }
  24. private __config:any;
  25. get config():any{
  26. return this.__config;
  27. }
  28. /**
  29. * 闲置
  30. */
  31. Stance():void{
  32. if(this.state==0){
  33. return;
  34. }
  35. this.state=0;
  36. this.animation.play(this.config.idleAnimation);
  37. }
  38. /**
  39. * 受伤
  40. * @param value
  41. */
  42. Damage(value:number):void{
  43. if(this.IsDie){
  44. throw new Error("栅栏已经死了但是还受到了攻击!");
  45. }
  46. this.hp-=value;
  47. if(this.hp<=0){
  48. this.hp=0;
  49. this.Die();
  50. }else{
  51. this.state=1;
  52. this.removeAllAnimationCallBack();
  53. this.animation.on(AnimationComponent.EventType.FINISHED,this.DamageComplete,this,true);
  54. this.animation.play(this.config.damageAnimation);
  55. }
  56. }
  57. private DamageComplete():void{
  58. if(this.state==2){
  59. return;
  60. }
  61. this.Stance();
  62. }
  63. /**
  64. * 死亡
  65. */
  66. Die():void{
  67. if(this.state==2){
  68. return;
  69. }
  70. this.state=2;
  71. this.removeAllAnimationCallBack();
  72. this.animation.on(AnimationComponent.EventType.FINISHED,this.DieComplete,this,true);
  73. this.animation.play(this.config.dieAnimation);
  74. }
  75. private DieComplete():void{
  76. this.node.destroy();
  77. }
  78. get IsDie():boolean{
  79. if(this.hp<=0){
  80. return true;
  81. }
  82. return false;
  83. }
  84. private removeAllAnimationCallBack():void{
  85. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DamageComplete,this)){
  86. this.animation.off(AnimationComponent.EventType.FINISHED,this.DamageComplete,this);
  87. }
  88. }
  89. // update (deltaTime: number) {
  90. // }
  91. }