FFenceTrigger.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. element.getComponent(cc.PhysicsBoxCollider).enabled = true;
  159. element.zIndex = 0;
  160. let nodes = element.children;
  161. nodes.forEach(tmp => {
  162. let spine = tmp.getComponent(sp.Skeleton);
  163. if (spine) {
  164. spine.setAnimation(0, 'close', false);
  165. }
  166. });
  167. }
  168. this.moveCamera(this.node.getPosition(), 1, () => {
  169. this.dialog(0);
  170. })
  171. }
  172. private dialog(index: number) {
  173. if (index >= this.text.length) {
  174. this.startFight1();
  175. return;
  176. }
  177. let texts = this.text[index].split('|')
  178. let mid = parseInt(texts.shift());
  179. if (mid == -1) {//主角
  180. let my = this.ff.mainSprite.node;
  181. this.showDialog(my, texts, () => {
  182. index++;
  183. this.dialog(index);
  184. });
  185. } else {
  186. let my = this.mMonster[mid].node;
  187. this.showDialog(my, texts, () => {
  188. index++;
  189. this.dialog(index);
  190. });
  191. }
  192. }
  193. private startFight1() {
  194. this.addMark();
  195. // this.moveTo();
  196. if (this.isBoss) {
  197. this.ff.flushHP(this.mMonster[0]);
  198. }
  199. if (this.countDown != -1) {
  200. this.ff.mCountDown.startCountDown(this.countDown);
  201. }
  202. cc.tween(this).sequence(
  203. cc.delayTime(0.6),
  204. cc.callFunc(() => {
  205. this.playSkill(1);
  206. this.playSkill(2);
  207. this.playSkill(3);
  208. }),
  209. cc.delayTime(1.5),
  210. cc.callFunc(() => {
  211. this.ff.pauseSprite(false);
  212. this.ff.mBlockInputEvents.active = false;
  213. for (let i = 0; i < this.mMonster.length; i++) {
  214. const element = this.mMonster[i];
  215. element.isActive = true;
  216. }
  217. }),
  218. ).start()
  219. }
  220. //添加怪物标记
  221. private addMark() {
  222. for (let i = 0; i < this.mMonster.length; i++) {
  223. const element = this.mMonster[i];
  224. let node = element.addActiveIcon();
  225. this.actionMark(node);
  226. }
  227. }
  228. private actionMark(node: cc.Node) {
  229. node.scale = 0;
  230. cc.tween(node).sequence(
  231. cc.delayTime(1),
  232. cc.scaleTo(0.2, 1.2, 1.2),
  233. cc.scaleTo(0.2, 0.9, 0.9),
  234. cc.scaleTo(0.2, 1.1, 1.1),
  235. cc.scaleTo(0.2, 0.9, 0.9),
  236. cc.scaleTo(0.2, 1.1, 1.1),
  237. cc.scaleTo(0.2, 1, 1),
  238. cc.blink(1, 3),
  239. cc.fadeOut(1.5),
  240. // cc.spawn(
  241. // cc.blink(2,5),
  242. // cc.fadeOut(2)
  243. // ),
  244. cc.destroySelf(),
  245. ).start();
  246. }
  247. /**
  248. * 移动伙伴
  249. * @param sprite
  250. */
  251. private moveTo() {
  252. let ffs = this.ff.getGroupBy(GroupType.A);
  253. for (let i = 0; i < ffs.length; i++) {
  254. const element = ffs[i];
  255. if (element) {
  256. // element.setRuning(false);
  257. // element.setDir({x:0,y:0});
  258. element.playAction(SpriteActionType.run, true)
  259. let attrData = element.attrData;
  260. let pos = cc.v2();
  261. if (attrData.post == 3) {
  262. pos.x = this.node.x + this.mInitB.x + CMath.getRandom(-this.mInitB.width, this.mInitB.width);
  263. pos.y = this.node.y + this.mInitB.y + CMath.getRandom(-this.mInitB.height, this.mInitB.height);
  264. } else {
  265. pos.x = this.node.x + this.mInitA.x + CMath.getRandom(-this.mInitA.width, this.mInitA.width);
  266. pos.y = this.node.y + this.mInitA.y + CMath.getRandom(-this.mInitA.height, this.mInitA.height);
  267. }
  268. cc.tween(element.node).sequence(
  269. cc.moveTo(0.5, pos),
  270. cc.callFunc(() => {
  271. element.playAction(SpriteActionType.stand, true);
  272. })
  273. ).start();
  274. }
  275. }
  276. }
  277. public removeCallback(f: FSprite) {
  278. for (let i = 0; i < this.mMonster.length; i++) {
  279. const element = this.mMonster[i];
  280. if (element == f) {
  281. this.mMonster.splice(i, 1);
  282. break;
  283. }
  284. }
  285. if (this.mMonster.length <= 0) {
  286. this.mIndex++
  287. if (this.mIndex < this.groupMonster.length) {
  288. this.mMonster = this.groupMonster[this.mIndex]
  289. this.playAppear()
  290. } else {
  291. for (let i = 0; i < this.mFenceTrigger.length; i++) {
  292. const element = this.mFenceTrigger[i];
  293. // element.active = false;
  294. this.playCancelFence(element);
  295. }
  296. this.ff.regRemoveCallback(null);
  297. //主角减速等待伙伴
  298. // let t1 = this.ff.mainSprite.SPEED_WALK;
  299. // let t2 = this.ff.mainSprite.SPEED_RUN;
  300. // let mainFSprite = this.ff.mainSprite;
  301. // mainFSprite.SPEED_WALK = 100;
  302. // mainFSprite.SPEED_RUN = 100;
  303. this.ff.mainSprite.setPause(true);
  304. cc.tween(this.node).sequence(
  305. cc.delayTime(0.5),
  306. cc.callFunc(() => {
  307. this.ff.mainSprite.setPause(false);
  308. })
  309. ).start()
  310. this.checkOpen();
  311. }
  312. }
  313. }
  314. /**
  315. * 播放怪物出现
  316. */
  317. private playAppear() {
  318. for (let i = 0; i < this.mMonster.length; i++) {
  319. const element = this.mMonster[i];
  320. element.node.active = true
  321. if (this.mAppear) {
  322. let node = cc.instantiate(this.mAppear)
  323. node.parent = element.node
  324. node.group = element.node.group
  325. let spine = node.getComponent(sp.Skeleton)
  326. spine.setCompleteListener(() => {
  327. node.destroy()
  328. element.isActive = true
  329. });
  330. spine.setAnimation(0, 'animation', false);
  331. } else {
  332. element.isActive = true
  333. }
  334. }
  335. }
  336. /**
  337. * 播放消失动画
  338. */
  339. private playCancelFence(node: cc.Node) {
  340. if (this.countDown != -1) {
  341. this.ff.mCountDown.stopCountDown();
  342. }
  343. let nodes = node.children;
  344. let count = 0;
  345. nodes.forEach(element => {
  346. let spine = element.getComponent(sp.Skeleton);
  347. if (spine) {
  348. spine.setCompleteListener(() => {
  349. element.active = false;
  350. count++;
  351. if (count >= nodes.length) {
  352. node.active = false;
  353. }
  354. });
  355. spine.setAnimation(0, 'open', false);
  356. }
  357. });
  358. }
  359. /**
  360. * 释放buff技能
  361. * @param sprite
  362. * @param path
  363. */
  364. private playSkill(id) {
  365. cc.resources.load('prefab/role/skills/skills_' + id, cc.Prefab, (err, prefab: cc.Prefab) => {
  366. if (err) {
  367. cc.error(err);
  368. } else {
  369. let fss = this.ff.getGroupBy(GroupType.A);
  370. for (let i = 0; i < fss.length; i++) {
  371. const element = fss[i];
  372. if (element) {
  373. let node = cc.instantiate(prefab);
  374. node.parent = element.node;
  375. node.zIndex = 9999;
  376. let spine = node.getComponent(sp.Skeleton);
  377. if (id != 3) {
  378. spine.setCompleteListener(() => {
  379. node.destroy();
  380. });
  381. } else {
  382. element.hasDun = true;
  383. cc.tween(node).sequence(
  384. cc.delayTime(10),
  385. cc.callFunc(() => {
  386. element.hasDun = false
  387. }),
  388. cc.destroySelf()
  389. ).start();
  390. }
  391. }
  392. }
  393. }
  394. });
  395. }
  396. private checkOpen() {
  397. //检查其它开关是否打开
  398. if (!this.mFenceTrigger2.length) return
  399. this.ff.pauseSprite(true);
  400. this.moveCamera(this.mFenceTrigger2[0].getPosition(), 1, () => {
  401. cc.tween(this.node).sequence(
  402. cc.callFunc(() => {
  403. for (let i = 0; i < this.mFenceTrigger2.length; i++) {
  404. const element = this.mFenceTrigger2[i];
  405. this.showFence(element, "open");
  406. }
  407. }),
  408. cc.delayTime(1),
  409. cc.callFunc(() => {
  410. for (let i = 0; i < this.mFenceTrigger2.length; i++) {
  411. const element = this.mFenceTrigger2[i];
  412. element.getComponent(cc.PhysicsBoxCollider).enabled = false;
  413. element.zIndex = -9999;
  414. }
  415. this.ff.pauseSprite(false);
  416. if (this.isPlot) {
  417. EventListener.dispatchEvent("Step1");
  418. }
  419. })
  420. ).start();
  421. })
  422. }
  423. private showFence(element, action) {
  424. let nodes = element.children;
  425. for (let i = 0; i < nodes.length; i++) {
  426. const element = nodes[i];
  427. let spine: sp.Skeleton = element.getComponent(sp.Skeleton);
  428. if (spine) {
  429. spine.setAnimation(0, action, false);
  430. }
  431. }
  432. }
  433. }