战斗管理系统基础
This commit is contained in:
@@ -1,24 +1,37 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { BattleState } from "./BattleStateComp";
|
||||
import { BattleStateComp } from "./BattleStateComp";
|
||||
import { DamageResult } from "../damage/DamageComp";
|
||||
import { HeroViewComp } from "../hero/HeroViewComp";
|
||||
import { HeroSkillsComp } from "../skill/heroSkillsComp";
|
||||
import { HCardComp } from "../map/HCardComp";
|
||||
|
||||
@ecs.register('BattleEndSystem')
|
||||
export class BattleEndSystem extends ecs.ComblockSystem {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(BattleState);
|
||||
|
||||
return ecs.allOf(BattleStateComp);
|
||||
}
|
||||
|
||||
update(e: ecs.Entity) {
|
||||
const state = e.get(BattleState);
|
||||
const state = e.get(BattleStateComp);
|
||||
if (state.isEnded) {
|
||||
// 清理所有残留伤害组件
|
||||
ecs.query(ecs.allOf(DamageResult)).forEach(entity => {
|
||||
entity.remove(DamageResult);
|
||||
});
|
||||
// 清理战斗残留组件
|
||||
this.cleanComponents();
|
||||
|
||||
// 重置战斗状态
|
||||
state.isEnded = false;
|
||||
// 重置状态
|
||||
state.reset();
|
||||
console.log(`战斗结束,持续时间: ${Date.now() - state.startTime}ms`);
|
||||
}
|
||||
}
|
||||
|
||||
private cleanComponents() {
|
||||
console.log("cleanComponents");
|
||||
console.log("DamageResult",ecs.query(ecs.allOf(DamageResult)));
|
||||
console.log("HCardComp",ecs.query(ecs.allOf(HCardComp)));
|
||||
console.log("HeroSkillsComp",ecs.query(ecs.allOf(HeroSkillsComp)));
|
||||
console.log("HeroViewComp",ecs.query(ecs.allOf(HeroViewComp)));
|
||||
ecs.query(ecs.allOf(DamageResult)).forEach(entity => entity.remove(DamageResult));
|
||||
ecs.query(ecs.allOf(HCardComp)).forEach(entity => entity.remove(HCardComp));
|
||||
ecs.query(ecs.allOf(HeroSkillsComp)).forEach(entity => entity.remove(HeroSkillsComp));
|
||||
ecs.query(ecs.allOf(HeroViewComp)).forEach(entity => entity.remove(HeroViewComp));
|
||||
}
|
||||
}
|
||||
17
assets/script/game/battle/BattleEntity.ts
Normal file
17
assets/script/game/battle/BattleEntity.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { BattleStateComp } from "./BattleStateComp";
|
||||
import { BattleManagerComp } from "./BattleManagerComp";
|
||||
|
||||
/** 战斗实体 */
|
||||
export class BattleEntity extends ecs.Entity {
|
||||
BattleState!: BattleStateComp;
|
||||
BattleManager!: BattleManagerComp;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected init() {
|
||||
this.addComponents<ecs.Comp>(BattleStateComp, BattleManagerComp);
|
||||
}
|
||||
}
|
||||
9
assets/script/game/battle/BattleEntity.ts.meta
Normal file
9
assets/script/game/battle/BattleEntity.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "12e09b78-0059-421e-bab8-1677760401d1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,76 +1,46 @@
|
||||
import { oops } from "db://oops-framework/core/Oops";
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { BattleState } from "./BattleStateComp";
|
||||
import { smc } from "../common/SingletonModuleComp";
|
||||
import { GameEvent } from "../common/config/GameEvent";
|
||||
|
||||
@ecs.register('BattleManager')
|
||||
export class BattleManager extends ecs.Entity {
|
||||
private static _instance: BattleManager;
|
||||
|
||||
static get instance(): BattleManager {
|
||||
if (!this._instance) {
|
||||
this._instance = new BattleManager();
|
||||
this._instance.add(BattleState);
|
||||
}
|
||||
return this._instance;
|
||||
import { BattleEntity } from "./BattleEntity";
|
||||
import { BattleStateComp } from "./BattleStateComp";
|
||||
import { UIID } from "../common/config/GameUIConfig";
|
||||
import { oops } from "db://oops-framework/core/Oops";
|
||||
|
||||
export class BattleManager {
|
||||
/** 获取战斗实体实例 */
|
||||
static get instance(): ecs.Entity {
|
||||
return ecs.query(ecs.allOf(BattleStateComp))[0];
|
||||
}
|
||||
|
||||
/** 外部调用入口:开始新战斗 */
|
||||
startBattle(missionId: number) {
|
||||
this.get(BattleState).isEnded = false;
|
||||
// 加载关卡配置、生成敌人等
|
||||
smc.mission.load(missionId);
|
||||
/** 启动战斗 */
|
||||
static startBattle(missionId: number) {
|
||||
const entity = ecs.getEntity(BattleEntity);
|
||||
entity.get(BattleStateComp).missionId = missionId;
|
||||
}
|
||||
|
||||
/** 外部调用入口:结束战斗 */
|
||||
endBattle() {
|
||||
this.get(BattleState).isEnded = true;
|
||||
}
|
||||
|
||||
private initEvents() {
|
||||
oops.message.on("HeroDead", this.onHeroDead, this);
|
||||
oops.message.on("AllEnemyDead", this.onAllEnemyDead, this);
|
||||
oops.message.on(GameEvent.MissionEnd, this.endBattle, this);
|
||||
oops.message.on("BattleEndCheck", this.onBattleEndCheck, this);
|
||||
}
|
||||
|
||||
private onHeroDead() {
|
||||
this.endBattle();
|
||||
}
|
||||
|
||||
private onAllEnemyDead() {
|
||||
this.endBattle();
|
||||
}
|
||||
|
||||
private onBattleEndCheck() {
|
||||
// 留空或添加实际需要的检测逻辑
|
||||
}
|
||||
}
|
||||
|
||||
@ecs.register('BattleManagerComp')
|
||||
export class BattleManagerComp extends ecs.Comp {
|
||||
reset() {
|
||||
// 初始化战斗状态
|
||||
}
|
||||
}
|
||||
|
||||
export class BattleManagerSystem extends ecs.ComblockSystem {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(BattleManagerComp);
|
||||
}
|
||||
|
||||
update(e: ecs.Entity) {
|
||||
const state = e.get(BattleState);
|
||||
if (state.isEnded) {
|
||||
// 处理战斗结束逻辑
|
||||
this.onBattleEnd(e);
|
||||
/** 结束战斗 */
|
||||
static endBattle() {
|
||||
const entity = ecs.query(ecs.allOf(BattleStateComp))[0];
|
||||
if (entity) {
|
||||
entity.get(BattleStateComp).isEnded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private onBattleEnd(entity: ecs.Entity) {
|
||||
// 发放奖励、保存进度等
|
||||
entity.destroy();
|
||||
/** 扩展:带UI管理的战斗启动 */
|
||||
static startBattleWithUI(missionId: number) {
|
||||
// 关闭主界面
|
||||
// oops.gui.remove(UIID.MainMenu);
|
||||
// // 显示战斗HUD
|
||||
// oops.gui.open(UIID.BattleHUD);
|
||||
// 启动战斗
|
||||
this.startBattle(missionId);
|
||||
}
|
||||
|
||||
/** 扩展:带UI管理的战斗结束 */
|
||||
static endBattleWithUI(isVictory: boolean) {
|
||||
// 关闭战斗HUD
|
||||
// oops.gui.remove(UIID.BattleHUD);
|
||||
// 显示结算界面
|
||||
// oops.gui.open(isVictory ? UIID.Victory : UIID.Defeat);
|
||||
// 结束战斗
|
||||
this.endBattle();
|
||||
}
|
||||
}
|
||||
13
assets/script/game/battle/BattleManagerComp.ts
Normal file
13
assets/script/game/battle/BattleManagerComp.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
/** 战斗管理组件 */
|
||||
@ecs.register('BattleManager')
|
||||
export class BattleManagerComp extends ecs.Comp {
|
||||
/** 单例实例 */
|
||||
static instance: ecs.Entity;
|
||||
|
||||
reset() {
|
||||
BattleManagerComp.instance = this.ent;
|
||||
}
|
||||
|
||||
}
|
||||
9
assets/script/game/battle/BattleManagerComp.ts.meta
Normal file
9
assets/script/game/battle/BattleManagerComp.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7ab0451e-efe7-4ec3-9d54-d7027a96e005",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
53
assets/script/game/battle/BattlePhaseSystem.ts
Normal file
53
assets/script/game/battle/BattlePhaseSystem.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { BattleStateComp } from "./BattleStateComp";
|
||||
import { BattleManager } from "./BattleManager";
|
||||
|
||||
@ecs.register('BattlePhaseSystem')
|
||||
export class BattlePhaseSystem extends ecs.ComblockSystem {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(BattleStateComp);
|
||||
|
||||
}
|
||||
|
||||
update(e: ecs.Entity) {
|
||||
const state = e.get(BattleStateComp);
|
||||
|
||||
switch(state.phase) {
|
||||
case BattleStateComp.Phase.Preparation:
|
||||
this.handlePreparation(state);
|
||||
break;
|
||||
case BattleStateComp.Phase.Fighting:
|
||||
this.handleFighting(state);
|
||||
break;
|
||||
case BattleStateComp.Phase.Victory:
|
||||
this.handleVictory(state);
|
||||
break;
|
||||
case BattleStateComp.Phase.Defeat:
|
||||
this.handleDefeat(state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private handlePreparation(state: BattleStateComp) {
|
||||
// 准备阶段逻辑:角色选择、装备检查等
|
||||
if (state.startTime > 0) {
|
||||
state.setPhase(BattleStateComp.Phase.Fighting);
|
||||
}
|
||||
}
|
||||
|
||||
private handleFighting(state: BattleStateComp) {
|
||||
// 实时战斗逻辑:由其他系统处理
|
||||
}
|
||||
|
||||
private handleVictory(state: BattleStateComp) {
|
||||
// 胜利结算逻辑
|
||||
BattleManager.endBattleWithUI(true);
|
||||
state.setPhase(BattleStateComp.Phase.Preparation);
|
||||
}
|
||||
|
||||
private handleDefeat(state: BattleStateComp) {
|
||||
// 失败结算逻辑
|
||||
BattleManager.endBattleWithUI(false);
|
||||
state.setPhase(BattleStateComp.Phase.Preparation);
|
||||
}
|
||||
}
|
||||
9
assets/script/game/battle/BattlePhaseSystem.ts.meta
Normal file
9
assets/script/game/battle/BattlePhaseSystem.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "60eca3c6-1b6b-4090-a7f9-ca98a3f3c05d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
20
assets/script/game/battle/BattleStartSystem.ts
Normal file
20
assets/script/game/battle/BattleStartSystem.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { BattleStateComp } from "./BattleStateComp";
|
||||
import { smc } from "../common/SingletonModuleComp";
|
||||
|
||||
@ecs.register('BattleStartSystem')
|
||||
export class BattleStartSystem extends ecs.ComblockSystem {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(BattleStateComp);
|
||||
}
|
||||
|
||||
update(e: ecs.Entity) {
|
||||
const state = e.get(BattleStateComp);
|
||||
if (state.startTime === 0) {
|
||||
// 初始化战斗逻辑
|
||||
state.startTime = Date.now();
|
||||
|
||||
console.log(`战斗开始,关卡ID: ${state.missionId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
assets/script/game/battle/BattleStartSystem.ts.meta
Normal file
9
assets/script/game/battle/BattleStartSystem.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7a7dfd93-aebc-4f85-b9b9-f1e355f23285",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,11 +1,43 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
/** 战斗状态组件 */
|
||||
@ecs.register('BattleState')
|
||||
export class BattleState extends ecs.Comp {
|
||||
isEnded: boolean = false;
|
||||
export class BattleStateComp extends ecs.Comp {
|
||||
/** 战斗阶段枚举 */
|
||||
static Phase = {
|
||||
Preparation: 0, // 准备阶段
|
||||
Fighting: 1, // 战斗中
|
||||
Victory: 2, // 胜利
|
||||
Defeat: 3 // 失败
|
||||
};
|
||||
|
||||
phase: number = BattleStateComp.Phase.Preparation;
|
||||
/** 是否结束 */
|
||||
isEnded: boolean = false;
|
||||
/** 当前关卡ID */
|
||||
missionId: number = 0;
|
||||
/** 战斗开始时间戳 */
|
||||
startTime: number = 0;
|
||||
|
||||
reset() {
|
||||
this.isEnded = false;
|
||||
this.missionId = 0;
|
||||
this.startTime = 0;
|
||||
}
|
||||
|
||||
/** 安全状态转换 */
|
||||
setPhase(newPhase: number) {
|
||||
const validTransitions = {
|
||||
[BattleStateComp.Phase.Preparation]: [BattleStateComp.Phase.Fighting],
|
||||
[BattleStateComp.Phase.Fighting]: [BattleStateComp.Phase.Victory, BattleStateComp.Phase.Defeat],
|
||||
[BattleStateComp.Phase.Victory]: [BattleStateComp.Phase.Preparation],
|
||||
[BattleStateComp.Phase.Defeat]: [BattleStateComp.Phase.Preparation]
|
||||
};
|
||||
|
||||
if (validTransitions[this.phase]?.includes(newPhase)) {
|
||||
this.phase = newPhase;
|
||||
} else {
|
||||
console.error(`Invalid phase transition from ${this.phase} to ${newPhase}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user