MonsterCeater.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { _decorator, Component, Node, instantiate, Prefab, CCString, Vec3, v3 } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  4. import { Utils } from '../core/utils/Utils';
  5. import { ConfigData } from '../data/ConfigData';
  6. import { GM } from '../launch/GM';
  7. import { BattleData, BattleState } from './BattleData';
  8. import { Buff } from './Buff';
  9. import { MissionData } from './MissionData';
  10. import { Monster } from './Monster';
  11. import { MonsterUI } from './MonsterUI';
  12. import { TakeAWalk, WalkState } from './TakeAWalk';
  13. import { BattleUIData } from './ui/BattleUIData';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('MonsterCeater')
  16. export class MonsterCeater extends Component {
  17. @property({ type: ResourceLoader, tooltip: "资源加载组件" }) res: ResourceLoader;
  18. @property({ type: Node, tooltip: "怪物父级" }) monsterLayer: Node;
  19. @property({ type: [CCString], tooltip: "怪物路径" }) monsterPoints: Array<string> = [];
  20. @property({ type: Prefab, tooltip: "怪物预制体" }) prefab: Prefab;
  21. @property({ type: Prefab, tooltip: "ui预制体" }) ui: Prefab;
  22. /**怪物路径集合 */
  23. private lines: Array<Array<Node>> = [];
  24. /**关卡配置 */
  25. private missionConfig: any;
  26. /**战斗数据 */
  27. private data: BattleData;
  28. /**移动计时器 */
  29. private moveTimer: number = 0;
  30. /**当前关卡移动间隔 */
  31. private interval: number = 0;
  32. /**当前关卡创建的怪物数量 */
  33. private createCount = 0;
  34. /**已创建的怪物内部ID集合 */
  35. private monstersID = [];
  36. /**当前已经移动的怪物索引 */
  37. private curIndex = -1;
  38. start() {
  39. this.data = DataSystem.getData(BattleData);
  40. this.missionConfig = DataSystem.getData(ConfigData)["mission"];
  41. for (let i = 0; i < this.monsterPoints.length; i++) {//格式化路径
  42. this.lines.push([]);
  43. let points = this.monsterPoints[i].split(";");
  44. for (let j = 0; j < points.length; j++) {
  45. let point = v3(parseFloat(points[j].split(":")[0]), parseFloat(points[j].split(":")[1]), 0);
  46. let node = new Node();
  47. node.setPosition(point);
  48. this.lines[i].push(node);
  49. }
  50. }
  51. }
  52. update(dt: number) {
  53. DataSystem.watch(BattleData, "mission") && this.cleanMonster();
  54. DataSystem.watch(BattleUIData, "_bannerRunOver") && this.changeMission();
  55. DataSystem.watch(BattleData, "_deleteMonster") && this.deleteMonster();
  56. this.monsterMove(dt);
  57. }
  58. /**当关卡发生变化时 */
  59. public async changeMission() {
  60. if (DataSystem.getData(BattleUIData)._bannerRunOver) {
  61. // GM.addBattleLog(`关卡:${this.data.mission},开始创建怪物`);
  62. let isBoss = this.missionConfig[this.data.mission]["type"] == "2";
  63. let monsterData = this.missionConfig[this.data.mission]["monsterData"].split(",");
  64. this.interval = parseFloat(monsterData[3]);
  65. this.createCount = parseInt(monsterData[2]);
  66. let _id = Date.now();
  67. this.monstersID = [];
  68. this.curIndex = -1;
  69. for (let i = 0; i < this.createCount; i++) {
  70. this.createMonster({
  71. _id: _id + i,
  72. isBoss: isBoss,
  73. id: parseInt(monsterData[0]),
  74. speed: parseFloat(monsterData[1]),
  75. });
  76. }
  77. this.data.battleState = BattleState.inBattle;
  78. // GM.addBattleLog(`关卡:${this.data.mission},${this.createCount}个怪物创建完成`);
  79. }
  80. }
  81. /**生成怪物 */
  82. private async createMonster(data: any) {
  83. let monsterNode = instantiate(this.prefab);
  84. monsterNode.setPosition(v3(-10000, 10000, 0));
  85. let monster = monsterNode.getComponent(Monster);
  86. monsterNode.setParent(this.monsterLayer);
  87. let monsterUINode = instantiate(this.ui);
  88. monsterUINode.setParent(this.monsterLayer);
  89. monsterUINode.position = monsterNode.position;
  90. monster.setData({ _id: data._id, isBoss: data.isBoss, id: data.id, movePoints: this.lines[Utils.random_both(0, 1)], speed: data.speed, hp: DataSystem.getData(ConfigData)["monster"][data.id]["hp"] }, monsterUINode.getComponent(MonsterUI));
  91. this.data._monsters.push(monster);
  92. this.monstersID.push(data._id);
  93. }
  94. /**怪物开始移动 */
  95. private monsterMove(dt: number): void {
  96. if (this.data._monsters.length > 0 && this.createCount > 0) {
  97. this.moveTimer += dt;
  98. if (this.moveTimer >= this.interval) {
  99. this.curIndex++;
  100. if (this.curIndex < this.monstersID.length) {
  101. for (let i = 0; i < this.data._monsters.length; i++) {
  102. if (this.data._monsters[i].data._id == this.monstersID[this.curIndex]) {
  103. // GM.addBattleLog(`关卡:${this.data.mission},怪物:${this.data._monsters[i].data._id},开始移动`);
  104. this.data._monsters[i].walk();
  105. this.moveTimer = 0;
  106. this.createCount--;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. /**清除残余的怪物 */
  115. private cleanMonster(): void {
  116. for (let i = 0; i < this.monsterLayer.children.length; i++) {
  117. this.monsterLayer.children[i].destroy();
  118. }
  119. this.data._deleteMonster = [];
  120. this.data._monsters = [];
  121. this.createCount = 0;
  122. this.moveTimer = 0;
  123. this.interval = 0
  124. // GM.addBattleLog(`关卡:${this.data.mission},开始清除剩余怪物,当前monsters中有${this.data._monsters.length}个怪物`);
  125. }
  126. /**删除怪物 */
  127. private deleteMonster(): void {
  128. if (this.data._deleteMonster.length > 0) {
  129. for (let i = 0; i < this.data._deleteMonster.length; i++) {
  130. for (let j = 0; j < this.data._monsters.length; j++) {
  131. if (this.data._monsters[j].data && this.data._deleteMonster[i] == this.data._monsters[j].data._id) {
  132. this.data._monsters.splice(j, 1);
  133. break;
  134. }
  135. }
  136. }
  137. this.data._deleteMonster = [];
  138. if (this.data._monsters.length == 0) {
  139. DataSystem.getData(MissionData)._missionOver = true;
  140. }
  141. }
  142. }
  143. }