freeblox/packages/engine/src/types/game-object.ts

69 lines
1.7 KiB
TypeScript

import { Euler, Object3D, Vector3 } from 'three';
import { Property } from './property';
export type EditorProperties = { [x: string]: Property };
export const gameObjectEditorProperties: EditorProperties = {
name: new Property('name', String, true, []),
visible: new Property('visible', Boolean, true, []),
};
export const gameObject3DEditorProperties: EditorProperties = {
...gameObjectEditorProperties,
position: new Property('position', Vector3, true, []),
scale: new Property('scale', Vector3, true, []),
rotation: new Property('rotation', Euler, true, []),
};
export class GameObject extends Object3D {
public objectType = 'GameObject';
constructor(
public editorProperties: EditorProperties = gameObjectEditorProperties
) {
super();
}
/**
* Serialize GameObject for exporting
*/
serialize() {
const object: SerializedObject = {
name: this.name,
objectType: this.objectType,
children: this.children
.filter((entry) => entry instanceof GameObject)
.map((entry) => (entry as GameObject).serialize()),
visible: this.visible,
};
const keys = Object.keys(this.editorProperties);
Object.assign(
object,
keys.reduce<{ [x: string]: unknown }>(
(obj, key) => ({
...obj,
[key]: (this as Record<string, unknown>)[key],
}),
{}
)
);
return object;
}
}
export class GameObject3D extends GameObject {
public objectType = 'GameObject3D';
constructor(public editorProperties = gameObject3DEditorProperties) {
super(editorProperties);
}
}
export interface SerializedObject {
[x: string]: unknown;
name: string;
objectType: string;
children: SerializedObject[];
visible: boolean;
}