Weapon.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import EventMgr from "./EventMgr";
  2. import { GameEvent } from "./GameEvent";
  3. import Hero, { GameInput } from "./Hero";
  4. import HeroHit from "./HeroHit";
  5. import Main from "./Main";
  6. import MonsterBase from "./MonsterBase";
  7. import { WeaponState } from "./WeaponState";
  8. import { WeaponType } from "./WeaponType";
  9. const {ccclass, property} = cc._decorator;
  10. // 动画名称
  11. enum Anima {
  12. /**武器被英雄手持 */
  13. holdInHero = 'holdInHero',
  14. /**武器扔出去路上 */
  15. move = 'move',
  16. /**击中在敌人身上 */
  17. hitInEnemy = 'hitInEnemy',
  18. /**武器掉落 */
  19. drop = 'drop',
  20. }
  21. @ccclass
  22. export default class Weapon extends cc.Component {
  23. @property(cc.Node)
  24. canAttachTag: cc.Node = null;
  25. @property(cc.Node)
  26. warnTag: cc.Node = null;
  27. private state:WeaponState;
  28. public get State():WeaponState
  29. {
  30. return this.state;
  31. }
  32. public set State(val:WeaponState)
  33. {
  34. // console.error("weapon state:"+val);
  35. this.state = val;
  36. }
  37. protected weaponType:WeaponType = WeaponType.throw;
  38. public get WeaponType():WeaponType
  39. {
  40. return this.weaponType;
  41. }
  42. public set WeaponType(val:WeaponType)
  43. {
  44. this.weaponType = val;
  45. }
  46. protected canPick:boolean;
  47. public get CanPick():boolean
  48. {
  49. return this.canPick;
  50. }
  51. protected isSafe:boolean;
  52. public get IsSafe():boolean
  53. {
  54. return this.isSafe;
  55. }
  56. @property(cc.Animation)
  57. animCom: cc.Animation = null;
  58. @property(cc.RigidBody)
  59. private rigidBody:cc.RigidBody = null;
  60. annima:string = null
  61. private speed:number = 400;
  62. // 玩家节点
  63. playerNode: cc.Node = null;
  64. private parentId:string;
  65. public get WPos(){
  66. return this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
  67. }
  68. protected get handAnim():string {
  69. return Anima.holdInHero.toString();
  70. }
  71. protected get dropAnim():string {
  72. return Anima.drop.toString();
  73. }
  74. /**
  75. * 擊中需要附在敵人臉上
  76. */
  77. public IsAttatch: boolean = true;
  78. // LIFE-CYCLE CALLBACKS:
  79. /**运动方向 -1:左,1:右 */
  80. private moveDir:number;
  81. protected onLoad () {
  82. this.State = WeaponState.none;
  83. this.node.scale = 1.3;
  84. EventMgr.Instance.add_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
  85. Ticker.register(this.tickFun,this);
  86. if (Main.Ins.IsGuide) {
  87. EventMgr.Instance.add_event_listenner(GameEvent.GuideStepWolfStop, this, this.OnGuideStepWolfStop);
  88. }
  89. }
  90. OnGuideStepWolfStop(GuideStepEnd: GameEvent, isStop) {
  91. // if (guide == 5) {
  92. // this.stopMove();
  93. // }
  94. if (isStop) {
  95. this.stopMove();
  96. }else{
  97. this.repuseMove();
  98. }
  99. }
  100. private isStop: boolean = false;
  101. private stopSpeed: number;
  102. private stopGravityScale:number;
  103. public stopMove() {
  104. this.isStop = true;
  105. this.stopGravityScale = this.rigidBody.gravityScale;
  106. this.stopSpeed = this.rigidBody.linearVelocity.x;
  107. this.rigidBody.linearVelocity = cc.v2( 0,0);
  108. }
  109. public repuseMove() {
  110. this.isStop = false;
  111. this.rigidBody.gravityScale = this.stopGravityScale;
  112. this.rigidBody.linearVelocity = cc.v2(this.stopSpeed, 0);
  113. }
  114. OnHeroSuperSkill(HeroSuperSkill: GameEvent, arg1: this, OnHeroSuperSkill: any) {
  115. this.node.destroy();
  116. }
  117. onDestroy() {
  118. this.logErr("武器 销毁");
  119. EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
  120. EventMgr.Instance.remove_event_listenner(GameEvent.GuideStepWolfStop, this, this.OnGuideStepWolfStop);
  121. Ticker.unregister(this.tickFun,this);
  122. }
  123. /**
  124. *
  125. * @returns 扔出来的武器归属
  126. */
  127. public IsHero():boolean{
  128. return this.State==WeaponState.isHeroThrow;
  129. }
  130. /**
  131. * 扔出来的武器归属
  132. * @returns
  133. */
  134. public get IsMonster(): boolean {
  135. return this.State == WeaponState.isMonsterThrow;
  136. }
  137. // start () {}
  138. private tt: number = 0;
  139. private checkSameWolfAtk: boolean = false;
  140. tickFun(dt: number) {
  141. if (this.isStop) { return;}
  142. if (this.IsMonster) {
  143. this.tt += dt;
  144. if (this.tt > 0.2) {
  145. this.tt = 0;
  146. if (!this.checkSameWolfAtk) {//击中同类检测
  147. if (this.moveDir < 0 && this.node.position.x < 0
  148. || this.moveDir > 0 && this.node.position.x > 0
  149. ) {//超过左边半屏幕,可以攻击同种类狼
  150. //
  151. // console.error("改变分组:"+this.State+" "+this.IsMonster);
  152. this.checkSameWolfAtk = true;
  153. this.node.group = "heroWeapon";
  154. this.node.active = false;
  155. this.node.active = true;
  156. }
  157. }
  158. let p_position = Main.Ins.hero.node.position;
  159. let distance = Math.abs(p_position.x - this.node.position.x);// cc.Vec2.distance(p_position, e_position);
  160. if (distance <250) {
  161. if (Main.Ins.TryGuideStep(3) && Main.Ins.GuideSteping!=3) {
  162. Main.Ins.BallonGuide.showTop(true,"跳跃躲避攻击");
  163. Main.Ins.BallonGuide.showBottom(true,"蹲下躲避攻击");
  164. EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,true);
  165. Main.Ins.SetGuideIptPermit(GameInput.up,GameInput.down);
  166. // Main.Ins.pauseGame();
  167. // Main.Ins.guideView.show("跳跃或蹲下躲避攻击");
  168. }
  169. }
  170. // if (distance < 200) {
  171. // this.canPickShow();
  172. // } else
  173. if (this.annima != this.dropAnim) {
  174. if (this.moveDir < 0 && this.node.position.x > 30 ||
  175. this.moveDir > 0 && this.node.position.x < -30) {
  176. this.dangerShow();
  177. } else if (this.state!=WeaponState.heroHold){
  178. this.safeShow();
  179. }
  180. } else {
  181. if (distance < 200) {
  182. this.canPickShow();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * 播放新动画
  190. * @param animiat
  191. */
  192. setAnima(animiat: string):void {
  193. if(!animiat || this.annima == animiat) {
  194. return
  195. }
  196. // cc.log('动画改变 old {}, new {}',this.annima, animiat)
  197. this.annima = animiat
  198. // 播放新动画
  199. this.log('当前播放动画:' + animiat)
  200. this.animCom.play(animiat)
  201. }
  202. /**
  203. * 掉落
  204. * @param wPos
  205. */
  206. public Drop(wPos) {
  207. let me = this;
  208. this.SetWPos(wPos);
  209. this.node.group = "monsterWeapon";
  210. this.node.active = false;
  211. this.node.active = true;
  212. this.canPick = false;
  213. this.isSafe = false;
  214. // console.error(" weapon group:"+this.node.group);
  215. this.rigidBody.gravityScale = 1;
  216. this.rigidBody.linearVelocity = cc.v2(0, 550);
  217. Ticker.registerDelay(() => {
  218. me.State = WeaponState.drop;//晚点设置状态,以防刚出来就被捡走,玩家搞不清
  219. }, 500);
  220. this.setAnima(this.dropAnim);
  221. let p_position = Main.Ins.hero.node.position;
  222. let distance = Math.abs(p_position.x - this.node.position.x);// cc.Vec2.distance(p_position, e_position);
  223. /**这里为什么要加这个逻辑?*/
  224. if (distance < 200) {
  225. this.canPickShow();
  226. }
  227. }
  228. // 碰撞回调
  229. onCollisionEnter(other: cc.BoxCollider, self: cc.BoxCollider) {
  230. this.logErr("武器 onCollisionEnter: " + other.node.group+" self group"+self.node.group,true);
  231. if (other.node.group == 'hero' ) {//碰到英雄
  232. if (this.isSafe) { return; }
  233. let heroHit = other.node.getComponent(HeroHit);
  234. if (!heroHit) {return;}
  235. let hero = heroHit.node.parent.getComponent(Hero);
  236. if ( other.tag == 1 ) {
  237. if (hero.AtkWeapon(this)) //武器被击飞了
  238. {
  239. return;
  240. }
  241. if (this.state == WeaponState.drop) //判断并捡起武器
  242. {
  243. if (hero.OnHoldWeapon(this)) {
  244. this.setAnima(this.handAnim);
  245. this.rigidBody.linearVelocity = cc.v2(0,0);
  246. this.rigidBody.gravityScale = 0;
  247. this.canAttachTag.active = false;
  248. this.warnTag.active = false;
  249. }
  250. return;
  251. }
  252. // if (this.canPick && !Main.Ins.IsGuide) {
  253. // this.Drop(this.WPos);
  254. // }
  255. }
  256. // // if (other.size.width ==0 || other.size.height ==0 ) { return;}
  257. // if ( other.tag == 1 && this.canPick) //判断捡起武器
  258. // {
  259. // let hero = heroHit.node.parent.getComponent(Hero);
  260. // if (!hero) {return;}
  261. // if (hero.OnHoldWeapon(this)) {
  262. // this.setAnima(this.handAnim);
  263. // this.rigidBody.linearVelocity = cc.v2(0,0);
  264. // this.rigidBody.gravityScale = 0;
  265. // this.canAttachTag.active = false;
  266. // this.warnTag.active = false;
  267. // }
  268. // }
  269. else if (this.State == WeaponState.isMonsterThrow && other.tag == 2) {
  270. heroHit.onHurt(this.WPos.x < heroHit.WPos.x,this);
  271. // this.node.destroy();//击中了英雄,会转为掉落状态
  272. }
  273. }else if (this.State == WeaponState.isHeroThrow ||this.State == WeaponState.isMonsterThrow && other.node.group == 'enemy') {//碰到怪物
  274. let enemy = other.node.parent.getComponent(MonsterBase);
  275. if (enemy.id!=this.parentId) {//攻击生效
  276. if (enemy.hurtWithWeapon(this)) {
  277. this.HitWolf(!enemy.isDead());
  278. }
  279. }
  280. }
  281. }
  282. public Body():cc.Node{
  283. return this.animCom.node;
  284. }
  285. SetWPos(wPos){
  286. let localPos = this.node.parent.convertToNodeSpaceAR(wPos);
  287. this.node.position =cc.v3(localPos.x,localPos.y,0);
  288. }
  289. public HeroHold(){
  290. this.State = WeaponState.heroHold;
  291. this.isSafe = false;
  292. this.canAttachTag.active = false;
  293. this.warnTag.active = false;
  294. }
  295. public HeroThrow(isLeft,parentId){
  296. this.logErr("HeroThrow");
  297. this.State = WeaponState.isHeroThrow;
  298. this.parentId = parentId;
  299. this.setAnima(Anima.move);
  300. this.moveDir = isLeft?-1:1;
  301. this.rigidBody.linearVelocity = cc.v2( this.moveDir*this.speed,0);
  302. this.canAttachTag.active = false;
  303. this.warnTag.active = false;
  304. }
  305. public MonstThrow(isLeft,parentId){
  306. this.logErr("MonstThrow");
  307. this.State = WeaponState.isMonsterThrow;
  308. this.parentId = parentId;
  309. this.rigidBody.gravityScale = 0;
  310. this.dangerShow();
  311. this.setAnima(Anima.move);
  312. this.moveDir = isLeft ? -1 : 1;
  313. this.rigidBody.linearVelocity = cc.v2( this.moveDir * this.speed, 0);
  314. }
  315. /**击中怪物 */
  316. public HitWolf(isAttatch){
  317. // return;
  318. this.logErr("击中怪物");
  319. // SoundMgr.Instance.play_effect("飞刀击中狼");
  320. // if (isAttatch) {
  321. // this.setAnima(Anima.hitInEnemy);
  322. // this.State = State.hitInEnemy;
  323. // this.rigidBody.linearVelocity = cc.v2(0,0);
  324. // this.node.group = "default";
  325. // this.node.active = false;
  326. // this.node.active = true;
  327. // }else{
  328. this.node.destroy();
  329. // }
  330. }
  331. safeShow(){
  332. this.isSafe = true;
  333. this.canPick = false;
  334. this.canAttachTag.active = true;
  335. this.warnTag.active = false;
  336. }
  337. canPickShow(){
  338. this.canPick = true;
  339. this.isSafe = false;
  340. this.canAttachTag.active = true;
  341. this.warnTag.active = false;
  342. }
  343. dangerShow(){
  344. this.canPick = false;
  345. this.isSafe = false;
  346. this.canAttachTag.active = false;
  347. this.warnTag.active = true;
  348. }
  349. /**被英雄的手持武器击中 */
  350. heroAtkWeapon(isLeft:boolean,parentId){
  351. if (isLeft) {
  352. //向右
  353. this.moveDir = 1;
  354. }else{
  355. this.moveDir = -1;
  356. }
  357. this.canPick = false;
  358. this.canAttachTag.active = false;
  359. this.warnTag.active = false;
  360. this.speed*=2;
  361. this.rigidBody.gravityScale = 0;
  362. this.rigidBody.linearVelocity = cc.v2( this.moveDir*this.speed,0);
  363. this.canAttachTag.active = false;
  364. this.warnTag.active = true;
  365. this.node.group = "heroWeapon";
  366. this.node.active = false;
  367. this.node.active = true;
  368. this.State = WeaponState.isHeroThrow;
  369. this.parentId = parentId;
  370. }
  371. private log(msg){
  372. return;
  373. console.log("武器-->"+msg);
  374. }
  375. private logWarn(msg){
  376. return;
  377. console.warn("武器-->"+msg);
  378. }
  379. private logErr(msg,isShow:boolean = false){
  380. if (!isShow) {
  381. return;
  382. }
  383. return;
  384. console.error("武器-->"+msg);
  385. }
  386. }