Monster2.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import mainView from "../view/mainView";
  2. import Main from "./Main";
  3. import Monster1 from "./Monster1";
  4. import { MonsterAnima, MonsterFace, MonsterState } from "./MonsterBase";
  5. import Weapon from "./Weapon";
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class Monster2 extends Monster1 {
  9. // hero状态
  10. @property(cc.Node)
  11. weaponAtkPosNode: cc.Node = null;
  12. // hero状态
  13. @property(cc.Prefab)
  14. weaponPrefab: cc.Prefab = null;
  15. /**武器扔出初始位置 */
  16. private weaponAtkLPos = cc.v2(100, 250);
  17. private timerId_move;
  18. private _walkAnim;
  19. protected get walkAnim() {
  20. return this._walkAnim;
  21. }
  22. /**准备扔武器了,不要动 */
  23. private prethrowWeapon:boolean = true;
  24. protected init(): void {
  25. super.init();
  26. let me = this;
  27. this._walkAnim = "walk2";
  28. this.speed = Util.random(100,140);
  29. }
  30. protected enemyAction(): void {
  31. super.enemyAction();
  32. let me = this;
  33. // if ( me.prethrowWeapon) {
  34. // this.state = MonsterState.none;
  35. // }
  36. if (me.prethrowWeapon && this.heroDisAbsolut<cc.view.getVisibleSize().width*0.5-150)//扔武器
  37. {
  38. me.prethrowWeapon = false;
  39. this.state = MonsterState.none;
  40. me.dirX = 0;
  41. me.setSpeed();
  42. me.throwWeapon();
  43. this.timerId_move = Ticker.registerDelay(() => {
  44. me._walkAnim = MonsterAnima.walk;
  45. me.speed = me.initSpeed;
  46. this.state = MonsterState.move;
  47. }, 150);
  48. }
  49. }
  50. private throwWeapon(){
  51. this.setAnima("throwWeapon");
  52. let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon);
  53. let wPos =this.node.convertToWorldSpaceAR(cc.v2(130,120));// this.weaponAtkPosNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
  54. weapon.node.parent= this.node.parent;
  55. let lPos = weapon.node.parent.convertToNodeSpaceAR(wPos);
  56. weapon.node.group = "monsterWeapon";
  57. weapon.node.active = false;
  58. weapon.node.active = true;
  59. // console.error(lPos);
  60. weapon.node.position = cc.v3(lPos.x,lPos.y,0);
  61. // return;
  62. weapon.MonstThrow(this.monsteFace == MonsterFace.left,this.id);
  63. }
  64. // /**
  65. // * 掉落武器
  66. // */
  67. // private dropWeapon() {
  68. // if (Math.random() < 0.3) {
  69. // let wPos = this.node.convertToWorldSpaceAR(cc.v2(100, 150));
  70. // // this("掉落武器:" + this.monsteFace + " wPos:" + wPos);
  71. // // this.tmp.parent = this.node.parent;
  72. // // let lPos = this.tmp.parent.convertToNodeSpaceAR(wPos);
  73. // // this.tmp.position = cc.v3(lPos.x,lPos.y,0);
  74. // // this.tmp.active = true;
  75. // let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon);
  76. // weapon.node.parent = this.node.parent;
  77. // weapon.SetWPos(wPos);
  78. // weapon.Drop();
  79. // }
  80. // }
  81. protected dropWeapon(): void {
  82. if (Math.random() < 0.3) {
  83. let wPos = this.node.convertToWorldSpaceAR(this.weaponAtkLPos);
  84. let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon);
  85. weapon.node.parent = this.node.parent;
  86. // weapon.SetWPos(wPos);
  87. weapon.Drop(wPos);
  88. }
  89. }
  90. protected moveInScreen(): void {
  91. super.moveInScreen();
  92. if (this.state == MonsterState.dead || this.state == MonsterState.win) {
  93. return;
  94. }
  95. // console.error("出现在屏幕中了");
  96. let me = this;
  97. Ticker.registerDelay(() => {
  98. // me.startThrowWeapon();
  99. // this.state = MonsterState.none;
  100. // me.prethrowWeapon = true;
  101. // me.dirX = 0;
  102. // me.setSpeed();
  103. // me.throwWeapon();
  104. // this.timerId_move = Ticker.registerDelay(() => {
  105. // me._walkAnim = MonsterAnima.walk;
  106. // me.prethrowWeapon = false;
  107. // me.speed = me.initSpeed;
  108. // this.state = MonsterState.move;
  109. // }, 100);
  110. }, 800);
  111. }
  112. // protected onHurtDie(): void {
  113. // super.onHurtDie();
  114. // Ticker.unregisterDelay(this.timerId_move);
  115. // }
  116. protected onHurt(hurtLeft:boolean, isHeavy:boolean, weaponHurt:boolean,mustKill:boolean) {
  117. Ticker.unregisterDelay(this.timerId_move);
  118. super.onHurt(hurtLeft,isHeavy,weaponHurt,mustKill);
  119. if (!this.prethrowWeapon) {
  120. this.speed = this.initSpeed;
  121. }
  122. // if (this.hp>0) {
  123. // this.state = MonsterState.move;
  124. // }
  125. }
  126. }