Initial commit
This commit is contained in:
45
C#/TSEngine/Core/Behaviors/behavior.ts
Normal file
45
C#/TSEngine/Core/Behaviors/behavior.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace TSE {
|
||||
|
||||
export class BaseBehavior {
|
||||
public name: string;
|
||||
protected _data: IBehaviorData;
|
||||
protected _owner: SimObject;
|
||||
|
||||
public constructor(data: IBehaviorData) {
|
||||
this._data = data;
|
||||
this.name = data.name;
|
||||
}
|
||||
|
||||
public set owner(value: SimObject) { this._owner = value; }
|
||||
|
||||
public update(time: number): void {}
|
||||
public apply(userData: any): void {}
|
||||
|
||||
}
|
||||
|
||||
export interface IBehaviorBuilder {
|
||||
get type(): string;
|
||||
buildFromJson(json: any): BaseBehavior;
|
||||
}
|
||||
|
||||
export interface IBehaviorData {
|
||||
name: string;
|
||||
setFromJson(json: any): void;
|
||||
}
|
||||
|
||||
export class BehaviorManager {
|
||||
|
||||
private static _registeredBuilders: {[type: string]: IBehaviorBuilder} = {};
|
||||
|
||||
public static registerBuilder(builder: IBehaviorBuilder): void {
|
||||
BehaviorManager._registeredBuilders[builder.type] = builder;
|
||||
}
|
||||
|
||||
public static extractComponent(json: any): BaseBehavior {
|
||||
if (BehaviorManager._registeredBuilders[json?.type] === undefined) return undefined;
|
||||
return BehaviorManager._registeredBuilders[json.type].buildFromJson(json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
46
C#/TSEngine/Core/Behaviors/rotationBehavior.ts
Normal file
46
C#/TSEngine/Core/Behaviors/rotationBehavior.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
///<reference path="behavior.ts"/>
|
||||
|
||||
namespace TSE {
|
||||
|
||||
export class RotationBehaviorData implements IBehaviorData {
|
||||
public name: string;
|
||||
public rotation: Vector3 = Vector3.zero;
|
||||
|
||||
public setFromJson(json: any): void {
|
||||
if (json?.name === undefined) return;
|
||||
this.name = String(json.name);
|
||||
this.rotation.setFromJson(json.rotation);
|
||||
}
|
||||
}
|
||||
|
||||
export class RotationBehaviorBuilder implements IBehaviorBuilder {
|
||||
public buildFromJson(json: any): TSE.BaseBehavior {
|
||||
let data = new RotationBehaviorData();
|
||||
data.setFromJson(json);
|
||||
return new RotationBehavior(data);
|
||||
}
|
||||
|
||||
public get type(): string { return "rotation"; }
|
||||
}
|
||||
|
||||
export class RotationBehavior extends BaseBehavior{
|
||||
|
||||
private _rotation: Vector3;
|
||||
|
||||
public constructor(data: RotationBehaviorData) {
|
||||
super(data);
|
||||
|
||||
this._rotation = data.rotation;
|
||||
}
|
||||
|
||||
public update(time: number) {
|
||||
this._owner.transform.rotation.add(this._rotation);
|
||||
|
||||
super.update(time);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BehaviorManager.registerBuilder(new RotationBehaviorBuilder());
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user