monsterManger.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import { RoadType } from "../../configData/Enum";
  2. import monsterNode from "./monsterNode";
  3. import path from "../../configData/path";
  4. import ReadCsv from "../../configData/ReadCsv";
  5. import GameConfig from "../../configData/GameConfig";
  6. import gameData from "../../configData/gameData";
  7. import gameUI from "../UIFace/gameUI";
  8. import DataObj from "../../configData/DataObj";
  9. import gameManger from "../UIFace/gameManger";
  10. import path1 from "../../configData/path1";
  11. import path2 from "../../configData/path2";
  12. import path3 from "../../configData/path3";
  13. const { ccclass, property } = cc._decorator;
  14. @ccclass
  15. export default class monsterManger extends cc.Component {
  16. @property(cc.Sprite)
  17. private testspr: cc.Sprite = null;
  18. @property(cc.Sprite)
  19. private chaqi: cc.Sprite = null;
  20. private kulou: cc.Node[] = [];
  21. private monsterArr: monsterNode[] = [];
  22. public myDataObj: DataObj = null;
  23. private road_group_1 = [];
  24. private road_group_2 = [];
  25. private road_group_3 = [];
  26. private road_group_4 = [];
  27. private kulou_pos_1 = [];
  28. private kulou_pos_2 = [];
  29. private kulou_pos_3 = [];
  30. private kulou_pos_4 = [];
  31. private birth_rate_1: number[] = [];
  32. private birth_rate_2: number[] = [];
  33. private birth_rate_3: number[] = [];
  34. private birth_rate_4: number[] = [];
  35. private next_time: number[] = [];
  36. private isKuloushow = [[], [], [], []];
  37. private step: number[] = [0, 0, 0];
  38. private group_dex: number = 0;
  39. private roadcount = 0;
  40. private kuloucount = 0;
  41. /**是否可以判断结算 */
  42. private isCanOver: boolean = false;
  43. public static instance: monsterManger = null;
  44. protected onLoad() {
  45. monsterManger.instance = this;
  46. this.myDataObj = new DataObj();
  47. this.chaqi.node.zIndex = 1007;
  48. this.testspr.node.zIndex = 1007;
  49. this.Loadkulou();
  50. }
  51. /**加载骷髅 */
  52. private Loadkulou() {
  53. var self = this;
  54. cc.resources.load("prefabs/kulou", cc.Prefab, function (err, prefab: cc.Prefab) {
  55. for (let i = 0; i < 4; i++) {
  56. var newNode = cc.instantiate(prefab);
  57. self.node.addChild(newNode, 1008);
  58. newNode.active = false;
  59. self.kulou.push(newNode);
  60. newNode.on(cc.Node.EventType.TOUCH_START, self.tapOrTimeUp, self);
  61. }
  62. });
  63. }
  64. /**初始化数据 */
  65. public resetData() {
  66. this.road_group_1 = [];
  67. this.road_group_2 = [];
  68. this.road_group_3 = [];
  69. this.road_group_4 = [];
  70. this.kulou_pos_1 = [];
  71. this.kulou_pos_2 = [];
  72. this.kulou_pos_3 = [];
  73. this.kulou_pos_4 = [];
  74. this.birth_rate_1 = [];
  75. this.birth_rate_2 = [];
  76. this.birth_rate_3 = [];
  77. this.birth_rate_4 = [];
  78. this.next_time = [];
  79. this.isKuloushow = [[], [], [], []];
  80. this.step = [0, 0, 0, 0];
  81. this.group_dex = -1;
  82. this.kuloucount = 0;
  83. this.unscheduleAllCallbacks();
  84. this.node.stopAllActions();
  85. for (let i = 0; i < this.monsterArr.length; i++) {
  86. const element = this.monsterArr[i];
  87. element.node.destroy();
  88. }
  89. this.monsterArr = [];
  90. for (let i = 0; i < 4; i++) {
  91. this.kulou[i].active = false;
  92. let myprogress = this.kulou[i].getComponent(cc.ProgressBar);
  93. myprogress.progress = 1;
  94. }
  95. }
  96. /**加载怪物出现时机数据 */
  97. public initMonster() {
  98. this.resetData();
  99. let self = this;
  100. gameData.instance().readLevel(gameUI.instance.myDataObj.LevelData.scene_path, function () {
  101. self.readData();
  102. });
  103. }
  104. private readData() {
  105. let data = gameData.instance().timeLevel_Config;
  106. let nameArr = [];
  107. for (let i = 0; i < data.length; i++) {
  108. let rowArr = data[i];
  109. if (i == 2) {
  110. for (let k = 0; k < rowArr.length; k++) {
  111. const element = rowArr[k];
  112. nameArr.push(element);
  113. }
  114. }
  115. if (i >= 3) {
  116. for (let k = 0; k < rowArr.length; k++) {
  117. const element = rowArr[k];
  118. let name = nameArr[k];
  119. if (name == 'group_num') {
  120. }
  121. if (name == 'road_1_group') {
  122. let grouparr: number[] = element.split("*");
  123. this.road_group_1.push(grouparr);
  124. }
  125. if (name == 'road_2_group') {
  126. let grouparr: number[] = element.split("*");
  127. this.road_group_2.push(grouparr);
  128. }
  129. if (name == 'road_3_group') {
  130. let grouparr: number[] = element.split("*");
  131. this.road_group_3.push(grouparr);
  132. }
  133. if (name == 'road_4_group') {
  134. let grouparr: number[] = element.split("*");
  135. this.road_group_4.push(grouparr);
  136. }
  137. if (name == 'road_next_time') {
  138. this.next_time.push(element);
  139. }
  140. if (name == 'road_1_birth_rate') {
  141. this.birth_rate_1.push(Number(element));
  142. }
  143. if (name == 'road_2_birth_rate') {
  144. this.birth_rate_2.push(Number(element));
  145. }
  146. if (name == 'road_3_birth_rate') {
  147. this.birth_rate_3.push(Number(element));
  148. }
  149. if (name == 'road_4_birth_rate') {
  150. this.birth_rate_4.push(Number(element));
  151. }
  152. if (name == 'road_1_position') {
  153. let grouparr = element.split("*");
  154. this.kulou_pos_1.push(grouparr);
  155. if (grouparr != '') {
  156. this.isKuloushow[0].push(grouparr);
  157. }
  158. }
  159. if (name == 'road_2_position') {
  160. let grouparr = element.split("*");
  161. this.kulou_pos_2.push(grouparr);
  162. if (grouparr != '') {
  163. this.isKuloushow[1].push(grouparr);
  164. }
  165. }
  166. if (name == 'road_3_position') {
  167. let grouparr = element.split("*");
  168. this.kulou_pos_3.push(grouparr);
  169. if (grouparr != '') {
  170. this.isKuloushow[2].push(grouparr);
  171. }
  172. }
  173. if (name == 'road_4_position') {
  174. let grouparr = element.split("*");
  175. this.kulou_pos_4.push(grouparr);
  176. if (grouparr != '') {
  177. this.isKuloushow[3].push(grouparr);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. this.initKulou();
  184. gameUI.instance.setTotalGroup(0, this.road_group_1.length);
  185. }
  186. /**设置骷髅坐标方向 */
  187. private initKulou() {
  188. for (let i = 0; i < 4; i++) {
  189. let jiantou = this.kulou[i].getChildByName('monsterIndir');
  190. let pos = cc.v2();
  191. let monpos = cc.v2();
  192. if (this.isKuloushow[i].length > 0) {
  193. pos = cc.v2(Number(this.isKuloushow[i][0][0]), Number(this.isKuloushow[i][0][1]));
  194. let isShow = false;
  195. if (i == 0) {
  196. monpos = cc.v2(path.instance.getPathData()[0]);
  197. isShow = this.kulou_pos_1[0][0] != '';
  198. }
  199. if (i == 1) {
  200. monpos = cc.v2(path1.instance.getpathData()[0]);
  201. isShow = this.kulou_pos_2[0][0] != '';
  202. }
  203. if (i == 2) {
  204. monpos = cc.v2(path2.instance.getpathData()[0]);
  205. isShow = this.kulou_pos_3[0][0] != '';
  206. }
  207. if (i == 3) {
  208. monpos = cc.v2(path3.instance.getpathData()[0]);
  209. isShow = this.kulou_pos_4[0][0] != '';
  210. }
  211. if (isShow) {
  212. this.kulou[i].active = true;
  213. this.kulou[i].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.1), cc.scaleTo(0.4, 1))));
  214. }
  215. this.kulou[i].setPosition(pos);
  216. }
  217. if (monpos.y > 540) {
  218. //上
  219. jiantou.setPosition(0, 70);
  220. jiantou.angle = 0;
  221. }
  222. if (monpos.y < -540) {
  223. //下
  224. jiantou.setPosition(0, -70);
  225. jiantou.angle = 180;
  226. }
  227. if (monpos.x > 960) {
  228. //右
  229. jiantou.setPosition(70, 0);
  230. jiantou.angle = 270;
  231. }
  232. if (monpos.x < -960) {
  233. //左
  234. jiantou.setPosition(-70, 0);
  235. jiantou.angle = 90;
  236. }
  237. }
  238. }
  239. /**怪物开始刷新进场 */
  240. public startMonsterGo() {
  241. this.group_dex++;
  242. gameUI.instance.setTotalGroup(this.group_dex + 1, this.road_group_1.length);
  243. this.roadcount = 0;
  244. this.step = [0, 0, 0, 0];
  245. this.roadOne();
  246. this.roadTwo();
  247. this.roadThree();
  248. this.roadFour();
  249. }
  250. /**道路一 */
  251. private roadOne() {
  252. let leth = this.road_group_1[this.group_dex].length;
  253. if (leth - 1 >= this.step[0] && this.road_group_1[this.group_dex][this.step[0]] != '') {
  254. let montype: number = Number(this.road_group_1[this.group_dex][this.step[0]]);
  255. if (montype != 0) {
  256. this.seleMinster(1, Math.floor(Math.random() * 6), montype);
  257. }
  258. this.scheduleOnce(function () {
  259. this.roadOne();
  260. }, this.birth_rate_1[this.group_dex]);
  261. this.step[0]++;
  262. }
  263. else {
  264. this.roadcount++;
  265. if (this.roadcount == 4) {
  266. this.delayedTime();
  267. }
  268. }
  269. }
  270. private roadTwo() {
  271. let leth = this.road_group_2[this.group_dex].length;
  272. if (leth - 1 >= this.step[1] && this.road_group_2[this.group_dex][this.step[1]] != '') {
  273. let montype: number = Number(this.road_group_2[this.group_dex][this.step[1]]);
  274. if (montype != 0) {
  275. this.seleMinster(2, Math.floor(Math.random() * 6), montype);
  276. }
  277. this.scheduleOnce(function () {
  278. this.roadTwo();
  279. }, this.birth_rate_2[this.group_dex]);
  280. this.step[1]++;
  281. }
  282. else {
  283. this.roadcount++;
  284. if (this.roadcount == 4) {
  285. this.delayedTime();
  286. }
  287. }
  288. }
  289. private roadThree() {
  290. let leth = this.road_group_3[this.group_dex].length;
  291. if (leth - 1 >= this.step[2] && this.road_group_3[this.group_dex][this.step[2]] != '') {
  292. let montype: number = Number(this.road_group_3[this.group_dex][this.step[2]]);
  293. if (montype != 0) {
  294. this.seleMinster(3, Math.floor(Math.random() * 6), montype);
  295. }
  296. this.scheduleOnce(function () {
  297. this.roadThree();
  298. }, this.birth_rate_3[this.group_dex]);
  299. this.step[2]++;
  300. }
  301. else {
  302. this.roadcount++;
  303. if (this.roadcount == 4) {
  304. this.delayedTime();
  305. }
  306. }
  307. }
  308. private roadFour() {
  309. let leth = this.road_group_4[this.group_dex].length;
  310. if (leth - 1 >= this.step[3] && this.road_group_4[this.group_dex][this.step[3]] != '') {
  311. let montype: number = Number(this.road_group_4[this.group_dex][this.step[3]]);
  312. if (montype != 0) {
  313. this.seleMinster(4, Math.floor(Math.random() * 6), montype);
  314. }
  315. this.scheduleOnce(function () {
  316. this.roadFour();
  317. }, this.birth_rate_4[this.group_dex]);
  318. this.step[3]++;
  319. }
  320. else {
  321. this.roadcount++;
  322. if (this.roadcount == 4) {
  323. this.delayedTime();
  324. }
  325. }
  326. }
  327. /**延时 */
  328. private delayedTime() {
  329. if (this.group_dex < this.road_group_1.length - 1) {
  330. this.scheduleOnce(function () {
  331. //展示骷髅
  332. this.kulou[0].active = false;
  333. this.kulou[1].active = false;
  334. this.kulou[2].active = false;
  335. this.kulou[3].active = false;
  336. if (this.kulou_pos_1[this.group_dex + 1][0] != '') {
  337. this.kulou[0].active = true;
  338. this.kulou[0].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.1), cc.scaleTo(0.4, 1))));
  339. let myprogress = this.kulou[0].getComponent(cc.ProgressBar);
  340. myprogress.progress = 1;
  341. }
  342. if (this.kulou_pos_2[this.group_dex + 1][0] != '') {
  343. this.kulou[1].active = true;
  344. this.kulou[1].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.1), cc.scaleTo(0.4, 1))));
  345. let myprogress = this.kulou[1].getComponent(cc.ProgressBar);
  346. myprogress.progress = 1;
  347. }
  348. if (this.kulou_pos_3[this.group_dex + 1][0] != '') {
  349. this.kulou[2].active = true;
  350. this.kulou[2].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.1), cc.scaleTo(0.4, 1))));
  351. let myprogress = this.kulou[2].getComponent(cc.ProgressBar);
  352. myprogress.progress = 1;
  353. }
  354. if (this.kulou_pos_4[this.group_dex + 1][0] != '') {
  355. this.kulou[3].active = true;
  356. this.kulou[3].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.4, 1.1), cc.scaleTo(0.4, 1))));
  357. let myprogress = this.kulou[3].getComponent(cc.ProgressBar);
  358. myprogress.progress = 1;
  359. }
  360. this.kuloucount = 0;
  361. this.showKulou();
  362. }, this.next_time[this.group_dex]);
  363. }
  364. else {
  365. //判断结算
  366. this.isCanOver = true;
  367. }
  368. }
  369. /**出现骷髅 */
  370. public showKulou() {
  371. if (this.kuloucount >= 10000) {
  372. return;
  373. }
  374. this.kuloucount++;
  375. let total: number = Number(gameData.instance().game_Const[4][2]);
  376. for (let i = 0; i < this.kulou.length; i++) {
  377. if (this.kulou[i].active == true) {
  378. let myprogress = this.kulou[i].getComponent(cc.ProgressBar);
  379. myprogress.progress = 1 - this.kuloucount / (total * 10);
  380. }
  381. }
  382. if (this.kuloucount >= total * 10) {
  383. this.TimeUp();
  384. return;
  385. }
  386. this.scheduleOnce(function () {
  387. this.showKulou();
  388. }, 0.1);
  389. }
  390. /**计时完 */
  391. public TimeUp() {
  392. for (let i = 0; i < this.kulou.length; i++) {
  393. this.kulou[i].active = false;
  394. this.kulou[i].stopAllActions();
  395. }
  396. this.kuloucount = 10000;
  397. this.startMonsterGo();
  398. }
  399. /**点击骷髅或倒计时完 */
  400. public tapOrTimeUp() {
  401. for (let i = 0; i < this.kulou.length; i++) {
  402. this.kulou[i].active = false;
  403. this.kulou[i].stopAllActions();
  404. }
  405. this.kuloucount = 10000;
  406. this.startMonsterGo();
  407. }
  408. private sddd = 0;
  409. /**创建怪物 */
  410. private seleMinster(way: number, road: RoadType, monsterId: number): void {
  411. var self = this;
  412. this.myDataObj.writeMonster_Config(monsterId);
  413. cc.resources.load("prefabs/" + this.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) {
  414. let guainode = cc.instantiate(prefab);
  415. let monster = guainode.getComponent(monsterNode);
  416. self.sddd++;
  417. monster.name = "name_" + self.sddd;
  418. self.node.addChild(guainode, 2);
  419. monster.initLevelData(way, road, monsterId);
  420. self.monsterArr.push(monster);
  421. });
  422. }
  423. /**不能插旗 */
  424. public updatest(poss: cc.Vec2) {
  425. this.testspr.node.opacity = 255;
  426. this.testspr.node.setPosition(poss);
  427. this.testspr.node.stopAllActions();
  428. cc.tween(this.testspr.node)
  429. .to(0.3, { scale: 1.2 })
  430. .to(0.3, { scale: 1 })
  431. .to(0.3, { scale: 1.2 })
  432. .to(0.3, { scale: 1, opacity: 0 })
  433. .start();
  434. }
  435. /**插旗 */
  436. public chaqidian(poss: cc.Vec2) {
  437. this.chaqi.node.opacity = 255;
  438. this.chaqi.node.setPosition(poss);
  439. this.chaqi.node.stopAllActions();
  440. cc.tween(this.chaqi.node)
  441. .delay(2)
  442. .to(0.3, { opacity: 0 })
  443. .start();
  444. }
  445. /**删除怪物 */
  446. public dieDelatemonster(monter: monsterNode) {
  447. let index = this.monsterArr.indexOf(monter);
  448. if (index >= 0) {
  449. this.monsterArr.splice(index, 1);
  450. }
  451. }
  452. /**获取所有怪物 */
  453. public GetMonsterArr(): monsterNode[] {
  454. return this.monsterArr;
  455. }
  456. /**判断游戏结束结算 */
  457. public getIsCanOver(): boolean {
  458. return this.isCanOver;
  459. }
  460. /**判断游戏结束结算 */
  461. public isGameOver() {
  462. if (this.isCanOver) {
  463. //结算
  464. this.isCanOver = false;
  465. // this.node.pauseAllActions();
  466. // this.unscheduleAllCallbacks();
  467. gameManger.instance.gameOver(gameUI.instance.getHp());
  468. }
  469. }
  470. }