FFenceTrigger.ts 15 KB

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