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

122 lines
3.3 KiB
TypeScript

import { Color, Euler, Object3D, Vector3 } from 'three';
import { Property } from './property';
import { EditorProperty } from '../decorators/property';
import { readMetadataOf } from '../utils/read-metadata';
export class GameObject extends Object3D {
public objectType = 'GameObject';
public virtual = false;
@EditorProperty({ type: String })
public override name: string = '';
@EditorProperty({ type: Boolean })
public override visible: boolean = true;
constructor() {
super();
this.name = this.objectType;
}
private get excludedProperties() {
return readMetadataOf<string>(this, 'excludedProperties');
}
/** The exposed properties for this game object, used for the editor */
get properties() {
const exclude = this.excludedProperties;
const properties = readMetadataOf<Property>(this, 'properties');
return properties.filter(
(item) => !exclude.includes(item.definition.name!)
);
}
/**
* 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 = this.properties.map((property) => property.definition.name!);
Object.assign(
object,
keys.reduce<{ [x: string]: unknown }>((obj, key) => {
const indexable = this as Record<string, unknown>;
const value = (indexable[key] as any)?.toArray
? (indexable[key] as any).toArray()
: indexable[key];
return {
...obj,
[key]: value,
};
}, {})
);
return object;
}
override copy(object: Object3D, recursive = true) {
super.copy(object as any, recursive);
this.properties
.map((property) => property.definition.name!)
.filter((key) => !['position', 'rotation', 'scale'].includes(key))
.forEach((key) => {
(this as any)[key] = (object as any)[key];
});
return this;
}
/**
* Deserialize a serialized object into properties on this object.
* @param input Serialized information
*/
deserialize(input: SerializedObject) {
Object.keys(input)
.filter((key) => !['children', 'objectType'].includes(key))
.forEach((key) => {
const indexable = this as any;
if (indexable[key]?.fromArray && Array.isArray(input[key])) {
indexable[key].fromArray(input[key]);
} else if (indexable[key].isColor) {
indexable[key] = new Color(input[key] as string);
} else if (indexable[key].copy) {
indexable[key].copy(input[key]);
} else {
indexable[key] = input[key];
}
});
}
}
export class GameObject3D extends GameObject {
public objectType = 'GameObject3D';
@EditorProperty({ type: Boolean })
public locked = false;
@EditorProperty({ type: Vector3 })
public override position!: Vector3;
@EditorProperty({ type: Vector3 })
public override scale!: Vector3;
@EditorProperty({ type: Euler })
public override rotation!: Euler;
}
export interface SerializedObject {
[x: string]: unknown;
name: string;
objectType: string;
children: SerializedObject[];
visible: boolean;
}