Hero.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. import TouchMgr from "../help/TouchMgr";
  2. import EventMgr from "./EventMgr";
  3. import { GameEvent } from "./GameEvent";
  4. import { GameMode } from "./GameMode";
  5. import HeroHit from "./HeroHit";
  6. import HurtTag from "./HurtTag";
  7. import Main from "./Main";
  8. import ResMgr from "./ResMgr";
  9. import { SDKIpt } from "./SDKIpt";
  10. import SoundMgr from "./SoundMgr";
  11. import Weapon from "./Weapon";
  12. import { WeaponType } from "./WeaponType";
  13. // hero状态
  14. enum State {
  15. stand = "stand",
  16. // standAtk = "standAtk",
  17. jump = "jump",
  18. // jumpAtk = 4,
  19. dun = "dun",
  20. // holdWeapon = "holdWeapon",
  21. // dunAtk = "dunAtk",
  22. die = "die",
  23. }
  24. // 动画名称
  25. enum Anima {
  26. attack1 = 'atk1',
  27. attack2 = 'atk2',
  28. attack3 = 'atk3',
  29. attack4 = 'atk4',
  30. die = 'die',
  31. dun = 'dun',
  32. dun_atk1 = 'dun_atk1',
  33. // dun_atk2 = 'dun_atk2',
  34. /**蹲 -勾拳 */
  35. dun_hookAtk = "dun_hookAtk",
  36. idle = 'idle',
  37. jump = "jump",
  38. jump_up_atk_sdk = "jump_up_atk_sdk",
  39. weapon_atk="weapon_atk",
  40. weapon_hold="weapon_hold",
  41. superSkill = "superSkill",
  42. /**羊面向右,向左倾 */
  43. hurt_left="hurt_left",
  44. hurt_right="hurt_right",
  45. }
  46. // 输入状态
  47. export enum GameInput {
  48. none = "none" ,
  49. leftClick = "leftClick",//左踩MOTION.LEFT
  50. up = "up",//跳MOTION.JUMP
  51. // leftUp = "leftUp",//跳MOTION.JUMP
  52. leftDown = "leftDown",//左下MOTION.LEFT_DOWN
  53. rightClick = "rightClick",//右MOTION.RIGHT
  54. // rightUp = "rightUp",//跳MOTION.JUMP
  55. rightDown = "rightDown",//右下MOTION.RIGHT_DOWN
  56. down = "down",//右下MOTION.RIGHT_DOWN
  57. }
  58. // const Input = {}
  59. const {ccclass, property} = cc._decorator;
  60. @ccclass
  61. export default class Hero extends cc.Component {
  62. private weapon: Weapon = null;
  63. private hp: number;
  64. private maxHp: number;
  65. public get Hp(): number { return this.hp; }
  66. public set Hp(val:number){
  67. if (val>this.maxHp) {val = this.maxHp;}
  68. if (val<0) {val = 0;}
  69. if (this.hp!=val) {
  70. this.hp = val;
  71. EventMgr.Instance.dispatch_event(GameEvent.HeroHpChange,this.hp);
  72. }
  73. }
  74. private maxRage: number;
  75. private rage: number = 0;
  76. public get Rage(): number { return this.rage; }
  77. public set Rage(val:number){
  78. if (val>this.maxRage) {val = this.maxRage;}
  79. if (val<0) {val = 0;}
  80. if (this.rage!=val) {
  81. this.rage = val;
  82. EventMgr.Instance.dispatch_event(GameEvent.HeroRageChange,this.rage);
  83. }
  84. }
  85. touchType:string;
  86. // 速度
  87. speed: number = 0;
  88. wPos: cc.Vec2 = null;
  89. // 方向
  90. sp: cc.Vec2 = null;
  91. // hero动画
  92. heroAni: cc.Animation = null;
  93. private heroHit:HeroHit = null;
  94. // hero动画状态
  95. annima:Anima = null
  96. // 站立连击
  97. private standAtkCombo: number = 0;
  98. // 站立连击
  99. public get IsHeavy(): boolean {
  100. if (this.standAtkCombo == 3 || this.annima == Anima.jump_up_atk_sdk || this.annima==Anima.dun_atk1) {
  101. // console.error("重击");
  102. }
  103. return this.standAtkCombo == 3|| this.annima == Anima.jump_up_atk_sdk || this.annima==Anima.dun_atk1;
  104. }
  105. // /**蹲着连击 */
  106. // private dunAtkCombo: number = 0;
  107. // 刚体在世界坐标下的(线性速度)
  108. lv: cc.Vec2 = null;
  109. // LIFE-CYCLE CALLBACKS:
  110. // private inputState :Input;
  111. private rigid:cc.RigidBody;
  112. // @property(cc.Animation)
  113. // hurtTagAnim: cc.Animation = null;
  114. private hurtTag: HurtTag = null;
  115. @property(cc.Node)
  116. weaponHoldPosNode: cc.Node = null;
  117. @property(cc.Node)
  118. weaponAtkPosNode: cc.Node = null;
  119. @property(cc.Node)
  120. doctorWolfRoot: cc.Node = null;
  121. private isHeroLeftDir = false;
  122. private jump_up_atk_count:number = 0;
  123. private jump_up_atk_ing:boolean = false;
  124. private comTimerID;
  125. private _heroSate:State = State.stand;
  126. /**
  127. * 输入状态
  128. */
  129. public get heroSate():State{
  130. return this._heroSate;
  131. }
  132. public set heroSate(val)
  133. {
  134. let old = this._heroSate ;
  135. this._heroSate = val;
  136. this.heroStateChange(old,val);
  137. }
  138. private _id:string;
  139. public get id(){
  140. return this._id;
  141. }
  142. /**击杀狼数量 ,每击杀两只,涨一点怒气*/
  143. private killWolf:number =0;
  144. private lastInputState:GameInput = GameInput.none;
  145. private _inputState:GameInput = GameInput.none;
  146. /**是否正在蹲下攻击:此时屏蔽其他所有动作*/
  147. private isDunAtking:boolean = false;
  148. /**
  149. * 输入状态
  150. */
  151. public get inputState(): GameInput {
  152. return this._inputState;
  153. }
  154. public set inputState(val) {
  155. // console.log("英雄 动作变化1:"+val+" "+this.isDunAtking);
  156. if (!Main.Ins.HasGuideIptPermit(val)) {
  157. return;
  158. }
  159. let dunValid = this.isDunAtking && val != GameInput.none && val != GameInput.up;
  160. if (dunValid) { return; }
  161. // console.log("英雄 动作变化2:"+val);
  162. let old = this._inputState;
  163. this.lastInputState = old;
  164. this._inputState = val;
  165. this.inputStateChange(old, val);
  166. }
  167. init (hp:number) {
  168. // cc.director.getPhysicsManager().debugDrawFlags = 1;
  169. // console.error("生命值:"+hp);
  170. this.superSkillComb = 0;
  171. this.speed = 200
  172. this.sp = cc.v2(0, 0)
  173. this.maxHp = hp;
  174. this.Hp = Main.Ins.IsGuide ? 7 : hp;// 10;
  175. this.maxRage = 10;
  176. this.Rage = 0;
  177. this.rigid = this.node.getComponent(cc.RigidBody);
  178. this.heroAni = this.node.getChildByName('body').getComponent(cc.Animation)
  179. this.heroHit = this.node.getChildByName('body').getComponent(HeroHit)
  180. // // 动画监听 播放结束
  181. this.heroAni.on(cc.Animation.EventType.FINISHED, this.onAnimationFinish, this)
  182. this.wPos = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
  183. this.annima = Anima.idle;
  184. this.heroSate = State.stand;
  185. let hurtTagNode:cc.Node= cc.instantiate(ResMgr.Instance.getAsset("LifePrefab", "hurtTag"));
  186. hurtTagNode.parent = this.node;
  187. this.hurtTag = hurtTagNode.getComponent(HurtTag);
  188. this.hurtTag.init(true);
  189. }
  190. start () {
  191. // console.error("英雄 start:"+Main.Ins.SDKOpen);
  192. if (Main.Ins.SDKOpen) {
  193. if (cc.sys.isBrowser) {
  194. EventMgr.Instance.add_event_listenner(GameEvent.DebugSDKInput,this,this.OnDebugSDKInput);
  195. }
  196. EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  197. }else{
  198. EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_START,this,this.ON_TOUCH_START);
  199. EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_MOVE,this,this.ON_TOUCH_MOVE);
  200. EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_END,this,this.ON_TOUCH_END);
  201. EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_CANCEL,this,this.ON_TOUCH_END);
  202. }
  203. EventMgr.Instance.add_event_listenner(GameEvent.MonsterDie, this, this.OnMonsterDie);
  204. }
  205. /** 每击杀两只,涨一点怒气*/
  206. OnMonsterDie(MonsterDie: GameEvent, arg1: this, OnMonsterDie: any) {
  207. if(this.superSkillComb>=4) return
  208. this.killWolf++;
  209. if (this.killWolf==2) {
  210. this.Rage++;
  211. this.killWolf = 0;
  212. }
  213. }
  214. OnDebugSDKInput(DebugSDKInput: GameEvent, ipt: GameInput, OnDebugSDKInput: any) {
  215. this.inputState = ipt;
  216. }
  217. ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
  218. // console.error("mainview 收到 sdk 动作:"+ipt+" 界面是否激活:"+this.isActive);
  219. // if (!this.isActive) { return; }
  220. switch (ipt) {
  221. case SDKIpt.hero_down:
  222. this.inputState = GameInput.down;
  223. break;
  224. case SDKIpt.hero_left:
  225. this.inputState =GameInput.leftClick;
  226. break;
  227. case SDKIpt.hero_right://主界面确认进入游戏//y
  228. this.inputState =GameInput.rightClick;//
  229. break;
  230. case SDKIpt.hero_up:
  231. this.inputState = GameInput.up;
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. // private _onMotionLeftDown(arg0: string, _onMotionLeftDown: any, arg2: this) {
  238. // this.inputState =GameInput.leftClick;// GameInput.leftDown;
  239. // }
  240. // private _onMotionRightDown(arg0: string, _onMotionRightDown: any, arg2: this) {
  241. // this.inputState =GameInput.rightClick;// GameInput.rightDown;
  242. // }
  243. // private _onMotionJump(arg0: string, _onMotionJump: any, arg2: this) {
  244. // this.inputState = GameInput.up;
  245. // }
  246. // private _onMotionDown(arg0: string, _onMotionDown: any, arg2: this) {
  247. // this.inputState = GameInput.down;
  248. // }
  249. // private _onMotionRight(arg0: string, _onMotionRight: any, arg2: this) {
  250. // this.inputState = GameInput.rightClick;
  251. // }
  252. // private _onMotionLeft(arg0: string, _onMotionLeft: any, arg2: this) {
  253. // this.inputState = GameInput.leftClick;
  254. // }
  255. ON_TOUCH_END(TOUCH_END: string, arg1: this, ON_TOUCH_END: any) {
  256. let touchRight = this.isTouchRight();
  257. // this.log("ON_TOUCH_END:"+this.touchType+" touchRight:"+touchRight +" now state:"+ this.inputState);
  258. if (this.touchType == cc.Node.EventType.TOUCH_START) {
  259. this.inputState = touchRight?GameInput.rightClick:GameInput.leftClick;
  260. }
  261. this.touchType = cc.Node.EventType.TOUCH_END;
  262. }
  263. ON_TOUCH_MOVE(TOUCH_MOVE: string, arg1: this, ON_TOUCH_MOVE: any) {
  264. // this.log("ON_TOUCH_MOVE:"+this.touchType);
  265. if (this.touchType == cc.Node.EventType.TOUCH_MOVE) { return; }
  266. if (TouchMgr.Instance.getMoveStrength() < 5) { return; }
  267. this.touchType = cc.Node.EventType.TOUCH_MOVE;
  268. let touchRight = this.isTouchRight();
  269. let moveUp = this.isMoveUp();
  270. // if (touchRight) {//右侧
  271. // this.inputState = moveUp? GameInput.rightUp : GameInput.rightDown;
  272. // } else {
  273. // this.inputState = moveUp? GameInput.leftUp : GameInput.leftDown;
  274. // }
  275. this.inputState = GameInput.up;
  276. }
  277. ON_TOUCH_START(TOUCH_START: string, arg1: this, ON_TOUCH_START: any) {
  278. this.touchType = cc.Node.EventType.TOUCH_START;
  279. }
  280. onDestroy() {
  281. // this.heroAni.off(cc.Animation.EventType.FINISHED, this.onAnimationFinish, this);
  282. this.removeIptListen();
  283. }
  284. private removeIptListen(){
  285. EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_START,this,this.ON_TOUCH_START);
  286. EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_MOVE,this,this.ON_TOUCH_MOVE);
  287. EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_END,this,this.ON_TOUCH_END);
  288. EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_CANCEL,this,this.ON_TOUCH_END);
  289. EventMgr.Instance.remove_event_listenner(GameEvent.DebugSDKInput,this,this.OnDebugSDKInput);
  290. EventMgr.Instance.remove_event_listenner(GameEvent.MonsterDie,this,this.OnMonsterDie);
  291. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  292. }
  293. /**
  294. * 获取触摸左侧还是右侧
  295. * @returns >0?右側 <左側
  296. */
  297. isTouchRight():boolean{
  298. return this.getTouchSide()>0;
  299. }
  300. getTouchSide():number{
  301. let side;
  302. let touchWPos = TouchMgr.Instance.WPos;
  303. let touchWPosX = touchWPos.x;
  304. let delt = touchWPosX-this.wPos.x;
  305. if (Math.abs(delt)<50) {
  306. side = 0;
  307. }else if (delt<0) {
  308. side = -1;
  309. }else{
  310. side = 1;
  311. }
  312. return side;
  313. }
  314. isMoveUp(){
  315. return TouchMgr.Instance.getMoveDir()<0;
  316. }
  317. private superSkillComb:number;
  318. private timer_out_superSkill:string;
  319. /**因为每一次旧的输入状态都会置为 GameInput.none,所以用这个记录真实输入状态*/
  320. private superSkillIptState:GameInput;
  321. /**
  322. * 输入状态改变
  323. */
  324. inputStateChange(oldIpt: GameInput, newIpt: GameInput) {
  325. let me = this;
  326. if (newIpt == GameInput.down || newIpt == GameInput.up) {
  327. if (Main.Ins.TryGuideStep(3)) {
  328. Main.Ins.BallonGuide.showTop(false);
  329. Main.Ins.BallonGuide.showBottom(false);
  330. // Main.Ins.resumeGame();
  331. EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false);
  332. Main.Ins.SetGuideSteping(3);
  333. Main.Ins.SetGuideIptPermit(GameInput.down,GameInput.up,GameInput.leftClick,GameInput.rightClick);
  334. } else if (Main.Ins.TryGuideStep(4)) {
  335. Main.Ins.BallonGuide.showTop(false);
  336. EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false);
  337. Main.Ins.SetGuideSteping(4);
  338. // Main.Ins.SetGuideIptPermit();
  339. }else if (Main.Ins.TryGuideStep(5)) {
  340. Main.Ins.BallonGuide.showTop(false);
  341. EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false);
  342. Main.Ins.GuideStepEnd(5);
  343. EventMgr.Instance.dispatch_event(GameEvent.GuideState,false);
  344. }
  345. }
  346. this.error("输入状态更新-> oldIpt: " + oldIpt + " newIpt: " + newIpt);
  347. if (newIpt == GameInput.none) {return; }
  348. let isHeroLeftDir = this.isHeroLeftDir;
  349. if (this.heroSate != State.jump) {
  350. if (Main.Ins.SDKOpen) {
  351. isHeroLeftDir = newIpt == GameInput.leftClick || newIpt == GameInput.leftDown;
  352. } else {
  353. isHeroLeftDir = newIpt == GameInput.leftClick || newIpt == GameInput.leftDown || newIpt == GameInput.up;
  354. }
  355. }
  356. if (newIpt == GameInput.up || newIpt == GameInput.down) {
  357. isHeroLeftDir = this.isHeroLeftDir;
  358. }
  359. this.isHeroLeftDir = isHeroLeftDir;
  360. switch (newIpt) {
  361. case GameInput.leftClick:
  362. case GameInput.rightClick:
  363. let checkSuperSkill = function () {
  364. if (newIpt != me.superSkillIptState && (me.superSkillIptState ==GameInput.leftClick||me.superSkillIptState ==GameInput.rightClick)) {
  365. // console.error("superSkillComb:"+me.superSkillComb);
  366. if (++me.superSkillComb >= 4 && me.Rage>=me.maxRage) {
  367. //放大招
  368. // console.error("放大招1");
  369. me.heroSate = State.jump;
  370. return true;
  371. }
  372. if (me.timer_out_superSkill) { Ticker.unregisterDelay(me.timer_out_superSkill); }
  373. me.timer_out_superSkill = Ticker.registerDelay(() => {
  374. me.superSkillComb = 0;
  375. }, 3000);
  376. return false;
  377. }
  378. me.superSkillComb =1;//记录第一次
  379. return false;
  380. }
  381. if (this.heroSate == State.stand) {
  382. // this.onAttack();
  383. if (!checkSuperSkill()) {
  384. this.heroSate = State.stand;
  385. }
  386. } else if (this.heroSate == State.dun) {
  387. if (!checkSuperSkill()) {
  388. this.heroSate = State.stand;
  389. }
  390. } else if (this.heroSate == State.jump) {
  391. this.heroSate = State.jump;
  392. }
  393. break;
  394. case GameInput.leftDown:
  395. case GameInput.rightDown:
  396. case GameInput.down:
  397. me.superSkillComb =0;//归零
  398. // this.heroSate = State.dun;
  399. if (this.heroSate == State.stand) {
  400. // this.onAttack();
  401. this.heroSate = State.dun;
  402. } else if (this.heroSate == State.dun) {
  403. this.heroSate = State.dun;
  404. } else if (this.heroSate == State.jump) {
  405. this.heroSate = State.jump;
  406. }
  407. break;
  408. case GameInput.up:
  409. // case GameInput.rightUp:
  410. me.superSkillComb =0;//归零
  411. if (this.heroSate == State.stand) {
  412. // this.onAttack();
  413. this.heroSate = State.jump;
  414. } else if (this.heroSate == State.dun) {
  415. this.heroSate = State.jump;
  416. } else if (this.heroSate == State.jump) {
  417. this.heroSate = State.jump;
  418. }
  419. break;
  420. }
  421. // console.error("now:"+this.node.scaleX+" new:"+(isHeroLeftDir?-1:1));
  422. this.node.scaleX =isHeroLeftDir?-1:1;
  423. this.superSkillIptState = newIpt;
  424. this.inputState = GameInput.none;
  425. }
  426. /**
  427. * 英雄状态改变
  428. */
  429. heroStateChange(oldState: State, newState: State) {
  430. this.warn("英雄状态更新-> oldState: " + oldState + " newState: " + newState);
  431. // if (oldState==State.die) { return; }
  432. //更新状态
  433. switch (newState) {
  434. case State.die:
  435. this.onDieState(oldState);
  436. break;
  437. case State.dun:
  438. this.onDunState(oldState);
  439. break;
  440. case State.jump:
  441. this.onJumpState(oldState);
  442. break;
  443. case State.stand:
  444. this.onStandState(oldState);
  445. break;
  446. default:
  447. break;
  448. }
  449. }
  450. onStandState(oldState:State) {
  451. let me = this;
  452. let annima = Anima.idle;
  453. this.log("onStandState oldState:"+oldState+" inputState:"+this.inputState);
  454. if (this.inputState == GameInput.leftClick || this.inputState == GameInput.rightClick) {
  455. if (oldState == State.dun) {
  456. if (this.isHoldWeapon) {
  457. annima = Anima.weapon_atk;
  458. this.weaponAtk();
  459. }else{
  460. annima = Anima.dun_hookAtk;
  461. }
  462. } else if (oldState == State.stand) {
  463. // // 播放攻击动画
  464. this.log('即将播放攻击动画 站: ' + this.standAtkCombo);
  465. if (this.comTimerID) { Ticker.unregisterDelay(this.comTimerID); }
  466. if (this.standAtkCombo == 0) {
  467. annima = Anima.attack1
  468. } else if (this.standAtkCombo == 1) {
  469. annima = Anima.attack2
  470. } else if (this.standAtkCombo == 2) {
  471. annima = Anima.attack3
  472. } else if (this.standAtkCombo == 3) {
  473. annima = Anima.attack4
  474. }
  475. // else if (this.standAtkCombo == 4) {
  476. // annima = Anima.attack5
  477. // } else if (this.standAtkCombo == 5) {
  478. // annima = Anima.attack6
  479. // }
  480. if (this.isHoldWeapon) {//this.annima == Anima.weapon_hold &&
  481. annima = Anima.weapon_atk;
  482. this.weaponAtk();
  483. }
  484. }
  485. }else {
  486. if (this.isHoldWeapon) {
  487. annima = Anima.weapon_hold;
  488. this.setWeaponShow(true);
  489. }
  490. }
  491. // 修改动画
  492. this.setAnima(annima)
  493. }
  494. onDunState(oldState:State){
  495. let annima = Anima.dun;
  496. this.log("onDunState oldState:"+oldState+" inputState:"+this.inputState);
  497. if (this.inputState == GameInput.leftClick || this.inputState == GameInput.rightClick) {
  498. }else if (this.inputState == GameInput.leftDown || this.inputState == GameInput.rightDown|| this.inputState == GameInput.down) {
  499. annima = Anima.dun_atk1;
  500. // // 播放攻击动画
  501. // this.log('即将播放攻击动画 蹲:' + this.dunAtkCombo);
  502. // if (this.dunAtkCombo == 0) {
  503. // } else if (this.dunAtkCombo == 1) {
  504. // annima = Anima.dun_atk1
  505. // }
  506. }else if (this.inputState == GameInput.up)// || this.inputState == GameInput.rightUp)
  507. {
  508. }
  509. this.setWeaponShow(false);
  510. // 修改动画
  511. this.setAnima(annima);
  512. }
  513. /**
  514. * 在空中
  515. */
  516. onJumpState(oldState:State) {
  517. let annima = Anima.jump;
  518. if (this.jump_up_atk_ing) { return; }
  519. this.log("onJumpState oldState:"+oldState+" inputState:"+this.inputState);
  520. if (oldState == State.dun || oldState == State.stand) {
  521. if (this.jump_up_atk_count > 0) { return; }
  522. this.jump_up_atk_ing = true;
  523. this.jump_up_atk_count++;
  524. if (this.superSkillComb>=4) {
  525. // console.error("放大招2");
  526. this.Rage = 0;
  527. // this.superSkillComb = 0;
  528. annima = Anima.superSkill;
  529. // this.rigidLineVelocity(0,2000);
  530. // this.setAnima(Anima.superSkill);
  531. EventMgr.Instance.dispatch_event(GameEvent.SCRENN_COLOR);
  532. // Ticker.registerDelay(() => {
  533. // SoundMgr.Instance.play_effect("小羊大招");
  534. // EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,600);
  535. // }, 400);
  536. setTimeout(() => {
  537. EventMgr.Instance.dispatch_event(GameEvent.HeroSuperSkill);
  538. SoundMgr.Instance.play_effect("小羊大招");
  539. EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,600);
  540. }, 400);
  541. // Ticker.registerDelay(() => {
  542. // }, 400);
  543. }else{
  544. annima = Anima.jump_up_atk_sdk;
  545. // this.rigid.linearVelocity= cc.v2(0,500);//
  546. this.rigidLineVelocity(0,900);
  547. // this.error("跳-上击-->"+this.rigid.linearVelocity);
  548. }
  549. }
  550. else if (this.inputState == GameInput.leftDown || this.inputState == GameInput.rightDown|| this.inputState == GameInput.down) {
  551. // //往下砸
  552. // annima = Anima.dun;
  553. }
  554. this.setWeaponShow(false);
  555. // 修改动画
  556. this.setAnima(annima)
  557. }
  558. onDieState(oldState:State){
  559. if ( oldState == State.die) {
  560. return;
  561. }
  562. // 修改动画
  563. this.setAnima(Anima.die);
  564. this.setWeaponShow(false);
  565. // console.error(" hero ------------------- =英雄死了============================================");
  566. EventMgr.Instance.dispatch_event(GameEvent.HeroDie);
  567. }
  568. /**
  569. * 播放新动画
  570. * @param animiat
  571. */
  572. setAnima(animiat: Anima): void {
  573. if (!animiat || this.annima == animiat) { return;}
  574. // this.log('old {}, new {}',this.annima, animiat)
  575. this.annima = animiat
  576. // 播放新动画
  577. this.log('当前播放动画:' + animiat);
  578. // console.log('当前播放动画:' + animiat);
  579. // if (animiat==Anima.jump||animiat==Anima.jump_atk||animiat==Anima.jump_bothSide||animiat==Anima.jump_atk1||animiat==Anima.jump_up_atk_sdk) {
  580. if (animiat==Anima.jump_up_atk_sdk) {
  581. // console.error("跳起音效");
  582. SoundMgr.Instance.play_effect("小羊跳跃");
  583. }else if(animiat==Anima.dun_atk1){
  584. // console.error("跳起音效");
  585. SoundMgr.Instance.play_effect("小羊蹲下");
  586. }
  587. else if (animiat==Anima.dun_hookAtk||
  588. animiat==Anima.attack1||
  589. animiat==Anima.weapon_atk) {
  590. SoundMgr.Instance.play_effect("攻击1");
  591. }else if (animiat==Anima.attack2) {
  592. SoundMgr.Instance.play_effect("攻击2");
  593. }else if (animiat==Anima.attack3) {
  594. SoundMgr.Instance.play_effect("攻击3");
  595. }else if (animiat==Anima.attack4) {
  596. SoundMgr.Instance.play_effect("攻击4");
  597. }else if (animiat==Anima.superSkill) {
  598. }
  599. if (animiat==Anima.jump||animiat==Anima.jump_up_atk_sdk) {
  600. this.heroHit.jumpCollider();
  601. }
  602. else if (animiat==Anima.dun ||animiat==Anima.dun_atk1) {
  603. this.heroHit.dunCollider();
  604. }else if (animiat==Anima.superSkill) {
  605. this.heroHit.superSkillCollider();
  606. }else{
  607. this.heroHit.standCollider();
  608. }
  609. if (animiat == Anima.dun_atk1) {
  610. this.isDunAtking = true;
  611. }
  612. this.heroAni.play(animiat);
  613. }
  614. /**
  615. * 动画监听事件
  616. * @param e
  617. * @param data
  618. */e: cc.Event.EventKeyboard
  619. onAnimationFinish(e:string, data:cc.AnimationState) {
  620. // 重置为静态动画
  621. let animName = data.name;
  622. this.isDunAtking = false;
  623. this.log('动画结束:'+animName+" standAtkCombo:"+this.standAtkCombo ,true);
  624. if (animName == Anima.attack1 || animName == Anima.attack2 || animName == Anima.attack3||
  625. animName == Anima.attack4 ) {
  626. this.heroSate = State.stand;
  627. // 连击判定
  628. this.standAtkCombo = (this.standAtkCombo + 1) % 4;
  629. this.comTimerID= Ticker.registerDelay(() => {
  630. if (this.heroSate == State.stand) {
  631. this.heroSate = State.stand;
  632. }
  633. // if (this.heroSate == State.stand) { return; }
  634. this.standAtkCombo = 0
  635. }, 1000)
  636. }else if (animName == Anima.dun_atk1) {
  637. this.heroSate = State.dun;
  638. // // 连击判定
  639. // this.dunAtkCombo = (this.dunAtkCombo + 1) % 2;
  640. // this.comTimerID=Ticker.registerDelay(()=> {
  641. // if (this.heroSate == State.dun) {
  642. // this.heroSate = State.dun;
  643. // }
  644. // // if(this.heroSate == State.dun) { return; }
  645. // this.dunAtkCombo = 0
  646. // }, 1000)
  647. } else {
  648. if ( animName == Anima.jump_up_atk_sdk|| animName == Anima.superSkill) {
  649. this.jump_up_atk_ing = false;
  650. }
  651. this.heroSate = this.heroSate;
  652. }
  653. }
  654. onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
  655. let otherGroup = otherCollider.node.group;
  656. if (otherGroup == 'ground') {
  657. if (this.heroSate == State.jump) {
  658. this.log("onBeginContact jump:"+this.annima);
  659. if (this.annima == Anima.dun) {
  660. this.heroSate = State.dun;
  661. } else if(this.annima == Anima.jump)//|| this.annima == Anima.superSkill)
  662. {
  663. this.heroSate = State.stand;
  664. }
  665. if (this.annima != Anima.superSkill) {
  666. this.jump_up_atk_ing = false;
  667. this.jump_up_atk_count = 0;
  668. }
  669. }
  670. }
  671. }
  672. /**
  673. *
  674. * @param isEnemyInleft 敌人是否在左边
  675. * @param weapon 如果是被武器击中的话,需要掉落武器
  676. * @returns 已经死了,或者在跳跃状态不能受击
  677. */
  678. OnHurt(isEnemyInleft:boolean,weapon:Weapon = null){
  679. if (this.heroSate == State.die ||this.heroSate == State.jump) {
  680. return false;
  681. }
  682. if (weapon) {//被武器击中
  683. let dropX;
  684. if (this.isFaceLeft)
  685. {
  686. dropX = isEnemyInleft?100:-100;
  687. }else{ //面向右
  688. dropX = isEnemyInleft?-100:100;
  689. }
  690. let wPos = this.node.convertToWorldSpaceAR(cc.v2(dropX, 150));
  691. //武器掉落
  692. weapon.Drop(wPos);
  693. }
  694. SoundMgr.Instance.play_effect("小猪受攻");
  695. if (--this.Hp==0) {
  696. if (Main.Ins.IsGuide) {
  697. this.Hp = 1;
  698. }else{
  699. this.node.scaleX = isEnemyInleft?-1:1;
  700. this.heroSate = State.die;
  701. this.removeIptListen();
  702. }
  703. }else{
  704. if (this.heroSate !=State.dun) {
  705. let hurtAnim;
  706. if (this.isFaceLeft)
  707. {
  708. hurtAnim = isEnemyInleft?Anima.hurt_left:Anima.hurt_right;
  709. }else{ //面向右
  710. hurtAnim = isEnemyInleft?Anima.hurt_right:Anima.hurt_left;
  711. }
  712. this.setAnima(hurtAnim);
  713. }
  714. //加怒气
  715. // this.Rage++;
  716. }
  717. if (this.Hp < 0) { this.Hp = 0; }
  718. this.hurtTag.play(isEnemyInleft);
  719. // EventMgr.Instance.dispatch_event(GameEvent.HeroHpChange,this.Hp);
  720. EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,400);
  721. EventMgr.Instance.dispatch_event(GameEvent.HERO_HURT,1);
  722. return true;
  723. }
  724. /**无尽模式失败 */
  725. forceDie(){
  726. SoundMgr.Instance.play_effect("小猪受攻");
  727. // this.node.scaleX = isEnemyInleft?-1:1;
  728. this.heroSate = State.die;
  729. this.removeIptListen();
  730. }
  731. private isHoldWeapon:boolean = false;
  732. /**判断并执行击飞武器 */
  733. public AtkWeapon(weapon: Weapon): boolean {
  734. //手上有手持武器,把武器击飞
  735. if (this.isHoldWeapon && this.weapon.WeaponType == WeaponType.hold) {
  736. weapon.heroAtkWeapon(this.WPos.x < weapon.WPos.x, this.id);
  737. return true;
  738. }
  739. // else {//手上没有手持武器,先受击,,再掉落武器//再拾起武器
  740. // let eWPosX = weapon.WPos.x;
  741. // let hWposX = this.WPos.x;
  742. // let weaponInLeft = eWPosX < hWposX;
  743. // if (!this.OnHurt(weaponInLeft, weapon) || this.heroSate == State.die) {
  744. // }
  745. // return;
  746. // }
  747. return false;
  748. }
  749. public OnHoldWeapon(weapon:Weapon):boolean{
  750. // this.heroSate = State.holdWeapon;
  751. // if (weapon.State != WeaponState.drop) //武器不是出于掉落状态
  752. // {
  753. // //手上有手持武器,把武器击飞
  754. // if (this.isHoldWeapon && this.weapon.WeaponType == WeaponType.hold) {
  755. // weapon.heroAtkWeapon(this.WPos.x < weapon.WPos.x, this.id);
  756. // return;
  757. // } else {//手上没有手持武器,先受击,,再掉落武器//再拾起武器
  758. // let eWPosX = weapon.WPos.x;
  759. // let hWposX = this.WPos.x;
  760. // let weaponInLeft = eWPosX < hWposX;
  761. // if (!this.OnHurt(weaponInLeft, weapon) || this.heroSate == State.die) {
  762. // }
  763. // return;
  764. // }
  765. // }
  766. if (this.isHoldWeapon) {
  767. return false;
  768. }
  769. this.log('获得武器 ');
  770. weapon.node.active = false;
  771. this.isHoldWeapon = true;
  772. weapon.HeroHold();
  773. this.weapon = weapon;
  774. this.weapon.node.parent= this.weaponHoldPosNode;
  775. this.weapon.node.position = cc.Vec3.ZERO;
  776. this.weapon.node.group = "heroWeapon";
  777. // this.weapon.node.active = false;
  778. // this.weapon.node.active = true;
  779. return true;
  780. }
  781. private weaponAtk(){
  782. if (this.weapon)
  783. {
  784. if (this.weapon.WeaponType == WeaponType.throw) {
  785. this.throwWeapon();
  786. }else{
  787. // this.bigWeaponAtk();
  788. }
  789. }
  790. }
  791. private throwWeapon(){
  792. this.weapon.node.parent= this.node.parent;
  793. this.weapon.node.active = true;
  794. let wPos = this.weaponAtkPosNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
  795. let lPos = this.weapon.node.parent.convertToNodeSpaceAR(wPos);
  796. this.weapon.node.position = cc.v3(lPos.x,lPos.y,0);
  797. // return;
  798. this.weapon.HeroThrow(this.isFaceLeft,this.id);
  799. this.weapon = null;
  800. this.isHoldWeapon = false;
  801. }
  802. /**
  803. * 是否大武器击中
  804. * @returns true:是大武器击中;false 不是大武器击中
  805. */
  806. public bigWeaponAtk():boolean{
  807. if (this.weapon && this.weapon.WeaponType == WeaponType.hold) {
  808. this.weapon.WeaponType = WeaponType.throw;
  809. // let wPos = this.weaponAtkPosNode.convertToWorldSpaceAR(cc.v2(0, 0));
  810. let wPos = this.node.convertToWorldSpaceAR(cc.v2(20, 150));
  811. // this.weapon.node.group = ""
  812. this.weapon.node.parent = this.node.parent;
  813. this.weapon.Drop(wPos);
  814. this.weapon = null;
  815. this.isHoldWeapon = false;
  816. return true;
  817. }
  818. return false;
  819. }
  820. private setWeaponShow(isShow: boolean) {
  821. if (this.isHoldWeapon && this.weapon) {
  822. if (this.weapon.node) {
  823. this.weapon.node.active = isShow;
  824. this.weapon.node.parent = this.weaponHoldPosNode;
  825. this.weapon.node.position = cc.Vec3.ZERO;
  826. } else {
  827. this.isHoldWeapon = false;
  828. this.weapon = null;
  829. }
  830. }
  831. }
  832. private get isFaceLeft(): boolean {
  833. // return this.node.scaleX < 0;
  834. return this.isHeroLeftDir;
  835. }
  836. public get WPos(){
  837. return this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
  838. }
  839. rigidLineVelocity(x,y){
  840. // console.error("rigidLineVelocity->x:"+x+" y:"+y);
  841. this.rigid.linearVelocity = cc.v2(x,y);
  842. }
  843. public Cure(doctor: cc.Node, hp: number = 1) {
  844. let me = this;
  845. // console.error("治疗");
  846. doctor.group = 'hero';
  847. doctor.active = false;
  848. doctor.active = true;
  849. doctor.parent = this.doctorWolfRoot;
  850. // .addChild(doctor);
  851. doctor.position = cc.v3(0, 0, 0);
  852. this.Hp += hp;
  853. Ticker.registerDelay(() => {
  854. doctor.destroy();
  855. }, 1000);
  856. }
  857. protected onEnable(): void {
  858. if (this.rigid) {
  859. // this.rigid.awake = true;
  860. this.rigidLineVelocity(0,0);
  861. }
  862. }
  863. protected onDisable(): void {
  864. if (this.rigid) {
  865. // this.rigid.awake = false;
  866. }
  867. }
  868. private log(msg,isShow = false){
  869. // if (!isShow) {return; }
  870. return;
  871. console.log("hero-->"+msg)
  872. }
  873. private warn(msg,isShow = false){
  874. // if (!isShow) {return; }
  875. return;
  876. console.warn("hero-->"+msg)
  877. }
  878. private error(msg,isShow = false){
  879. // if (!isShow) {return; }
  880. return;
  881. console.error("hero-->"+msg)
  882. }
  883. }