Add record model into actuator

This commit is contained in:
MrKBear 2022-04-29 14:32:00 +08:00
parent bc67782365
commit 776a5f571e
3 changed files with 115 additions and 8 deletions

View File

@ -1,5 +1,13 @@
import { Model } from "@Model/Model";
import { Emitter } from "@Model/Emitter";
import { Clip } from "@Model/Clip";
enum ActuatorModel {
Play = 1,
Record = 2,
View = 3,
Offline = 4
}
interface IActuatorEvent {
startChange: boolean;
@ -26,6 +34,43 @@ class Actuator extends Emitter<IActuatorEvent> {
*/
private startFlag: boolean = false;
/**
*
*/
public mod: ActuatorModel = ActuatorModel.View;
/**
*
*/
public recordClip?: Clip;
/**
*
*/
public startRecord(clip: Clip) {
// 记录录制片段
this.recordClip = clip;
// 如果仿真未开启,开启仿真
if (!this.start()) this.start(true);
// 设置状态
this.mod = ActuatorModel.Record;
}
/**
*
*/
public endRecord() {
// 如果仿真未停止,停止仿真
if (this.start()) this.start(false);
// 设置状态
this.mod = ActuatorModel.View;
}
/**
*
*/
@ -72,13 +117,29 @@ class Actuator extends Emitter<IActuatorEvent> {
} else {
this.alignTimer += durTime;
if (this.alignTimer > (1 / this.fps)) {
// 更新模型
this.model.update(this.alignTimer * this.speed);
// 绘制模型
this.model.draw();
// 录制模型
if (
this.mod === ActuatorModel.Record ||
this.mod === ActuatorModel.Offline
) {
this.recordClip?.record(this.alignTimer * this.speed);
}
this.emit("loop", this.alignTimer);
this.alignTimer = 0;
}
}
}
} else {
}
else {
this.emit("loop", Infinity);
}
}
@ -122,4 +183,4 @@ class Actuator extends Emitter<IActuatorEvent> {
}
}
export { Actuator }
export { Actuator, ActuatorModel }

View File

@ -1,4 +1,5 @@
import { IAnyObject, Model } from "@Model/Model";
import { v4 as uuid } from "uuid";
import { Group } from "@Model/Group";
import { Range } from "@Model/Range";
@ -21,6 +22,8 @@ interface IFrame {
*/
class Clip {
public id: string;
/**
*
*/
@ -78,10 +81,13 @@ class Clip {
return frame;
}
public equal(clip?: Clip) {
return clip === this || clip?.id === this.id;
}
public constructor(model: Model) {
this.model = model;
this.id = uuid();
}
}

View File

@ -23,6 +23,7 @@ type ModelEvent = {
objectChange: CtrlObject[];
individualChange: Group;
behaviorChange: IAnyBehavior;
clipChange: Clip[];
};
/**
@ -331,8 +332,51 @@ class Model extends Emitter<ModelEvent> {
}
}
/**
*
*/
public clipPool: Clip[] = [];
/**
*
*/
public addClip(name?: string): Clip {
let newClip = new Clip(this);
newClip.name = name ?? "";
this.clipPool.push(newClip);
console.log(`Model: Create clip ${name ?? newClip.id}`);
this.emit("clipChange", this.clipPool);
return newClip;
}
/**
*
*/
public deleteClip(name: ObjectID | Clip) {
let deletedClip: Clip | undefined;
let index = 0;
for (let i = 0; i < this.clipPool.length; i++) {
if (name instanceof Clip) {
if (this.clipPool[i].equal(name)) {
deletedClip = this.clipPool[i];
index = i;
}
}
else if (name === this.clipPool[i].id) {
deletedClip = this.clipPool[i];
index = i;
}
}
if (deletedClip) {
this.behaviorPool.splice(index, 1);
console.log(`Model: Delete clip ${deletedClip.name ?? deletedClip.id}`);
this.emit("clipChange", this.clipPool);
}
}
/**
*
*/
@ -350,7 +394,7 @@ class Model extends Emitter<ModelEvent> {
/**
*
*/
public update(t: number, skipDraw: boolean = false) {
public update(t: number) {
// 第一轮更新
for (let i = 0; i < this.objectPool.length; i++) {
@ -375,10 +419,6 @@ class Model extends Emitter<ModelEvent> {
object.runner(t, "finalEffect");
}
}
if (!skipDraw) {
this.draw();
}
}
public draw() {