FFenceTrigger.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. @property(cc.Prefab)
  78. mMapDialog: cc.Prefab = null;
  79. @property([cc.String])
  80. text: Array<string> = [];
  81. @property({
  82. displayName: '是否开始就显示boss'
  83. })
  84. isBoss = false;
  85. @property({
  86. displayName: '倒计时时间'
  87. })
  88. countDown = -1;
  89. public ff:FF;
  90. private isOver = false;
  91. private mIndex = 0
  92. /**
  93. * 怪物分组
  94. */
  95. private groupMonster:Array<Array<FSprite>> = []
  96. private mMonster:Array<FSprite> = null
  97. onLoad(){
  98. this.groupMonster.push(this.mMonster1)
  99. if(this.mMonster2.length > 0){
  100. this.groupMonster.push(this.mMonster2)
  101. }
  102. if(this.mMonster3.length > 0){
  103. this.groupMonster.push(this.mMonster3)
  104. }
  105. if(this.mMonster4.length > 0){
  106. this.groupMonster.push(this.mMonster4)
  107. }
  108. if(this.mMonster5.length > 0){
  109. this.groupMonster.push(this.mMonster5)
  110. }
  111. }
  112. start(){
  113. for (let i = 0; i < this.groupMonster.length; i++) {
  114. const element:Array<FSprite> = this.groupMonster[i];
  115. for (let j = 0; j < element.length; j++) {
  116. const monster = element[j];
  117. monster.isActive = false
  118. if(i > 0){
  119. monster.node.active = false
  120. }
  121. }
  122. }
  123. this.mMonster = this.groupMonster[0]
  124. }
  125. onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider){
  126. if(this.isOver){
  127. return;
  128. }
  129. if(otherCollider.node.group == 'A'){//主角踩到机关
  130. let obj = otherCollider.node.getComponent(FSprite);
  131. this.ff = obj.ff;
  132. if(obj == this.ff.mainSprite){
  133. this.node.removeComponent(cc.PhysicsBoxCollider);
  134. this.node.removeComponent(cc.PhysicsBoxCollider);
  135. this.ff = obj.ff;
  136. this.ff.regRemoveCallback((f:FSprite)=>{
  137. this.removeCallback(f);
  138. });
  139. this.isOver = true;
  140. this.startFight();
  141. FqLogin.commitEvent(this.node.name,'','')
  142. }
  143. }
  144. }
  145. /**
  146. * 开始战斗
  147. */
  148. private startFight(){
  149. this.ff.pauseSprite(true);
  150. this.moveCamera(()=>{
  151. this.dialog(0);
  152. })
  153. }
  154. private dialog(index:number){
  155. if(index >= this.text.length){
  156. this.startFight1();
  157. return;
  158. }
  159. let texts = this.text[index].split('|')
  160. let mid = parseInt(texts.shift());
  161. if(mid == -1){//主角
  162. let my = this.ff.mainSprite.node;
  163. this.showDialog(my,texts,()=>{
  164. index ++;
  165. this.dialog(index);
  166. });
  167. }else{
  168. let my = this.mMonster[mid].node;
  169. this.showDialog(my,texts,()=>{
  170. index ++;
  171. this.dialog(index);
  172. });
  173. }
  174. }
  175. private showDialog(my:cc.Node,dialogs,fCallback:()=>void){
  176. this.ff.mBlockInputEvents.active = true;
  177. let node = cc.instantiate(this.mMapDialog);
  178. node.group = 'map'
  179. node.zIndex = 9999;
  180. node.x = my.x;
  181. node.y = my.y + my.height;
  182. node.parent = this.ff.mMap.mSprites;
  183. let obo = node.getComponent(WOneByone);
  184. obo.dialogs = dialogs;
  185. obo.setCallback(()=>{
  186. node.destroy();
  187. this.ff.setBlockInputCallback(null);
  188. fCallback();
  189. });
  190. this.ff.setBlockInputCallback(()=>{
  191. obo.jump();
  192. });
  193. obo._start();
  194. }
  195. private startFight1(){
  196. this.addMark();
  197. // this.moveTo();
  198. if(this.isBoss){
  199. this.ff.flushHP(this.mMonster[0]);
  200. }
  201. if(this.countDown != -1){
  202. this.ff.mCountDown.startCountDown(this.countDown);
  203. }
  204. cc.tween(this).sequence(
  205. cc.delayTime(0.6),
  206. cc.callFunc(()=>{
  207. this.playSkill(1);
  208. this.playSkill(2);
  209. this.playSkill(3);
  210. }),
  211. cc.delayTime(1.5),
  212. cc.callFunc(()=>{
  213. this.ff.pauseSprite(false);
  214. this.ff.mBlockInputEvents.active = false;
  215. for (let i = 0; i < this.mFenceTrigger.length; i++) {
  216. const element = this.mFenceTrigger[i];
  217. element.active = true;
  218. let nodes = element.children;
  219. nodes.forEach(tmp => {
  220. let spine = tmp.getComponent(sp.Skeleton);
  221. if(spine){
  222. spine.setAnimation(0, 'close', false);
  223. }
  224. });
  225. }
  226. for (let i = 0; i < this.mMonster.length; i++) {
  227. const element = this.mMonster[i];
  228. element.isActive = true;
  229. }
  230. }),
  231. ).start()
  232. }
  233. //添加怪物标记
  234. private addMark(){
  235. for (let i = 0; i < this.mMonster.length; i++) {
  236. const element = this.mMonster[i];
  237. let node = element.addActiveIcon();
  238. this.actionMark(node);
  239. }
  240. }
  241. private actionMark(node:cc.Node){
  242. node.scale = 0;
  243. cc.tween(node).sequence(
  244. cc.delayTime(1),
  245. cc.scaleTo(0.2,1.2,1.2),
  246. cc.scaleTo(0.2,0.9,0.9),
  247. cc.scaleTo(0.2,1.1,1.1),
  248. cc.scaleTo(0.2,0.9,0.9),
  249. cc.scaleTo(0.2,1.1,1.1),
  250. cc.scaleTo(0.2,1,1),
  251. cc.blink(1,3),
  252. cc.fadeOut(1.5),
  253. // cc.spawn(
  254. // cc.blink(2,5),
  255. // cc.fadeOut(2)
  256. // ),
  257. cc.destroySelf(),
  258. ).start();
  259. }
  260. private moveCamera(finishCallback:()=>void){
  261. if(!this.mCameraPos){
  262. finishCallback();
  263. return;
  264. }
  265. let map = this.ff.mMap;
  266. let camera = map.mCamera;
  267. let pos = cc.v2();
  268. let winsize = cc.winSize;
  269. pos.x = this.node.x + this.mCameraPos.x-winsize.width/2;
  270. pos.y = this.node.y + this.mCameraPos.y-winsize.height/2;
  271. cc.tween(camera.node).sequence(
  272. cc.moveTo(1,pos),
  273. cc.callFunc(()=>{
  274. finishCallback();
  275. })
  276. ).start()
  277. }
  278. /**
  279. * 移动伙伴
  280. * @param sprite
  281. */
  282. private moveTo(){
  283. let ffs = this.ff.getGroupBy(GroupType.A);
  284. for (let i = 0; i < ffs.length; i++) {
  285. const element = ffs[i];
  286. if(element){
  287. // element.setRuning(false);
  288. // element.setDir({x:0,y:0});
  289. element.playAction(SpriteActionType.run,true)
  290. let attrData = element.attrData;
  291. let pos = cc.v2();
  292. if(attrData.post == 3){
  293. pos.x = this.node.x + this.mInitB.x + CMath.getRandom(-this.mInitB.width,this.mInitB.width);
  294. pos.y = this.node.y + this.mInitB.y+ CMath.getRandom(-this.mInitB.height,this.mInitB.height);
  295. }else{
  296. pos.x = this.node.x + this.mInitA.x + CMath.getRandom(-this.mInitA.width,this.mInitA.width);
  297. pos.y = this.node.y + this.mInitA.y+ CMath.getRandom(-this.mInitA.height,this.mInitA.height);
  298. }
  299. cc.tween(element.node).sequence(
  300. cc.moveTo(0.5,pos),
  301. cc.callFunc(()=>{
  302. element.playAction(SpriteActionType.stand,true);
  303. })
  304. ).start();
  305. }
  306. }
  307. }
  308. public removeCallback(f:FSprite){
  309. for (let i = 0; i < this.mMonster.length; i++) {
  310. const element = this.mMonster[i];
  311. if(element == f){
  312. this.mMonster.splice(i,1);
  313. break;
  314. }
  315. }
  316. if(this.mMonster.length <= 0){
  317. this.mIndex ++
  318. if(this.mIndex < this.groupMonster.length){
  319. this.mMonster = this.groupMonster[this.mIndex]
  320. this.playAppear()
  321. }else{
  322. for (let i = 0; i < this.mFenceTrigger.length; i++) {
  323. const element = this.mFenceTrigger[i];
  324. // element.active = false;
  325. this.playCancelFence(element);
  326. }
  327. this.ff.regRemoveCallback(null);
  328. //主角减速等待伙伴
  329. // let t1 = this.ff.mainSprite.SPEED_WALK;
  330. // let t2 = this.ff.mainSprite.SPEED_RUN;
  331. // let mainFSprite = this.ff.mainSprite;
  332. // mainFSprite.SPEED_WALK = 100;
  333. // mainFSprite.SPEED_RUN = 100;
  334. this.ff.mainSprite.setPause(true);
  335. cc.tween(this.node).sequence(
  336. cc.delayTime(0.5),
  337. cc.callFunc(()=>{
  338. this.ff.mainSprite.setPause(false);
  339. })
  340. ).start()
  341. }
  342. }
  343. }
  344. /**
  345. * 播放怪物出现
  346. */
  347. private playAppear(){
  348. for (let i = 0; i < this.mMonster.length; i++) {
  349. const element = this.mMonster[i];
  350. element.node.active = true
  351. if(this.mAppear){
  352. let node = cc.instantiate(this.mAppear)
  353. node.parent = element.node
  354. node.group = element.node.group
  355. let spine = node.getComponent(sp.Skeleton)
  356. spine.setCompleteListener(() => {
  357. node.destroy()
  358. element.isActive = true
  359. });
  360. spine.setAnimation(0, 'animation', false);
  361. }else{
  362. element.isActive = true
  363. }
  364. }
  365. }
  366. /**
  367. * 播放消失动画
  368. */
  369. private playCancelFence(node:cc.Node){
  370. if(this.countDown != -1){
  371. this.ff.mCountDown.stopCountDown();
  372. }
  373. let nodes = node.children;
  374. let count = 0;
  375. nodes.forEach(element => {
  376. let spine = element.getComponent(sp.Skeleton);
  377. if(spine){
  378. spine.setCompleteListener(()=>{
  379. element.active = false;
  380. count ++;
  381. if(count >= nodes.length){
  382. node.active = false;
  383. }
  384. });
  385. spine.setAnimation(0, 'open', false);
  386. }
  387. });
  388. }
  389. /**
  390. * 释放buff技能
  391. * @param sprite
  392. * @param path
  393. */
  394. private playSkill(id){
  395. cc.resources.load('prefab/role/skills/skills_'+id, cc.Prefab, (err, prefab:cc.Prefab) =>{
  396. if(err){
  397. cc.error(err);
  398. }else{
  399. let fss = this.ff.getGroupBy(GroupType.A);
  400. for (let i = 0; i < fss.length; i++) {
  401. const element = fss[i];
  402. if(element){
  403. let node = cc.instantiate(prefab);
  404. node.parent = element.node;
  405. node.zIndex = 9999;
  406. let spine = node.getComponent(sp.Skeleton);
  407. if(id != 3){
  408. spine.setCompleteListener(()=>{
  409. node.destroy();
  410. });
  411. }else{
  412. element.hasDun = true;
  413. cc.tween(node).sequence(
  414. cc.delayTime(10),
  415. cc.callFunc(()=>{
  416. element.hasDun = false
  417. }),
  418. cc.destroySelf()
  419. ).start();
  420. }
  421. }
  422. }
  423. }
  424. } );
  425. }
  426. }