FFenceTrigger.ts 14 KB

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