FFenceTrigger.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import FqLogin from "../../../login/FqLogin";
  2. import CMath from "../../../util/CMath";
  3. import EventListener from "../../../util/EventListener";
  4. import FF from "../FF";
  5. import { GroupType } from "../object/FObject";
  6. import FSprite, { SpriteActionType } from "../object/FSprite";
  7. import FDialogMonster from "./FDialogMonster";
  8. import WOneByone from "./map1/WOneByone";
  9. /**
  10. * 激活怪物
  11. * 栅栏事件
  12. */
  13. const { ccclass, property } = cc._decorator;
  14. @ccclass
  15. export default class FFenceTrigger extends cc.Component {
  16. /**
  17. * 伙伴初始化区域
  18. */
  19. @property({
  20. type: cc.Node,
  21. displayName: '远程初始位置'
  22. })
  23. mInitA: cc.Node = null;
  24. /**
  25. * 伙伴初始化区域
  26. */
  27. @property({
  28. type: cc.Node,
  29. displayName: '近战初始位置'
  30. })
  31. mInitB: cc.Node = null;
  32. /**
  33. * 伙伴初始化区域
  34. */
  35. @property({
  36. type: cc.Node,
  37. displayName: '摄像机移动的目标位置'
  38. })
  39. mCameraPos: cc.Node = null;
  40. /**
  41. * 怪物出现动画
  42. */
  43. @property({
  44. type: cc.Prefab,
  45. displayName: '怪物出现动画'
  46. })
  47. mAppear: cc.Prefab = null;
  48. /**
  49. * 控制的栅栏
  50. */
  51. @property([cc.Node])
  52. mFenceTrigger: Array<cc.Node> = [];
  53. /**
  54. * 需要消灭的怪物1
  55. */
  56. @property([FSprite])
  57. mMonster1: Array<FSprite> = [];
  58. /**
  59. * 需要消灭的怪物2
  60. */
  61. @property([FSprite])
  62. mMonster2: Array<FSprite> = [];
  63. /**
  64. * 需要消灭的怪物3
  65. */
  66. @property([FSprite])
  67. mMonster3: Array<FSprite> = [];
  68. /**
  69. * 需要消灭的怪物4
  70. */
  71. @property([FSprite])
  72. mMonster4: Array<FSprite> = [];
  73. /**
  74. * 需要消灭的怪物5
  75. */
  76. @property([FSprite])
  77. mMonster5: Array<FSprite> = [];
  78. /**
  79. * 控制的栅栏
  80. */
  81. @property({
  82. type: cc.Node,
  83. displayName: '结束后开启的栅栏'
  84. })
  85. mFenceTrigger2: Array<cc.Node> = [];
  86. @property(cc.Prefab)
  87. mMapDialog: cc.Prefab = null;
  88. @property([cc.String])
  89. text: Array<string> = [];
  90. @property({
  91. displayName: '是否开始就显示boss'
  92. })
  93. isBoss = false;
  94. @property({
  95. displayName: '是否出发剧情'
  96. })
  97. isPlot = false;
  98. @property({
  99. displayName: '倒计时时间'
  100. })
  101. countDown = -1;
  102. public ff: FF;
  103. private isOver = false;
  104. private mIndex = 0
  105. /**
  106. * 怪物分组
  107. */
  108. private groupMonster: Array<Array<FSprite>> = []
  109. private mMonster: Array<FSprite> = null
  110. onLoad() {
  111. this.groupMonster.push(this.mMonster1)
  112. if (this.mMonster2.length > 0) {
  113. this.groupMonster.push(this.mMonster2)
  114. }
  115. if (this.mMonster3.length > 0) {
  116. this.groupMonster.push(this.mMonster3)
  117. }
  118. if (this.mMonster4.length > 0) {
  119. this.groupMonster.push(this.mMonster4)
  120. }
  121. if (this.mMonster5.length > 0) {
  122. this.groupMonster.push(this.mMonster5)
  123. }
  124. }
  125. start() {
  126. for (let i = 0; i < this.groupMonster.length; i++) {
  127. const element: Array<FSprite> = this.groupMonster[i];
  128. for (let j = 0; j < element.length; j++) {
  129. const monster = element[j];
  130. monster.isActive = false
  131. if (i > 0) {
  132. monster.node.active = false
  133. }
  134. }
  135. }
  136. this.mMonster = this.groupMonster[0]
  137. }
  138. onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
  139. if (this.isOver) {
  140. return;
  141. }
  142. if (otherCollider.node.group == 'A') {//主角踩到机关
  143. let obj = otherCollider.node.getComponent(FSprite);
  144. this.ff = obj.ff;
  145. if (obj == this.ff.mainSprite) {
  146. this.node.removeComponent(cc.PhysicsBoxCollider);
  147. this.node.removeComponent(cc.PhysicsBoxCollider);
  148. this.ff = obj.ff;
  149. this.ff.regRemoveCallback((f: FSprite) => {
  150. this.removeCallback(f);
  151. });
  152. this.isOver = true;
  153. this.startFight();
  154. FqLogin.commitEvent(this.node.name, '', '')
  155. }
  156. }
  157. }
  158. /**
  159. * 开始战斗
  160. */
  161. private startFight() {
  162. this.ff.pauseSprite(true);
  163. this.moveCamera(this.node, () => {
  164. this.dialog(0);
  165. })
  166. }
  167. private dialog(index: number) {
  168. if (index >= this.text.length) {
  169. this.startFight1();
  170. return;
  171. }
  172. let texts = this.text[index].split('|')
  173. let mid = parseInt(texts.shift());
  174. if (mid == -1) {//主角
  175. let my = this.ff.mainSprite.node;
  176. this.showDialog(my, texts, () => {
  177. index++;
  178. this.dialog(index);
  179. });
  180. } else {
  181. let my = this.mMonster[mid].node;
  182. this.showDialog(my, texts, () => {
  183. index++;
  184. this.dialog(index);
  185. });
  186. }
  187. }
  188. private showDialog(my: cc.Node, dialogs, fCallback: () => void) {
  189. this.ff.mBlockInputEvents.active = true;
  190. let node = cc.instantiate(this.mMapDialog);
  191. node.group = 'map'
  192. node.zIndex = 9999;
  193. node.x = my.x;
  194. node.y = my.y + my.height;
  195. node.parent = this.ff.mMap.mSprites;
  196. let obo = node.getComponent(WOneByone);
  197. obo.dialogs = dialogs;
  198. obo.setCallback(() => {
  199. node.destroy();
  200. this.ff.setBlockInputCallback(null);
  201. fCallback();
  202. });
  203. this.ff.setBlockInputCallback(() => {
  204. obo.jump();
  205. });
  206. obo._start();
  207. }
  208. private startFight1() {
  209. this.addMark();
  210. // this.moveTo();
  211. if (this.isBoss) {
  212. this.ff.flushHP(this.mMonster[0]);
  213. }
  214. if (this.countDown != -1) {
  215. this.ff.mCountDown.startCountDown(this.countDown);
  216. }
  217. cc.tween(this).sequence(
  218. cc.delayTime(0.6),
  219. cc.callFunc(() => {
  220. this.playSkill(1);
  221. this.playSkill(2);
  222. this.playSkill(3);
  223. }),
  224. cc.delayTime(1.5),
  225. cc.callFunc(() => {
  226. this.ff.pauseSprite(false);
  227. this.ff.mBlockInputEvents.active = false;
  228. for (let i = 0; i < this.mFenceTrigger.length; i++) {
  229. const element = this.mFenceTrigger[i];
  230. element.active = true;
  231. let nodes = element.children;
  232. nodes.forEach(tmp => {
  233. let spine = tmp.getComponent(sp.Skeleton);
  234. if (spine) {
  235. spine.setAnimation(0, 'close', false);
  236. }
  237. });
  238. }
  239. for (let i = 0; i < this.mMonster.length; i++) {
  240. const element = this.mMonster[i];
  241. element.isActive = true;
  242. }
  243. }),
  244. ).start()
  245. }
  246. //添加怪物标记
  247. private addMark() {
  248. for (let i = 0; i < this.mMonster.length; i++) {
  249. const element = this.mMonster[i];
  250. let node = element.addActiveIcon();
  251. this.actionMark(node);
  252. }
  253. }
  254. private actionMark(node: cc.Node) {
  255. node.scale = 0;
  256. cc.tween(node).sequence(
  257. cc.delayTime(1),
  258. cc.scaleTo(0.2, 1.2, 1.2),
  259. cc.scaleTo(0.2, 0.9, 0.9),
  260. cc.scaleTo(0.2, 1.1, 1.1),
  261. cc.scaleTo(0.2, 0.9, 0.9),
  262. cc.scaleTo(0.2, 1.1, 1.1),
  263. cc.scaleTo(0.2, 1, 1),
  264. cc.blink(1, 3),
  265. cc.fadeOut(1.5),
  266. // cc.spawn(
  267. // cc.blink(2,5),
  268. // cc.fadeOut(2)
  269. // ),
  270. cc.destroySelf(),
  271. ).start();
  272. }
  273. private moveCamera(node: cc.Node, finishCallback: () => void) {
  274. if (!this.mCameraPos) {
  275. finishCallback();
  276. return;
  277. }
  278. let map = this.ff.mMap;
  279. let camera = map.mCamera;
  280. let pos = cc.v2();
  281. let winsize = cc.winSize;
  282. pos.x = node.x + this.mCameraPos.x - winsize.width / 2;
  283. pos.y = node.y + this.mCameraPos.y - winsize.height / 2;
  284. cc.tween(camera.node).sequence(
  285. cc.moveTo(1, pos),
  286. cc.callFunc(() => {
  287. finishCallback();
  288. })
  289. ).start()
  290. }
  291. /**
  292. * 移动伙伴
  293. * @param sprite
  294. */
  295. private moveTo() {
  296. let ffs = this.ff.getGroupBy(GroupType.A);
  297. for (let i = 0; i < ffs.length; i++) {
  298. const element = ffs[i];
  299. if (element) {
  300. // element.setRuning(false);
  301. // element.setDir({x:0,y:0});
  302. element.playAction(SpriteActionType.run, true)
  303. let attrData = element.attrData;
  304. let pos = cc.v2();
  305. if (attrData.post == 3) {
  306. pos.x = this.node.x + this.mInitB.x + CMath.getRandom(-this.mInitB.width, this.mInitB.width);
  307. pos.y = this.node.y + this.mInitB.y + CMath.getRandom(-this.mInitB.height, this.mInitB.height);
  308. } else {
  309. pos.x = this.node.x + this.mInitA.x + CMath.getRandom(-this.mInitA.width, this.mInitA.width);
  310. pos.y = this.node.y + this.mInitA.y + CMath.getRandom(-this.mInitA.height, this.mInitA.height);
  311. }
  312. cc.tween(element.node).sequence(
  313. cc.moveTo(0.5, pos),
  314. cc.callFunc(() => {
  315. element.playAction(SpriteActionType.stand, true);
  316. })
  317. ).start();
  318. }
  319. }
  320. }
  321. public removeCallback(f: FSprite) {
  322. for (let i = 0; i < this.mMonster.length; i++) {
  323. const element = this.mMonster[i];
  324. if (element == f) {
  325. this.mMonster.splice(i, 1);
  326. break;
  327. }
  328. }
  329. if (this.mMonster.length <= 0) {
  330. this.mIndex++
  331. if (this.mIndex < this.groupMonster.length) {
  332. this.mMonster = this.groupMonster[this.mIndex]
  333. this.playAppear()
  334. } else {
  335. for (let i = 0; i < this.mFenceTrigger.length; i++) {
  336. const element = this.mFenceTrigger[i];
  337. // element.active = false;
  338. this.playCancelFence(element);
  339. }
  340. this.ff.regRemoveCallback(null);
  341. //主角减速等待伙伴
  342. // let t1 = this.ff.mainSprite.SPEED_WALK;
  343. // let t2 = this.ff.mainSprite.SPEED_RUN;
  344. // let mainFSprite = this.ff.mainSprite;
  345. // mainFSprite.SPEED_WALK = 100;
  346. // mainFSprite.SPEED_RUN = 100;
  347. this.ff.mainSprite.setPause(true);
  348. cc.tween(this.node).sequence(
  349. cc.delayTime(0.5),
  350. cc.callFunc(() => {
  351. this.ff.mainSprite.setPause(false);
  352. })
  353. ).start()
  354. this.checkOpen();
  355. }
  356. }
  357. }
  358. /**
  359. * 播放怪物出现
  360. */
  361. private playAppear() {
  362. for (let i = 0; i < this.mMonster.length; i++) {
  363. const element = this.mMonster[i];
  364. element.node.active = true
  365. if (this.mAppear) {
  366. let node = cc.instantiate(this.mAppear)
  367. node.parent = element.node
  368. node.group = element.node.group
  369. let spine = node.getComponent(sp.Skeleton)
  370. spine.setCompleteListener(() => {
  371. node.destroy()
  372. element.isActive = true
  373. });
  374. spine.setAnimation(0, 'animation', false);
  375. } else {
  376. element.isActive = true
  377. }
  378. }
  379. }
  380. /**
  381. * 播放消失动画
  382. */
  383. private playCancelFence(node: cc.Node) {
  384. if (this.countDown != -1) {
  385. this.ff.mCountDown.stopCountDown();
  386. }
  387. let nodes = node.children;
  388. let count = 0;
  389. nodes.forEach(element => {
  390. let spine = element.getComponent(sp.Skeleton);
  391. if (spine) {
  392. spine.setCompleteListener(() => {
  393. element.active = false;
  394. count++;
  395. if (count >= nodes.length) {
  396. node.active = false;
  397. }
  398. });
  399. spine.setAnimation(0, 'open', false);
  400. }
  401. });
  402. }
  403. /**
  404. * 释放buff技能
  405. * @param sprite
  406. * @param path
  407. */
  408. private playSkill(id) {
  409. cc.resources.load('prefab/role/skills/skills_' + id, cc.Prefab, (err, prefab: cc.Prefab) => {
  410. if (err) {
  411. cc.error(err);
  412. } else {
  413. let fss = this.ff.getGroupBy(GroupType.A);
  414. for (let i = 0; i < fss.length; i++) {
  415. const element = fss[i];
  416. if (element) {
  417. let node = cc.instantiate(prefab);
  418. node.parent = element.node;
  419. node.zIndex = 9999;
  420. let spine = node.getComponent(sp.Skeleton);
  421. if (id != 3) {
  422. spine.setCompleteListener(() => {
  423. node.destroy();
  424. });
  425. } else {
  426. element.hasDun = true;
  427. cc.tween(node).sequence(
  428. cc.delayTime(10),
  429. cc.callFunc(() => {
  430. element.hasDun = false
  431. }),
  432. cc.destroySelf()
  433. ).start();
  434. }
  435. }
  436. }
  437. }
  438. });
  439. }
  440. private checkOpen() {
  441. //检查其它开关是否打开
  442. if (!this.mFenceTrigger2.length) return
  443. this.ff.pauseSprite(true);
  444. this.moveCamera(this.mFenceTrigger2[0], () => {
  445. cc.tween(this.node).sequence(
  446. cc.callFunc(() => {
  447. for (let i = 0; i < this.mFenceTrigger2.length; i++) {
  448. const element = this.mFenceTrigger2[i];
  449. this.showFence(element, "open");
  450. }
  451. }),
  452. cc.delayTime(1),
  453. cc.callFunc(() => {
  454. for (let i = 0; i < this.mFenceTrigger2.length; i++) {
  455. const element = this.mFenceTrigger2[i];
  456. element.getComponent(cc.PhysicsBoxCollider).enabled = false;
  457. }
  458. this.ff.pauseSprite(false);
  459. if (this.isPlot) {
  460. EventListener.dispatchEvent("Step1");
  461. }
  462. })
  463. ).start();
  464. })
  465. }
  466. private showFence(element, action) {
  467. let nodes = element.children;
  468. for (let i = 0; i < nodes.length; i++) {
  469. const element = nodes[i];
  470. let spine: sp.Skeleton = element.getComponent(sp.Skeleton);
  471. if (spine) {
  472. spine.setAnimation(0, action, false);
  473. }
  474. }
  475. }
  476. }