Monster.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Armature from "./Armature";
  2. import Role from "./Role";
  3. const {ccclass, property} = cc._decorator;
  4. /**怪物类 */
  5. @ccclass
  6. export default class Monster extends Role {
  7. public property_damage:number = 8;
  8. public property_enduranceResumeSpeed:number = 6;
  9. public onLoad():void{
  10. //初始化骨骼
  11. this.armature =new Armature('armature/monster',this.node);
  12. //初始动画
  13. this.armature.playAnimation(Role.AnimationName_Idle,0);
  14. //获得目标对手监听,启动目标攻击事件监听
  15. this.node.on(Role.EventType_GetTarget,this.onTargetAttackCallback,this);
  16. super.onLoad();
  17. }
  18. /**目标攻击监听回调 */
  19. private onTargetAttackCallback():void{
  20. this.target.node.on(Role.EventType_DoAttack,()=>{
  21. let random = Math.random();
  22. if(random<0.5)return;//半概率不躲避
  23. //根据hp和耐力值决定出手策略
  24. let enoughHp = this.property_hp>0.6*this.property_maxHp;
  25. let enoughEndurance = this.property_endurance>0.6*this.property_maxEndurance;
  26. if(!enoughHp&&enoughEndurance){
  27. this.defense();
  28. }else if(enoughHp&&!enoughEndurance){
  29. if(this.target.direction==1){
  30. this.rightDodge();
  31. }else{
  32. this.leftDodge();
  33. }
  34. }else{
  35. if(random<0.65){
  36. this.defense();
  37. }else{
  38. if(this.target.direction==1){
  39. this.rightDodge();
  40. }else{
  41. this.leftDodge();
  42. }
  43. }
  44. }
  45. },this);
  46. }
  47. /**上一次攻击提示时间 */
  48. private lastAttackTipTime:number = 0;
  49. /**下一次攻击名称 */
  50. private nextAttack:string;
  51. /**自动闪躲攻击策略 */
  52. private autoDodgeAndAttack():void{
  53. if(this.pausing)return;
  54. let random = Math.random();
  55. let now = Date.now();
  56. if(now-this.lastAttackTipTime>400&&this.nextAttack){
  57. if(this.nextAttack=='leftJay'){
  58. this.leftJay();
  59. this.nextAttack = null;
  60. }else if(this.nextAttack=='rightJay'){
  61. this.rightJay();
  62. this.nextAttack = null;
  63. }
  64. }else if(random<0.2&&!this.nextAttack){
  65. if(random<0.05){
  66. this.leftDodge();
  67. }else if(random<0.1){
  68. this.rightDodge();
  69. }else if(random<0.2){
  70. if(random<0.15){
  71. if(now-this.lastAttackTipTime>1000){
  72. this.createMark(1);
  73. this.nextAttack = 'leftJay';
  74. this.lastAttackTipTime = now;
  75. }
  76. }else{
  77. if(now-this.lastAttackTipTime>1000){
  78. this.createMark(-1);
  79. this.nextAttack = 'rightJay';
  80. this.lastAttackTipTime = now;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. public update(dt:number):void{
  87. super.update(dt);
  88. this.autoDodgeAndAttack();
  89. }
  90. /**
  91. * 创建预备攻击提示标志
  92. * @param direction 1为左,-1为右
  93. * @param autoDestroy 自动销毁
  94. * @param callback 回调函数
  95. */
  96. private createMark(direction:number):void{
  97. let node = new cc.Node();
  98. node.setScale(3);
  99. node.setPosition(this.node.position.add(cc.v2(-250*direction,650)));
  100. node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/mark',cc.SpriteFrame);
  101. window.gameMgr.background.addChild(node);
  102. node.runAction(cc.sequence(cc.shake(0.4,5,10),cc.callFunc(()=>{
  103. node.destroy();
  104. },this)));
  105. cc.audioEngine.playEffect(cc.loader.getRes('audio/ding',cc.AudioClip),false);
  106. }
  107. }