You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.3 KiB
83 lines
2.3 KiB
import type { Cell } from '../battlefield/Cell';
|
|
import type { IBattleBridge } from '../bridge/IBattleBridge';
|
|
import type { EntityId } from '../ecs/EntityId';
|
|
import type { BattleEventListener } from '../events/BattleEvents';
|
|
import type { BattleEventSubscription } from '../events/BattleEventType';
|
|
import { BattleFactory, type BattleCreateOptions } from './BattleFactory';
|
|
import type { BattleSession } from './BattleSession';
|
|
|
|
/** BattleHost 创建参数 */
|
|
export interface BattleHostOptions extends BattleCreateOptions {
|
|
/** 项目层桥接 */
|
|
bridge?: IBattleBridge;
|
|
}
|
|
|
|
/**
|
|
* 通用战斗宿主 — 推荐入口。
|
|
* 负责:创建会话、驱动 tick、订阅事件、结束战斗。
|
|
*/
|
|
export class BattleHost {
|
|
/** 底层战斗会话 */
|
|
readonly session: BattleSession;
|
|
|
|
private constructor(session: BattleSession) {
|
|
this.session = session;
|
|
}
|
|
|
|
/**
|
|
* @param options 创建参数
|
|
* @returns 已就绪的 BattleHost 实例
|
|
*/
|
|
static open(options: BattleHostOptions): BattleHost {
|
|
const session = BattleFactory.create(options);
|
|
return new BattleHost(session);
|
|
}
|
|
|
|
/** @param dt 帧间隔(秒) */
|
|
tick(dt: number): void {
|
|
this.session.tick(dt);
|
|
}
|
|
|
|
/**
|
|
* @param playerId 玩家 ID
|
|
* @param unitDefId 单位配置 ID
|
|
* @param cell 锚点格子
|
|
* @returns 部署成功返回实体 ID
|
|
*/
|
|
deploy(playerId: number, unitDefId: string, cell: Cell): EntityId | null {
|
|
return this.session.deploy.place(playerId, unitDefId, cell);
|
|
}
|
|
|
|
/** 进入备战阶段 */
|
|
startPrep(): void {
|
|
this.session.startPrep();
|
|
}
|
|
|
|
/** 开始当前波战斗 */
|
|
startCombat(): void {
|
|
this.session.startCombat();
|
|
}
|
|
|
|
/**
|
|
* @param event 事件类型或通配符
|
|
* @param listener 回调
|
|
*/
|
|
on(event: BattleEventSubscription, listener: BattleEventListener): void {
|
|
this.session.events.on(event, listener);
|
|
}
|
|
|
|
/**
|
|
* @param event 事件类型或通配符
|
|
* @param listener 回调
|
|
*/
|
|
off(event: BattleEventSubscription, listener: BattleEventListener): void {
|
|
this.session.events.off(event, listener);
|
|
}
|
|
|
|
/**
|
|
* @param reason 结束原因
|
|
*/
|
|
destroy(reason = 'destroyed'): void {
|
|
this.session.endBattle(false, reason);
|
|
}
|
|
}
|
|
|