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

76 lines
1.8 KiB
TypeScript

import {
BufferGeometry,
Color,
ColorRepresentation,
Mesh,
MeshPhongMaterial,
} from 'three';
import { GameObject3D } from '../types/game-object';
import { gameObjectGeometries } from './geometries';
import { assetManager } from '../assets/manager';
import { AssetInfo } from '../types/asset';
import { EditorProperty } from '../decorators/property';
export class Brick extends GameObject3D {
public objectType = Brick.name;
private texturePath?: string;
protected material = new MeshPhongMaterial();
protected mesh: Mesh = new Mesh(this.geometry, this.material);
@EditorProperty({ type: Color })
get color() {
return this.material.color;
}
set color(color: ColorRepresentation) {
this.material.color = new Color(color);
}
@EditorProperty({ type: Number })
get transparency() {
return 1 - this.material.opacity;
}
set transparency(value: number) {
this.material.transparent = value != 0;
this.material.opacity = 1 - value;
this.material.needsUpdate = true;
}
@EditorProperty({ type: AssetInfo })
get texture() {
return this.texturePath;
}
set texture(path: string | undefined) {
if (!path) {
this.material.map = null;
this.texturePath = undefined;
return;
}
const asset = assetManager.getAssetByPath(path);
if (!asset || !asset.texture) {
console.error(`Asset ${path} does not exist or is not loaded`);
return;
}
this.texturePath = path;
this.material.map = asset.texture;
}
@EditorProperty({ type: Boolean })
public canCollide = true;
@EditorProperty({ type: Boolean })
public anchored = true;
@EditorProperty({ type: Number })
public mass = 1;
constructor(
protected geometry: BufferGeometry = gameObjectGeometries.boxGeometry
) {
super();
this.name = this.objectType;
this.add(this.mesh);
}
}