50 lines
No EOL
1.8 KiB
JavaScript
50 lines
No EOL
1.8 KiB
JavaScript
import { Input } from '../io/Input.js'
|
|
import { M } from '../math/Math.js'
|
|
import { Vec3 } from '../math/Vec3.js'
|
|
|
|
export class SimpleController {
|
|
|
|
constructor(camera) {
|
|
this.camera = camera
|
|
this.lookSpeed = 100
|
|
this.moveSpeed = 10
|
|
}
|
|
|
|
update(dt) {
|
|
var press = Input.isKeyDown
|
|
var rotate = new Vec3();
|
|
if (press('ArrowRight')) rotate.y += 1;
|
|
if (press('ArrowLeft')) rotate.y -= 1;
|
|
if (press('ArrowUp')) rotate.x -= 1;
|
|
if (press('ArrowDown')) rotate.x += 1;
|
|
if(rotate.dot(rotate) > Number.EPSILON) {
|
|
var normal = rotate.normalize()
|
|
this.camera.rotation.x += this.lookSpeed * dt * normal.x;
|
|
this.camera.rotation.y += this.lookSpeed * dt * normal.y;
|
|
this.camera.rotation.z += this.lookSpeed * dt * normal.z;
|
|
}
|
|
this.camera.rotation.x = M.clamp(this.camera.rotation.x, -90, 90)
|
|
this.camera.rotation.y = this.camera.rotation.y % 360
|
|
|
|
const yaw = this.camera.rotation.y * (Math.PI/180);
|
|
var forward = new Vec3(Math.sin(yaw), 0, Math.cos(yaw))
|
|
var left = new Vec3(-forward.z, 0, forward.x)
|
|
var up = new Vec3(0, 1, 0)
|
|
|
|
var move = new Vec3();
|
|
if (press('w')) move.add(forward)
|
|
if (press('s')) move.sub(forward)
|
|
if (press('a')) move.add(left)
|
|
if (press('d')) move.sub(left)
|
|
if (press('q')) move.sub(up)
|
|
if (press('e')) move.add(up)
|
|
|
|
if(move.dot(move) > Number.EPSILON) {
|
|
var normal = move.normalize()
|
|
this.camera.position.x += this.moveSpeed * dt * normal.x;
|
|
this.camera.position.y += this.moveSpeed * dt * normal.y;
|
|
this.camera.position.z += this.moveSpeed * dt * normal.z;
|
|
}
|
|
}
|
|
|
|
} |