Add behavior archive function
This commit is contained in:
parent
068acb19a8
commit
74bda21072
@ -2,6 +2,9 @@ import { Emitter, EventType } from "@Model/Emitter";
|
||||
import { IArchiveCtrlObject } from "@Model/CtrlObject";
|
||||
import { Model } from "@Model/Model";
|
||||
import { IArchiveLabel } from "@Model/Label";
|
||||
import { Group, IArchiveGroup } from "@Model/Group";
|
||||
import { IArchiveIndividual } from "@Model/Individual";
|
||||
import { IArchiveBehavior } from "@Model/Behavior";
|
||||
|
||||
interface IArchiveEvent {
|
||||
fileChange: Archive;
|
||||
@ -11,6 +14,7 @@ interface IArchiveObject {
|
||||
nextIndividualId: number;
|
||||
objectPool: IArchiveCtrlObject[];
|
||||
labelPool: IArchiveLabel[];
|
||||
behaviorPool: IArchiveBehavior[];
|
||||
}
|
||||
|
||||
class Archive<
|
||||
@ -47,19 +51,42 @@ class Archive<
|
||||
// 存贮 CtrlObject
|
||||
const objectPool: IArchiveCtrlObject[] = [];
|
||||
model.objectPool.forEach(obj => {
|
||||
objectPool.push(obj.toArchive());
|
||||
let archiveObject = obj.toArchive();
|
||||
|
||||
// 处理每个群的个体
|
||||
if (archiveObject.objectType === "G") {
|
||||
const archiveGroup: IArchiveGroup = archiveObject as any;
|
||||
const group: Group = obj as Group;
|
||||
|
||||
const individuals: IArchiveIndividual[] = [];
|
||||
group.individuals.forEach((item) => {
|
||||
individuals.push(item.toArchive());
|
||||
});
|
||||
|
||||
archiveGroup.individuals = individuals;
|
||||
}
|
||||
|
||||
objectPool.push(archiveObject);
|
||||
})
|
||||
|
||||
// 存储 Label
|
||||
const labelPool: IArchiveLabel[] = [];
|
||||
model.labelPool.forEach(obj => {
|
||||
labelPool.push(obj.toArchive());
|
||||
})
|
||||
});
|
||||
|
||||
// 存储全部行为
|
||||
const behaviorPool: IArchiveBehavior[] = [];
|
||||
model.behaviorPool.forEach(obj => {
|
||||
behaviorPool.push(obj.toArchive());
|
||||
});
|
||||
|
||||
// 生成存档对象
|
||||
const fileData: IArchiveObject = {
|
||||
nextIndividualId: model.nextIndividualId,
|
||||
objectPool: objectPool,
|
||||
labelPool: labelPool
|
||||
labelPool: labelPool,
|
||||
behaviorPool: behaviorPool
|
||||
};
|
||||
|
||||
console.log(fileData);
|
||||
|
@ -3,7 +3,11 @@ import { v4 as uuid } from "uuid";
|
||||
import type { Individual } from "@Model/Individual";
|
||||
import type { Group } from "@Model/Group";
|
||||
import type { Model } from "@Model/Model";
|
||||
import { getDefaultValue, IParameter, IParameterOption, IParameterValue } from "@Model/Parameter";
|
||||
import {
|
||||
archiveObject2Parameter,
|
||||
getDefaultValue, IArchiveParameterValue, IArchiveParseFn,
|
||||
IParameter, IParameterOption, IParameterValue, parameter2ArchiveObject
|
||||
} from "@Model/Parameter";
|
||||
|
||||
/**
|
||||
* 行为构造函数类型
|
||||
@ -113,6 +117,17 @@ class BehaviorRecorder<
|
||||
}
|
||||
}
|
||||
|
||||
interface IArchiveBehavior {
|
||||
behaviorId: string;
|
||||
name: string;
|
||||
id: string;
|
||||
color: number[];
|
||||
priority: number;
|
||||
currentGroupKey: string[];
|
||||
deleteFlag: boolean;
|
||||
parameter: IArchiveParameterValue<IParameter>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 群体的某种行为
|
||||
*/
|
||||
@ -189,6 +204,33 @@ class Behavior<
|
||||
return this.deleteFlag;
|
||||
}
|
||||
|
||||
public toArchive(): IArchiveBehavior {
|
||||
return {
|
||||
behaviorId: this.behaviorId,
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
color: this.color.concat([]),
|
||||
priority: this.priority,
|
||||
currentGroupKey: this.currentGroupKey.concat([]) as any,
|
||||
deleteFlag: this.deleteFlag,
|
||||
parameter: parameter2ArchiveObject(
|
||||
this.parameter, this.parameterOption
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
public fromArchive(archive: IArchiveBehavior, paster: IArchiveParseFn): void {
|
||||
this.name = this.name,
|
||||
this.id = this.id,
|
||||
this.color = this.color.concat([]),
|
||||
this.priority = this.priority,
|
||||
this.currentGroupKey = this.currentGroupKey.concat([]) as any,
|
||||
this.deleteFlag = this.deleteFlag,
|
||||
this.parameter = archiveObject2Parameter(
|
||||
archive.parameter, paster
|
||||
) as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载时调用
|
||||
*/
|
||||
@ -241,6 +283,6 @@ class Behavior<
|
||||
type IRenderBehavior = BehaviorInfo | Behavior;
|
||||
|
||||
export {
|
||||
Behavior, BehaviorRecorder, IAnyBehavior, IAnyBehaviorRecorder, BehaviorInfo, IRenderBehavior
|
||||
};
|
||||
export default { Behavior };
|
||||
Behavior, BehaviorRecorder, IAnyBehavior, IAnyBehaviorRecorder,
|
||||
BehaviorInfo, IRenderBehavior, IArchiveBehavior
|
||||
};
|
@ -4,16 +4,18 @@ import type { IAnyObject, Model } from "@Model/Model";
|
||||
import type { ObjectID } from "@Model/Model";
|
||||
import {
|
||||
parameter2ArchiveObject, archiveObject2Parameter,
|
||||
IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject
|
||||
IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject,
|
||||
IArchiveParameterValue, IParameter
|
||||
} from "@Model/Parameter";
|
||||
|
||||
interface IArchiveCtrlObject {
|
||||
objectType: "R" | "G";
|
||||
displayName: CtrlObject["displayName"];
|
||||
color: CtrlObject["color"];
|
||||
display: CtrlObject["display"];
|
||||
update: CtrlObject["update"];
|
||||
id: string;
|
||||
renderParameter: any;
|
||||
renderParameter: IArchiveParameterValue<IParameter>;
|
||||
deleteFlag: CtrlObject["deleteFlag"];
|
||||
labels: IObjectParamArchiveType[];
|
||||
}
|
||||
|
@ -4,7 +4,11 @@ import type { Behavior, IAnyBehavior } from "@Model/Behavior";
|
||||
import { Label } from "@Model/Label";
|
||||
import { Range } from "@Model/Range";
|
||||
import { Model, ObjectID } from "@Model/Model";
|
||||
import { getDefaultValue, IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject } from "@Model/Parameter";
|
||||
import { IArchiveIndividual } from "@Model/Individual";
|
||||
import {
|
||||
getDefaultValue, IArchiveParseFn,
|
||||
IObjectParamArchiveType, object2ArchiveObject
|
||||
} from "@Model/Parameter";
|
||||
|
||||
enum GenMod {
|
||||
Point = "p",
|
||||
@ -12,6 +16,7 @@ enum GenMod {
|
||||
}
|
||||
|
||||
interface IArchiveGroup {
|
||||
individuals: IArchiveIndividual[];
|
||||
genMethod: Group["genMethod"];
|
||||
genPoint: Group["genPoint"];
|
||||
genRange: IObjectParamArchiveType | undefined;
|
||||
@ -421,6 +426,7 @@ class Group extends CtrlObject<IArchiveGroup> {
|
||||
public override toArchive(): IArchiveCtrlObject & IArchiveGroup {
|
||||
return {
|
||||
...super.toArchive(),
|
||||
objectType: "G",
|
||||
genMethod: this.genMethod,
|
||||
genPoint: this.genPoint.concat([]),
|
||||
genRange: object2ArchiveObject(this.genRange) as IObjectParamArchiveType,
|
||||
@ -456,4 +462,4 @@ class Group extends CtrlObject<IArchiveGroup> {
|
||||
}
|
||||
}
|
||||
|
||||
export { Group, GenMod };
|
||||
export { Group, GenMod, IArchiveGroup };
|
@ -243,4 +243,4 @@ class Individual {
|
||||
}
|
||||
}
|
||||
|
||||
export { Individual };
|
||||
export { Individual, IArchiveIndividual };
|
@ -358,5 +358,6 @@ export {
|
||||
IParamType, IParamValue, isObjectType, isVectorType, getDefaultValue,
|
||||
IParameterOptionItem, IParameter, IParameterOption, IParameterValue,
|
||||
object2ArchiveObject, parameter2ArchiveObject, archiveObject2Parameter,
|
||||
IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType
|
||||
IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType,
|
||||
IArchiveParameterValue
|
||||
}
|
@ -34,6 +34,7 @@ class Range extends CtrlObject<IArchiveRange> {
|
||||
public override toArchive(): IArchiveCtrlObject & IArchiveRange {
|
||||
return {
|
||||
...super.toArchive(),
|
||||
objectType: "R",
|
||||
position: this.position.concat([]),
|
||||
radius: this.radius.concat([])
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user