WolfRush.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import EventMgr from "./EventMgr";
  2. import { GameEvent } from "./GameEvent";
  3. import { GameState } from "./GameState";
  4. import { GameInput } from "./Hero";
  5. import Main from "./Main";
  6. import Monster1 from "./Monster1";
  7. import { MonsterAnima, MonsterFace, MonsterState } from "./MonsterBase";
  8. import Weapon from "./Weapon";
  9. const {ccclass, property} = cc._decorator;
  10. /**
  11. * 冲刺农夫狼
  12. */
  13. @ccclass
  14. export default class WolfRush extends Monster1 {
  15. // hero状态
  16. @property(cc.Node)
  17. weaponAtkPosNode: cc.Node = null;
  18. // hero状态
  19. @property(cc.Prefab)
  20. weaponPrefab: cc.Prefab = null;
  21. private rushSpeed: number = 400;
  22. /**漫游 速度 */
  23. private wanderSpeed: number = 100;
  24. private isRushState: boolean = true;
  25. private timer_itv_wander: string;
  26. private timer_to_rush: string;
  27. private _walkAnim;
  28. protected get walkAnim() {
  29. return this._walkAnim;
  30. }
  31. protected init(): void {
  32. super.init();
  33. let me = this;
  34. this._walkAnim = MonsterAnima.walk;
  35. // this.speed = Util.random(100,140);
  36. // Ticker.registerDelay(() => {
  37. me._walkAnim = "rush";// MonsterAnima.walk;
  38. me.speed = me.rushSpeed;// me.initSpeed;
  39. this.state = MonsterState.move;
  40. // me.rushCheck();
  41. Main.Ins.guideLog("rush-init");
  42. // if (Main.Ins.IsGuide) {
  43. // EventMgr.Instance.add_event_listenner(GameEvent.GuideStepEnd, this, this.OnGuideStepEnd);
  44. // }
  45. // }, 800);
  46. }
  47. protected start(): void {
  48. this.initColor();
  49. }
  50. // OnGuideStepEnd(GuideStepEnd: GameEvent, guide) {
  51. // if (guide == 4) {
  52. // this.repuseMove();
  53. // }
  54. // }
  55. protected updateAction(dt):void{
  56. if (this.isRushState) //冲锋状态
  57. {
  58. if ( Main.Ins.TryGuideStep(4) && Main.Ins.GuideSteping!=4) {
  59. if (this.heroDisAbsolut <240) {
  60. Main.Ins.BallonGuide.showTop(true,"跳跃躲避攻击");
  61. Main.Ins.SetGuideIptPermit(GameInput.up);
  62. EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,true);
  63. // this.stopMove();
  64. // Main.Ins.pauseGame();
  65. // Main.Ins.guideView.show("跳跃或蹲下躲避攻击");
  66. }
  67. }
  68. }else{//普通状态
  69. super.updateAction(dt);
  70. }
  71. }
  72. protected onHurtSpeed(): void {
  73. if (!this.isRushState) {
  74. super.onHurtSpeed();
  75. }
  76. }
  77. protected onHurtStillLive(isHeavy: any, weaponHurt: any): void {
  78. if (this.isRushState) {
  79. if (weaponHurt) {
  80. this.changeToNormal();
  81. super.onHurtStillLive(true, weaponHurt);
  82. }
  83. } else {
  84. super.onHurtStillLive(isHeavy, weaponHurt);
  85. }
  86. }
  87. changeToNormal(){
  88. this.isRushState = false;
  89. this.speed = this.initSpeed;
  90. // /**击退*/
  91. this._walkAnim = MonsterAnima.walk;
  92. this.setBlink(false);
  93. }
  94. protected dropWeapon(): void {
  95. // if (Math.random() < 0.3) {
  96. if (this.isRushState) {
  97. let wPos = this.DropWPos;// this.node.convertToWorldSpaceAR(cc.v2(100, 150));
  98. let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon);
  99. weapon.node.parent = this.node.parent;
  100. weapon.Drop(wPos);
  101. }
  102. }
  103. private get DropWPos(){
  104. return this.node.convertToWorldSpaceAR(cc.v2(100, 150));
  105. }
  106. onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
  107. super.onBeginContact(contact, selfCollider, otherCollider);
  108. if (this.isRushState) {
  109. if (otherCollider.node.group == 'board') {
  110. Main.Ins.guideLog("rush-board");
  111. if (this.rushRound >= 1) {
  112. if (Main.Ins.IsGuide) {
  113. Main.Ins.SetGuideIptPermit();
  114. Main.Ins.GuideStepEnd(4);
  115. Main.Ins.Guide5();
  116. this.node.destroy();
  117. } else {
  118. this.changeToNormal();
  119. }
  120. } else {
  121. this.rushCheck();
  122. }
  123. }
  124. }
  125. }
  126. private rushRound: number = 0;
  127. rushCheck() {
  128. let me = this;
  129. if (Main.Ins.State == GameState.win || Main.Ins.State == GameState.fail) { return; }
  130. // if (me.dirX == 0) { return; }
  131. let monsteFace = this.heroDist > 0 ? MonsterFace.right : MonsterFace.left;
  132. me.monsteFace = monsteFace;
  133. me.faceHero();
  134. me.dirX = 0;
  135. me.setSpeed();
  136. Ticker.unregisterDelay(me.timer_to_rush);
  137. me.timer_to_rush = Ticker.registerDelay(() => {
  138. if (me.monsteFace == MonsterFace.left) {
  139. // <--
  140. me.dirX = -1;
  141. } else {
  142. me.dirX = 1;
  143. }
  144. me.rushRound++;
  145. me.setSpeed(me.rushSpeed);
  146. }, 500);
  147. }
  148. OnHeroDie() {
  149. // super.OnHeroDie();
  150. //开始漫步
  151. let me = this;
  152. // this.faceHero
  153. this.setAnima(MonsterAnima.walk);
  154. Ticker.unregisterDelay(me.timer_to_rush);
  155. let wanderFun = function () {
  156. me.dirX = me.dirX < 0 ? 1 : -1;
  157. let scaleX = Math.abs(me.node.scaleX)
  158. if (me.dirX < 0) {
  159. me.node.scaleX = -scaleX
  160. } else {
  161. me.node.scaleX = scaleX
  162. }
  163. me.setSpeed(me.wanderSpeed);
  164. }
  165. wanderFun();
  166. me.timer_itv_wander = Ticker.registerDelay(() => {
  167. wanderFun();
  168. }, Util.random(1000,3000),0);
  169. }
  170. private _material;
  171. private realVal:number = 0;
  172. private maxVal:number = 5;
  173. private dir:number = 10;
  174. private isBlink:boolean = false;
  175. private initColor(){
  176. // 获取材质
  177. if (this.hitCom.node.getComponent(cc.Sprite)) {
  178. this._material = this.hitCom.node.getComponent(cc.Sprite).getMaterial(0);
  179. if (this._material) {
  180. // 设置材质对应的属性
  181. this._material.setProperty("uSize", this.realVal);
  182. this._material.setProperty("uGlowColor", cc.color(255,208,75));
  183. Ticker.register(this.blinkTickFun,this);
  184. this.setBlink(true);
  185. }
  186. }
  187. }
  188. blinkTickFun(dt) {
  189. if (!this.isBlink) {return;}
  190. this.realVal+=dt * this.dir;
  191. if (this.realVal >= this.maxVal || this.realVal <= 0) {
  192. this.dir = -this.dir;
  193. // if (this.blinkCount-- <= 0) {
  194. // this.realVal = 0;
  195. // this.isBlink = false;
  196. // }
  197. }
  198. this._material.setProperty("uSize", this.realVal);
  199. }
  200. private setBlink(isBlink:boolean){
  201. this.isBlink = isBlink;
  202. if (!this._material) {
  203. this.isBlink = false;
  204. }
  205. // console.error(this._material)
  206. if (!this.isBlink) {
  207. this._material.setProperty("uSize", 0);
  208. }
  209. // console.error("设置 setBlink "+isBlink+" "+ this.isBlink)
  210. }
  211. onDestroy(){
  212. super.onDestroy();
  213. Ticker.unregisterDelay(this.timer_itv_wander);
  214. Ticker.unregisterDelay(this.timer_to_rush);
  215. Ticker.unregister(this.blinkTickFun,this);
  216. // EventMgr.Instance.remove_event_listenner(GameEvent.GuideStepEnd, this, this.OnGuideStepEnd);
  217. super.onDestroy();
  218. }
  219. }