blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
67 lines • 2.31 kB
JavaScript
import { mergeDeep } from "./utils/objects";
import Entity from "./entity";
import Camera from "./camera/camera";
import Viewport from "./camera/viewport";
import { vec2 } from "gl-matrix";
import RectCollider from "./physics/collider/rect";
import Rect from "./shapes/rect";
const defaultKeys = {
forward: "KeyW",
back: "KeyS",
left: "KeyA",
right: "KeyD",
sprint: "ShiftLeft",
};
/**
* A highly customizable player controller.
*/
export default class Player extends Entity {
/**
* Creates a {@link Player} instance with the given settings.
*
* @param pos The player's initial world position
* @param dimensions The width and height of the player's bounding box and body
* @param cameraViewport The width and height of the player camera's viewport
* @param keys The key map to use for player controls
*/
constructor(pos = vec2.fromValues(0, 0), dimensions = vec2.fromValues(2, 3), cameraViewport, keys = defaultKeys) {
super(pos, new RectCollider(dimensions[0], dimensions[1], pos), [
new Rect(dimensions[0], dimensions[1], vec2.fromValues(0, 0)),
]);
// right most value wins key collisions
this.keys = mergeDeep(defaultKeys, keys);
this.camera = new Camera(this.getPosition(), new Viewport(pos, cameraViewport[0], cameraViewport[1]));
// this.controls = new PointerLockControls(<HTMLCanvasElement>gl.canvas, this.camera, this);
}
/**
* Updates the player's camera, rotation, velocity and picks blocks if block picking is enabled.
*
* @param delta The time since the last tick in ms
*/
update(delta) {
super.update(delta);
// const speed = 0.5;
// if (isKeyPressed("KeyD")) {
// this.moveRight(speed);
// }
// if (isKeyPressed("KeyA")) {
// this.moveRight(-speed);
// }
// if (isKeyPressed("KeyW")) {
// this.moveUp(speed);
// }
// if (isKeyPressed("KeyS")) {
// this.moveUp(-speed);
// }
this.camera.setPosition(this.getPosition());
}
/**
* Gets the player's current camera.
*
* @returns The player's camera
*/
getCamera() {
return this.camera;
}
}
//# sourceMappingURL=player.js.map