12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import { canvas } from '../Core.js';
- import { keys, KeyCode } from '../KeyboardInput.js';
- export class PlayerController {
- constructor(pawn, camera) {
- this.pawn = pawn;
- this.camera = camera;
- }
- onMouseDown(event) {
- let canvasBounds = canvas.getBoundingClientRect();
- let touchPoint = {
- x: event.clientX + this.camera.x - canvasBounds.width / 2,
- y: event.clientY + this.camera.y - canvasBounds.height / 2
- };
- let angle = this.getAngleFromPawn(touchPoint);
- if (event.buttons == 1 && event.button == 0) {
- this.pawn.thrustDirection(angle);
- }
- this.pawn.setAngle(angle);
- }
- onMouseMove(event) {
- let canvasBounds = canvas.getBoundingClientRect();
- let touchPoint = {
- x: event.clientX + this.camera.x - canvasBounds.width / 2,
- y: event.clientY + this.camera.y - canvasBounds.height / 2
- };
- let angle = this.getAngleFromPawn(touchPoint);
- if (event.buttons == 1 && event.button == 0) {
- this.pawn.thrustDirection(angle);
- }
- this.pawn.setAngle(angle);
- }
- onMouseUp(event) {
- this.pawn.stopThrust();
- }
- onTouchStart(event) {
- let canvasBounds = canvas.getBoundingClientRect();
- let touchPoint = {
- x: event.touches[0].clientX + this.camera.x - canvasBounds.width / 2,
- y: event.touches[0].clientY + this.camera.y - canvasBounds.height / 2
- };
- let angle = this.getAngleFromPawn(touchPoint);
- this.pawn.thrustDirection(angle);
- }
- onTouchMove(event) {
- let canvasBounds = canvas.getBoundingClientRect();
- let touchPoint = {
- x: event.touches[0].clientX + this.camera.x - canvasBounds.width / 2,
- y: event.touches[0].clientY + this.camera.y - canvasBounds.height / 2
- };
- let angle = this.getAngleFromPawn(touchPoint);
- this.pawn.thrustDirection(angle);
- }
- onTouchEnd(event) {
- event.preventDefault();
- this.pawn.stopThrust();
- }
- getAngleFromPawn(touchPoint) {
- return Math.atan2(touchPoint.y - this.pawn.position.y, touchPoint.x - this.pawn.position.x);
- }
- }
|