diff --git a/source/Model/Behavior.ts b/source/Model/Behavior.ts index 2258936..47e5179 100644 --- a/source/Model/Behavior.ts +++ b/source/Model/Behavior.ts @@ -6,12 +6,6 @@ import type { Model } from "./Model"; import type { Range } from "./Range"; import type { Label } from "./Label"; -/** - * 行为构造函数类型 - */ -type IBehaviorConstructor> = - new (id: string, parameter: IBehaviorParameterValue) => B; - /** * 参数类型 */ @@ -43,8 +37,6 @@ type IObjectType = keyof IMapObjectParamTypeKeyToType; type IVectorType = keyof IMapVectorParamTypeKeyToType; type IParamValue = AllMapType[K]; - - /** * 特殊对象类型判定 */ @@ -111,25 +103,34 @@ interface IBehaviorParameterOptionItem { iconName?: string; } -/** - * 参数键值类型 - */ -type IBehaviorParameterValueItem

= IParamValue; +interface IBehaviorParameter { + [x: string]: IParamType; +} /** * 参数类型列表 */ -interface IBehaviorParameterOption { - [x: string]: IBehaviorParameterOptionItem; +type IBehaviorParameterOption

= { + [X in keyof P]: IBehaviorParameterOptionItem; } /** * 参数类型列表映射到参数对象 */ -type IBehaviorParameterValue

= { - [x in keyof P]: IBehaviorParameterValueItem +type IBehaviorParameterValue

= { + [X in keyof P]: IParamValue } +/** + * 行为构造函数类型 + */ +type IBehaviorConstructor< + P extends IBehaviorParameter = {}, + E extends Record = {} +> = new (id: string, parameter: IBehaviorParameterValue

) => Behavior; + +type IAnyBehavior = Behavior; +type IAnyBehaviorRecorder = BehaviorRecorder; /** * 行为的基础信息 @@ -158,8 +159,9 @@ class BehaviorInfo = {}> extends Emitter { } class BehaviorRecorder< - B extends Behavior -> extends BehaviorInfo { + P extends IBehaviorParameter = {}, + E extends Record = {} +> extends BehaviorInfo<{}> { /** * 命名序号 @@ -176,23 +178,23 @@ class BehaviorRecorder< /** * 行为类型 */ - public behavior: IBehaviorConstructor; + public behavior: IBehaviorConstructor; /** * 行为实例 */ - public behaviorInstance: B; + public behaviorInstance: Behavior; /** * 对象参数列表 */ - public parameterOption: B["parameterOption"]; + public parameterOption: IBehaviorParameterOption

; /** * 获取参数列表的默认值 */ - public getDefaultValue(): IBehaviorParameterValue { - let defaultObj = {} as IBehaviorParameterValue; + public getDefaultValue(): IBehaviorParameterValue

{ + let defaultObj = {} as IBehaviorParameterValue

; for (let key in this.parameterOption) { let defaultVal = this.parameterOption[key].defaultValue; @@ -224,11 +226,11 @@ class BehaviorRecorder< /** * 创建一个新的行为实例 */ - public new(): B { + public new(): Behavior { return new this.behavior(this.getNextId(), this.getDefaultValue()); } - public constructor(behavior: IBehaviorConstructor) { + public constructor(behavior: IBehaviorConstructor) { super(); this.behavior = behavior; this.behaviorInstance = new this.behavior(this.getNextId(), {} as any); @@ -244,7 +246,7 @@ class BehaviorRecorder< * 群体的某种行为 */ class Behavior< - P extends IBehaviorParameterOption = {}, + P extends IBehaviorParameter = {}, E extends Record = {} > extends BehaviorInfo { @@ -272,7 +274,7 @@ class Behavior< /** * 对象参数列表 */ - public parameterOption: P = {} as any; + public parameterOption: IBehaviorParameterOption

= {} as any; public constructor(id: string, parameter: IBehaviorParameterValue

) { super(); @@ -355,5 +357,8 @@ class Behavior< } -export { Behavior, BehaviorRecorder }; +export { + Behavior, BehaviorRecorder, IBehaviorParameterOption, IBehaviorParameterOptionItem, + IAnyBehavior, IAnyBehaviorRecorder +}; export default { Behavior }; \ No newline at end of file diff --git a/source/Model/Model.ts b/source/Model/Model.ts index 6f445af..a28f07b 100644 --- a/source/Model/Model.ts +++ b/source/Model/Model.ts @@ -5,14 +5,14 @@ import { Emitter, EventType, EventMixin } from "./Emitter"; import { CtrlObject } from "./CtrlObject"; import { ObjectID, AbstractRenderer } from "./Renderer"; import { Label } from "./Label"; -import { Behavior, BehaviorRecorder } from "./Behavior"; +import { Behavior, IAnyBehavior, IAnyBehaviorRecorder } from "./Behavior"; type ModelEvent = { loop: number; labelChange: Label[]; objectChange: CtrlObject[]; individualChange: Group; - behaviorChange: Behavior; + behaviorChange: IAnyBehavior; }; /** @@ -182,12 +182,12 @@ class Model extends Emitter { /** * 行为池 */ - public behaviorPool: Behavior[] = []; + public behaviorPool: IAnyBehavior[] = []; /** * 添加一个行为 */ - public addBehavior>(recorder: BehaviorRecorder): B { + public addBehavior(recorder: B): B["behaviorInstance"] { let behavior = recorder.new(); behavior.load(this); this.behaviorPool.push(behavior); @@ -199,7 +199,7 @@ class Model extends Emitter { /** * 通过 ID 获取行为 */ - public getBehaviorById(id: ObjectID): Behavior | undefined { + public getBehaviorById(id: ObjectID): IAnyBehavior | undefined { for (let i = 0; i < this.behaviorPool.length; i++) { if (this.behaviorPool[i].id.toString() === id.toString()) { return this.behaviorPool[i]; @@ -211,8 +211,8 @@ class Model extends Emitter { * 搜索并删除一个 Behavior * @param name 搜索值 */ - public deleteBehavior(name: Behavior | ObjectID) { - let deletedBehavior: Behavior | undefined; + public deleteBehavior(name: IAnyBehavior | ObjectID) { + let deletedBehavior: IAnyBehavior | undefined; let index = 0; for (let i = 0; i < this.behaviorPool.length; i++) {