-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.ts
40 lines (32 loc) · 1.03 KB
/
player.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as THREE from 'three';
import { TileIndex, indexToWorldPosition } from './world';
interface Player {
mesh: THREE.Mesh;
position: THREE.Vector3;
index: TileIndex;
}
interface PlayerInit {
scene: THREE.Scene;
}
export const player: Player = {
index: null,
mesh: null,
position: null,
};
export function init(playerInit: PlayerInit) {
const geometry = new THREE.BoxGeometry(0.6, 0.6, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x22aa33 });
const mesh = new THREE.Mesh(geometry, material);
playerInit.scene.add(mesh);
player.mesh = mesh;
player.index = { x: 0, y: 0, z: 0 };
player.position = new THREE.Vector3(0, 0, 0.8);
// DEBUG:
window.player = player;
}
export function loop(dt: number) {
const tilePos = indexToWorldPosition(player.index);
const withOffset = tilePos.add(new THREE.Vector3(0, 0, 0.8));
player.position.fromArray(withOffset.toArray());
player.mesh.position.set(player.position.x, player.position.y, player.position.z);
}