FenceBase.ts 2.6 KB

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