mirror of
https://github.com/motajs/template.git
synced 2026-11-18 10:14:30 +08:00
feat(event): complete phase one event system
This commit is contained in:
1 parent
b9213ddf22
commit
a297311ea6
51 files changed
+3261
-1114
No files matched your search
@@ -1,182 +0,0 @@
|
||||
import { IFacedTileLocator, logger } from '@motajs/common';
|
||||
import {
|
||||
IHeroChangeFloorInfo,
|
||||
IHeroState,
|
||||
IMapLayer,
|
||||
IMapState,
|
||||
IStateBase
|
||||
} from '@user/data-base';
|
||||
import {
|
||||
ChangeFloorPos,
|
||||
ChangeFloorTarget,
|
||||
ChangeFloorType,
|
||||
FaceDirection,
|
||||
IChangeFloorData,
|
||||
IHeroAttr
|
||||
} from '@user/data-common';
|
||||
import {
|
||||
BaseTrigger,
|
||||
ITrigger,
|
||||
ITriggerCollection,
|
||||
ITriggerHandler,
|
||||
TriggerCollection
|
||||
} from '@user/data-system';
|
||||
import { isNil } from 'lodash-es';
|
||||
|
||||
export const enum TriggerType {
|
||||
/** 楼层切换触发器 */
|
||||
ChangeFloor
|
||||
}
|
||||
|
||||
export class ChangeFloorTrigger extends BaseTrigger implements ITrigger {
|
||||
readonly type: number = TriggerType.ChangeFloor;
|
||||
readonly priority: number = 10;
|
||||
|
||||
constructor(
|
||||
state: IStateBase,
|
||||
readonly maps: IMapState,
|
||||
readonly hero: IHeroState<IHeroAttr>
|
||||
) {
|
||||
super(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取楼层切换的目标楼层
|
||||
* @param data 楼层切换数据
|
||||
*/
|
||||
private getFloorTarget(data: IChangeFloorData): string | undefined {
|
||||
if (data.floorType === ChangeFloorType.Specified) {
|
||||
if (isNil(data.targetFloor)) {
|
||||
logger.warn(165);
|
||||
return void 0;
|
||||
}
|
||||
return data.targetFloor;
|
||||
} else {
|
||||
if (isNil(data.relatedFloor)) {
|
||||
logger.warn(166);
|
||||
return void 0;
|
||||
}
|
||||
const curr = this.hero.location.floorId;
|
||||
if (isNil(curr)) {
|
||||
logger.warn(167);
|
||||
return void 0;
|
||||
}
|
||||
const index = this.maps.maps.indexOf(curr);
|
||||
if (index === -1) {
|
||||
logger.warn(168);
|
||||
return void 0;
|
||||
}
|
||||
if (data.relatedFloor === ChangeFloorTarget.Next) {
|
||||
const next = this.maps.maps[index + 1];
|
||||
if (isNil(next)) {
|
||||
logger.warn(169);
|
||||
return void 0;
|
||||
}
|
||||
return next;
|
||||
} else {
|
||||
const next = this.maps.maps[index - 1];
|
||||
if (isNil(next)) {
|
||||
logger.warn(169);
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getPosTarget(
|
||||
data: IChangeFloorData,
|
||||
layer: IMapLayer
|
||||
): IFacedTileLocator | undefined {
|
||||
if (data.posType === ChangeFloorType.Specified) {
|
||||
if (!data.targetPos) {
|
||||
logger.warn(165);
|
||||
return void 0;
|
||||
}
|
||||
return data.targetPos;
|
||||
} else {
|
||||
if (isNil(data.relatedPos)) {
|
||||
logger.warn(166);
|
||||
return void 0;
|
||||
}
|
||||
const { x, y, floorId } = this.hero.location;
|
||||
if (isNil(floorId)) {
|
||||
logger.warn(167);
|
||||
return void 0;
|
||||
}
|
||||
const { width, height } = layer;
|
||||
switch (data.relatedPos) {
|
||||
case ChangeFloorPos.Stand:
|
||||
return {
|
||||
direction: FaceDirection.Unknown,
|
||||
x,
|
||||
y
|
||||
};
|
||||
case ChangeFloorPos.SymmetryX:
|
||||
return {
|
||||
direction: FaceDirection.Unknown,
|
||||
x: width - x - 1,
|
||||
y
|
||||
};
|
||||
case ChangeFloorPos.SymmetryY:
|
||||
return {
|
||||
direction: FaceDirection.Unknown,
|
||||
x,
|
||||
y: height - y - 1
|
||||
};
|
||||
case ChangeFloorPos.CentralSymmetry:
|
||||
return {
|
||||
direction: FaceDirection.Unknown,
|
||||
x: width - x - 1,
|
||||
y: height - y - 1
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发楼层切换
|
||||
* @param handler 触发信息对象
|
||||
*/
|
||||
private async trigger(handler: ITriggerHandler): Promise<void> {
|
||||
if (!handler.layer || !handler.locator || !handler.mapLayer) {
|
||||
logger.warn(164);
|
||||
return;
|
||||
}
|
||||
const { x, y } = handler.locator;
|
||||
const data = handler.mapLayer.getLocationData(x, y);
|
||||
if (!data || !data.block.changeFloor) return;
|
||||
const floor = this.getFloorTarget(data.block.changeFloor);
|
||||
const pos = this.getPosTarget(data.block.changeFloor, handler.mapLayer);
|
||||
if (isNil(floor) || isNil(pos)) return;
|
||||
const hero = handler.state.hero;
|
||||
const isUnknown = pos.direction === FaceDirection.Unknown;
|
||||
const defaults = hero.location.mover.faceDirection;
|
||||
const info: IHeroChangeFloorInfo = {
|
||||
target: floor,
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
face: isUnknown ? defaults : pos.direction
|
||||
};
|
||||
return handler.state.hero.changeFloor(info);
|
||||
}
|
||||
|
||||
collection(): ITriggerCollection {
|
||||
return new TriggerCollection([this]);
|
||||
}
|
||||
|
||||
onEnter(handler: ITriggerHandler): Promise<void> {
|
||||
return this.trigger(handler);
|
||||
}
|
||||
|
||||
onHit(handler: ITriggerHandler): Promise<void> {
|
||||
return this.trigger(handler);
|
||||
}
|
||||
|
||||
onLeave(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onCannotEnter(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,12 @@ import {
|
||||
IEnemyAttr,
|
||||
ISaveSystem,
|
||||
SaveSystem,
|
||||
GameEventStore,
|
||||
IGameEventStore,
|
||||
IItemStore,
|
||||
ItemStore,
|
||||
IMapStore
|
||||
IMapStore,
|
||||
MapStore
|
||||
} from '@user/data-common';
|
||||
import {
|
||||
EnemyManager,
|
||||
@@ -38,12 +41,10 @@ import {
|
||||
import {
|
||||
DamageSystem,
|
||||
EnemyContext,
|
||||
GameEventSystem,
|
||||
IEnemyContext,
|
||||
ITriggerCollector,
|
||||
ITriggerRegistry,
|
||||
MapDamage,
|
||||
TriggerCollector,
|
||||
TriggerRegistry
|
||||
IGameEventSystem,
|
||||
MapDamage
|
||||
} from '@user/data-system';
|
||||
import {
|
||||
CommonAuraConverter,
|
||||
@@ -76,7 +77,6 @@ import { ILoadProgressTotal, LoadProgressTotal } from '@motajs/loader';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { logger } from '@motajs/common';
|
||||
import { DefaultHeroMoveTopImpl } from './hero';
|
||||
import { MapStore } from '../../data-common/src/store/mapStore';
|
||||
|
||||
export class CoreState implements ICoreState {
|
||||
// Layer 0 公共层,最底层的接口,不会依赖任何其他内容,一般是工具性接口及不需要存档的数据
|
||||
@@ -86,6 +86,7 @@ export class CoreState implements ICoreState {
|
||||
readonly tileStore: ITileStore<LegacyTileData>;
|
||||
readonly itemStore: IItemStore<IHeroAttr, LegacyItemData>;
|
||||
readonly mapStore: IMapStore;
|
||||
readonly eventStore: IGameEventStore;
|
||||
|
||||
// Layer 1 数据层,所有可存档内容都在这,一般用于数据存储
|
||||
readonly maps: IMapState;
|
||||
@@ -95,8 +96,7 @@ export class CoreState implements ICoreState {
|
||||
|
||||
// Layer 2 执行层,游戏逻辑对象都在这,包括一些需要操作数据层的逻辑系统等
|
||||
readonly enemyContext: IEnemyContext<IEnemyAttr, IHeroAttr>;
|
||||
readonly triggerRegistry: ITriggerRegistry;
|
||||
readonly triggerCollector: ITriggerCollector;
|
||||
readonly eventSystem: IGameEventSystem;
|
||||
|
||||
// Layer 3 用户层,也就是最顶层的内容,一般仅用于初始化以及仅供渲染端调用的顶层模块
|
||||
readonly loadProgress: ILoadProgressTotal;
|
||||
@@ -147,6 +147,10 @@ export class CoreState implements ICoreState {
|
||||
// 地图
|
||||
const mapStore = new MapStore();
|
||||
this.mapStore = mapStore;
|
||||
// 游戏事件
|
||||
const eventStore = new GameEventStore();
|
||||
this.eventStore = eventStore;
|
||||
// TODO: 后续在此初始化路径注册外部序列化事件定义与地图事件 id 绑定。
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -199,12 +203,9 @@ export class CoreState implements ICoreState {
|
||||
enemyContext.bindHero(heroAttribute);
|
||||
this.enemyContext = enemyContext;
|
||||
|
||||
// 触发器注册与收集器
|
||||
const triggerRegistry = new TriggerRegistry(this);
|
||||
const triggerCollector = new TriggerCollector();
|
||||
triggerCollector.attachRegistry(triggerRegistry);
|
||||
this.triggerRegistry = triggerRegistry;
|
||||
this.triggerCollector = triggerCollector;
|
||||
// 游戏事件系统
|
||||
const eventSystem = new GameEventSystem(this);
|
||||
this.eventSystem = eventSystem;
|
||||
|
||||
//#endregion
|
||||
|
||||
|
||||
Reference in new issue
Block a user