| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import { _decorator, Component, Node, instantiate, Prefab, CCString, Vec3, v3 } from 'cc';
- import { DataSystem } from '../core/data/DataSystem';
- import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
- import { Utils } from '../core/utils/Utils';
- import { ConfigData } from '../data/ConfigData';
- import { GM } from '../launch/GM';
- import { BattleData, BattleState } from './BattleData';
- import { Buff } from './Buff';
- import { MissionData } from './MissionData';
- import { Monster } from './Monster';
- import { MonsterUI } from './MonsterUI';
- import { TakeAWalk, WalkState } from './TakeAWalk';
- import { BattleUIData } from './ui/BattleUIData';
- const { ccclass, property } = _decorator;
- @ccclass('MonsterCeater')
- export class MonsterCeater extends Component {
- @property({ type: ResourceLoader, tooltip: "资源加载组件" }) res: ResourceLoader;
- @property({ type: Node, tooltip: "怪物父级" }) monsterLayer: Node;
- @property({ type: [CCString], tooltip: "怪物路径" }) monsterPoints: Array<string> = [];
- @property({ type: Prefab, tooltip: "怪物预制体" }) prefab: Prefab;
- @property({ type: Prefab, tooltip: "ui预制体" }) ui: Prefab;
- /**怪物路径集合 */
- private lines: Array<Array<Node>> = [];
- /**关卡配置 */
- private missionConfig: any;
- /**战斗数据 */
- private data: BattleData;
- /**移动计时器 */
- private moveTimer: number = 0;
- /**当前关卡移动间隔 */
- private interval: number = 0;
- /**当前关卡创建的怪物数量 */
- private createCount = 0;
- /**已创建的怪物内部ID集合 */
- private monstersID = [];
- /**当前已经移动的怪物索引 */
- private curIndex = -1;
- start() {
- this.data = DataSystem.getData(BattleData);
- this.missionConfig = DataSystem.getData(ConfigData)["mission"];
- for (let i = 0; i < this.monsterPoints.length; i++) {//格式化路径
- this.lines.push([]);
- let points = this.monsterPoints[i].split(";");
- for (let j = 0; j < points.length; j++) {
- let point = v3(parseFloat(points[j].split(":")[0]), parseFloat(points[j].split(":")[1]), 0);
- let node = new Node();
- node.setPosition(point);
- this.lines[i].push(node);
- }
- }
- }
- update(dt: number) {
- DataSystem.watch(BattleData, "mission") && this.cleanMonster();
- DataSystem.watch(BattleUIData, "_bannerRunOver") && this.changeMission();
- DataSystem.watch(BattleData, "_deleteMonster") && this.deleteMonster();
- this.monsterMove(dt);
- }
- /**当关卡发生变化时 */
- public async changeMission() {
- if (DataSystem.getData(BattleUIData)._bannerRunOver) {
- // GM.addBattleLog(`关卡:${this.data.mission},开始创建怪物`);
- let isBoss = this.missionConfig[this.data.mission]["type"] == "2";
- let monsterData = this.missionConfig[this.data.mission]["monsterData"].split(",");
- this.interval = parseFloat(monsterData[3]);
- this.createCount = parseInt(monsterData[2]);
- let _id = Date.now();
- this.monstersID = [];
- this.curIndex = -1;
- for (let i = 0; i < this.createCount; i++) {
- this.createMonster({
- _id: _id + i,
- isBoss: isBoss,
- id: parseInt(monsterData[0]),
- speed: parseFloat(monsterData[1]),
- });
- }
- this.data.battleState = BattleState.inBattle;
- // GM.addBattleLog(`关卡:${this.data.mission},${this.createCount}个怪物创建完成`);
- }
- }
- /**生成怪物 */
- private async createMonster(data: any) {
- let monsterNode = instantiate(this.prefab);
- monsterNode.setPosition(v3(-10000, 10000, 0));
- let monster = monsterNode.getComponent(Monster);
- monsterNode.setParent(this.monsterLayer);
- let monsterUINode = instantiate(this.ui);
- monsterUINode.setParent(this.monsterLayer);
- monsterUINode.position = monsterNode.position;
- 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));
- this.data._monsters.push(monster);
- this.monstersID.push(data._id);
- }
- /**怪物开始移动 */
- private monsterMove(dt: number): void {
- if (this.data._monsters.length > 0 && this.createCount > 0) {
- this.moveTimer += dt;
- if (this.moveTimer >= this.interval) {
- this.curIndex++;
- if (this.curIndex < this.monstersID.length) {
- for (let i = 0; i < this.data._monsters.length; i++) {
- if (this.data._monsters[i].data._id == this.monstersID[this.curIndex]) {
- // GM.addBattleLog(`关卡:${this.data.mission},怪物:${this.data._monsters[i].data._id},开始移动`);
- this.data._monsters[i].walk();
- this.moveTimer = 0;
- this.createCount--;
- break;
- }
- }
- }
- }
- }
- }
- /**清除残余的怪物 */
- private cleanMonster(): void {
- for (let i = 0; i < this.monsterLayer.children.length; i++) {
- this.monsterLayer.children[i].destroy();
- }
- this.data._deleteMonster = [];
- this.data._monsters = [];
- this.createCount = 0;
- this.moveTimer = 0;
- this.interval = 0
- // GM.addBattleLog(`关卡:${this.data.mission},开始清除剩余怪物,当前monsters中有${this.data._monsters.length}个怪物`);
- }
- /**删除怪物 */
- private deleteMonster(): void {
- if (this.data._deleteMonster.length > 0) {
- for (let i = 0; i < this.data._deleteMonster.length; i++) {
- for (let j = 0; j < this.data._monsters.length; j++) {
- if (this.data._monsters[j].data && this.data._deleteMonster[i] == this.data._monsters[j].data._id) {
- this.data._monsters.splice(j, 1);
- break;
- }
- }
- }
- this.data._deleteMonster = [];
- if (this.data._monsters.length == 0) {
- DataSystem.getData(MissionData)._missionOver = true;
- }
- }
- }
- }
|