Optimizing behavioral models
This commit is contained in:
		
							parent
							
								
									feb277c3a2
								
							
						
					
					
						commit
						850eea254c
					
				| @ -6,12 +6,6 @@ import type { Model } from "./Model"; | |||||||
| import type { Range } from "./Range"; | import type { Range } from "./Range"; | ||||||
| import type { Label } from "./Label"; | import type { Label } from "./Label"; | ||||||
| 
 | 
 | ||||||
| /** |  | ||||||
|  * 行为构造函数类型 |  | ||||||
|  */ |  | ||||||
| type IBehaviorConstructor<B extends Behavior<any, any>> = |  | ||||||
|     new (id: string, parameter: IBehaviorParameterValue<B["parameterOption"]>) => B; |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * 参数类型 |  * 参数类型 | ||||||
|  */ |  */ | ||||||
| @ -43,8 +37,6 @@ type IObjectType = keyof IMapObjectParamTypeKeyToType; | |||||||
| type IVectorType = keyof IMapVectorParamTypeKeyToType; | type IVectorType = keyof IMapVectorParamTypeKeyToType; | ||||||
| type IParamValue<K extends IParamType> = AllMapType[K]; | type IParamValue<K extends IParamType> = AllMapType[K]; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * 特殊对象类型判定 |  * 特殊对象类型判定 | ||||||
|  */ |  */ | ||||||
| @ -111,25 +103,34 @@ interface IBehaviorParameterOptionItem<T extends IParamType = IParamType> { | |||||||
|     iconName?: string; |     iconName?: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | interface IBehaviorParameter { | ||||||
|  * 参数键值类型 |     [x: string]: IParamType; | ||||||
|  */ | } | ||||||
| type IBehaviorParameterValueItem<P extends IBehaviorParameterOptionItem> = IParamValue<P["type"]>; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 参数类型列表 |  * 参数类型列表 | ||||||
|  */ |  */ | ||||||
| interface IBehaviorParameterOption { | type IBehaviorParameterOption<P extends IBehaviorParameter> = { | ||||||
|     [x: string]: IBehaviorParameterOptionItem; |     [X in keyof P]: IBehaviorParameterOptionItem<P[X]>; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 参数类型列表映射到参数对象 |  * 参数类型列表映射到参数对象 | ||||||
|  */ |  */ | ||||||
| type IBehaviorParameterValue<P extends IBehaviorParameterOption> = { | type IBehaviorParameterValue<P extends IBehaviorParameter> = { | ||||||
|     [x in keyof P]: IBehaviorParameterValueItem<P[x]> |     [X in keyof P]: IParamValue<P[X]> | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * 行为构造函数类型 | ||||||
|  |  */ | ||||||
|  | type IBehaviorConstructor< | ||||||
|  |     P extends IBehaviorParameter = {}, | ||||||
|  |     E extends Record<EventType, any> = {} | ||||||
|  | > = new (id: string, parameter: IBehaviorParameterValue<P>) => Behavior<P, E>; | ||||||
|  | 
 | ||||||
|  | type IAnyBehavior = Behavior<any, any>; | ||||||
|  | type IAnyBehaviorRecorder = BehaviorRecorder<any, any>; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 行为的基础信息 |  * 行为的基础信息 | ||||||
| @ -158,8 +159,9 @@ class BehaviorInfo<E extends Record<EventType, any> = {}> extends Emitter<E> { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class BehaviorRecorder< | class BehaviorRecorder< | ||||||
|     B extends Behavior<any, any> |     P extends IBehaviorParameter = {}, | ||||||
| > extends BehaviorInfo { |     E extends Record<EventType, any> = {} | ||||||
|  | > extends BehaviorInfo<{}> { | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 命名序号 |      * 命名序号 | ||||||
| @ -176,23 +178,23 @@ class BehaviorRecorder< | |||||||
|     /** |     /** | ||||||
|      * 行为类型 |      * 行为类型 | ||||||
|      */ |      */ | ||||||
|     public behavior: IBehaviorConstructor<B>; |     public behavior: IBehaviorConstructor<P, E>; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 行为实例 |      * 行为实例 | ||||||
|      */ |      */ | ||||||
|     public behaviorInstance: B; |     public behaviorInstance: Behavior<P, E>; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 对象参数列表 |      * 对象参数列表 | ||||||
|      */ |      */ | ||||||
|     public parameterOption: B["parameterOption"]; |     public parameterOption: IBehaviorParameterOption<P>; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 获取参数列表的默认值 |      * 获取参数列表的默认值 | ||||||
|      */ |      */ | ||||||
|     public getDefaultValue(): IBehaviorParameterValue<B["parameterOption"]> { |     public getDefaultValue(): IBehaviorParameterValue<P> { | ||||||
|         let defaultObj = {} as IBehaviorParameterValue<B["parameterOption"]>; |         let defaultObj = {} as IBehaviorParameterValue<P>; | ||||||
|         for (let key in this.parameterOption) { |         for (let key in this.parameterOption) { | ||||||
|             let defaultVal = this.parameterOption[key].defaultValue; |             let defaultVal = this.parameterOption[key].defaultValue; | ||||||
|              |              | ||||||
| @ -224,11 +226,11 @@ class BehaviorRecorder< | |||||||
|     /** |     /** | ||||||
|      * 创建一个新的行为实例 |      * 创建一个新的行为实例 | ||||||
|      */ |      */ | ||||||
|     public new(): B { |     public new(): Behavior<P, E> { | ||||||
|         return new this.behavior(this.getNextId(), this.getDefaultValue()); |         return new this.behavior(this.getNextId(), this.getDefaultValue()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public constructor(behavior: IBehaviorConstructor<B>) { |     public constructor(behavior: IBehaviorConstructor<P, E>) { | ||||||
|         super(); |         super(); | ||||||
|         this.behavior = behavior; |         this.behavior = behavior; | ||||||
|         this.behaviorInstance = new this.behavior(this.getNextId(), {} as any); |         this.behaviorInstance = new this.behavior(this.getNextId(), {} as any); | ||||||
| @ -244,7 +246,7 @@ class BehaviorRecorder< | |||||||
|  * 群体的某种行为 |  * 群体的某种行为 | ||||||
|  */ |  */ | ||||||
| class Behavior< | class Behavior< | ||||||
|     P extends IBehaviorParameterOption = {}, |     P extends IBehaviorParameter = {}, | ||||||
|     E extends Record<EventType, any> = {} |     E extends Record<EventType, any> = {} | ||||||
| > extends BehaviorInfo<E> { | > extends BehaviorInfo<E> { | ||||||
| 
 | 
 | ||||||
| @ -272,7 +274,7 @@ class Behavior< | |||||||
|     /** |     /** | ||||||
|      * 对象参数列表 |      * 对象参数列表 | ||||||
|      */ |      */ | ||||||
|     public parameterOption: P = {} as any; |     public parameterOption: IBehaviorParameterOption<P> = {} as any; | ||||||
| 
 | 
 | ||||||
|     public constructor(id: string, parameter: IBehaviorParameterValue<P>) { |     public constructor(id: string, parameter: IBehaviorParameterValue<P>) { | ||||||
|         super(); |         super(); | ||||||
| @ -355,5 +357,8 @@ class Behavior< | |||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export { Behavior, BehaviorRecorder }; | export { | ||||||
|  |     Behavior, BehaviorRecorder, IBehaviorParameterOption, IBehaviorParameterOptionItem, | ||||||
|  |     IAnyBehavior, IAnyBehaviorRecorder | ||||||
|  | }; | ||||||
| export default { Behavior }; | export default { Behavior }; | ||||||
| @ -5,14 +5,14 @@ import { Emitter, EventType, EventMixin } from "./Emitter"; | |||||||
| import { CtrlObject } from "./CtrlObject"; | import { CtrlObject } from "./CtrlObject"; | ||||||
| import { ObjectID, AbstractRenderer } from "./Renderer"; | import { ObjectID, AbstractRenderer } from "./Renderer"; | ||||||
| import { Label } from "./Label"; | import { Label } from "./Label"; | ||||||
| import { Behavior, BehaviorRecorder } from "./Behavior"; | import { Behavior, IAnyBehavior, IAnyBehaviorRecorder } from "./Behavior"; | ||||||
| 
 | 
 | ||||||
| type ModelEvent = { | type ModelEvent = { | ||||||
|     loop: number; |     loop: number; | ||||||
|     labelChange: Label[]; |     labelChange: Label[]; | ||||||
|     objectChange: CtrlObject[]; |     objectChange: CtrlObject[]; | ||||||
|     individualChange: Group; |     individualChange: Group; | ||||||
|     behaviorChange: Behavior; |     behaviorChange: IAnyBehavior; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -182,12 +182,12 @@ class Model extends Emitter<ModelEvent> { | |||||||
|     /** |     /** | ||||||
|      * 行为池 |      * 行为池 | ||||||
|      */ |      */ | ||||||
|     public behaviorPool: Behavior<any, any>[] = []; |     public behaviorPool: IAnyBehavior[] = []; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 添加一个行为 |      * 添加一个行为 | ||||||
|      */ |      */ | ||||||
|     public addBehavior<B extends Behavior<any, any>>(recorder: BehaviorRecorder<B>): B { |     public addBehavior<B extends IAnyBehaviorRecorder>(recorder: B): B["behaviorInstance"] { | ||||||
|         let behavior = recorder.new(); |         let behavior = recorder.new(); | ||||||
|         behavior.load(this); |         behavior.load(this); | ||||||
|         this.behaviorPool.push(behavior); |         this.behaviorPool.push(behavior); | ||||||
| @ -199,7 +199,7 @@ class Model extends Emitter<ModelEvent> { | |||||||
|     /** |     /** | ||||||
|      * 通过 ID 获取行为 |      * 通过 ID 获取行为 | ||||||
|      */ |      */ | ||||||
|     public getBehaviorById(id: ObjectID): Behavior<any, any> | undefined { |     public getBehaviorById(id: ObjectID): IAnyBehavior | undefined { | ||||||
|         for (let i = 0; i < this.behaviorPool.length; i++) { |         for (let i = 0; i < this.behaviorPool.length; i++) { | ||||||
|             if (this.behaviorPool[i].id.toString() === id.toString()) { |             if (this.behaviorPool[i].id.toString() === id.toString()) { | ||||||
|                 return this.behaviorPool[i]; |                 return this.behaviorPool[i]; | ||||||
| @ -211,8 +211,8 @@ class Model extends Emitter<ModelEvent> { | |||||||
|      * 搜索并删除一个 Behavior |      * 搜索并删除一个 Behavior | ||||||
|      * @param name 搜索值 |      * @param name 搜索值 | ||||||
|      */ |      */ | ||||||
|     public deleteBehavior(name: Behavior<any, any> | ObjectID) { |     public deleteBehavior(name: IAnyBehavior | ObjectID) { | ||||||
|         let deletedBehavior: Behavior<any, any> | undefined; |         let deletedBehavior: IAnyBehavior | undefined; | ||||||
|         let index = 0; |         let index = 0; | ||||||
| 
 | 
 | ||||||
|         for (let i = 0; i < this.behaviorPool.length; i++) { |         for (let i = 0; i < this.behaviorPool.length; i++) { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user