phantasmal-world/src/rendering/entities.ts
2019-06-27 18:50:22 +02:00

46 lines
1.3 KiB
TypeScript

import { autorun } from 'mobx';
import { BufferGeometry, DoubleSide, Mesh, MeshLambertMaterial } from 'three';
import { QuestNpc, QuestObject, QuestEntity } from '../domain';
export const OBJECT_COLOR = 0xFFFF00;
export const OBJECT_HOVER_COLOR = 0xFFDF3F;
export const OBJECT_SELECTED_COLOR = 0xFFAA00;
export const NPC_COLOR = 0xFF0000;
export const NPC_HOVER_COLOR = 0xFF3F5F;
export const NPC_SELECTED_COLOR = 0xFF0054;
export function create_object_mesh(object: QuestObject, geometry: BufferGeometry): Mesh {
return create_mesh(object, geometry, OBJECT_COLOR, 'Object');
}
export function create_npc_mesh(npc: QuestNpc, geometry: BufferGeometry): Mesh {
return create_mesh(npc, geometry, NPC_COLOR, 'NPC');
}
function create_mesh(
entity: QuestEntity,
geometry: BufferGeometry,
color: number,
type: string
): Mesh {
const object_3d = new Mesh(
geometry,
new MeshLambertMaterial({
color,
side: DoubleSide
})
);
object_3d.name = type;
object_3d.userData.entity = entity;
// TODO: dispose autorun?
autorun(() => {
const { x, y, z } = entity.position;
object_3d.position.set(x, y, z);
const rot = entity.rotation;
object_3d.rotation.set(rot.x, rot.y, rot.z);
});
return object_3d;
}