freeblox/packages/engine/src/gameobjects/brick.object.ts

51 lines
1.2 KiB
TypeScript

import {
BufferGeometry,
Color,
ColorRepresentation,
Material,
Mesh,
MeshPhongMaterial,
} from 'three';
import {
EditorProperties,
GameObject3D,
gameObject3DEditorProperties,
} from '../types/game-object';
import { Property } from '../types/property';
import { gameObjectFactory } from './factory';
export const brickEditorProperties: EditorProperties = {
...gameObject3DEditorProperties,
color: new Property('color', Color, true, []),
transparency: new Property('transparency', Number, true, []),
};
export class Brick extends GameObject3D {
public objectType = Brick.name;
protected material = new MeshPhongMaterial();
protected mesh: Mesh = new Mesh(this.geometry, this.material);
constructor(
protected geometry: BufferGeometry = gameObjectFactory.boxGeometry,
public editorProperties: EditorProperties = brickEditorProperties
) {
super(editorProperties);
this.add(this.mesh);
}
get color() {
return this.material.color;
}
set color(color: ColorRepresentation) {
this.material.color = new Color(color);
}
get transparency() {
return 1 - this.material.opacity;
}
set transparency(value: number) {
this.material.transparent = value != 0;
this.material.opacity = 1 - value;
}
}