import { game } from '..';
import Entity from './Entity'
import { Smooth } from '../Utils/EasingFunctions'
...
export default class Player extends Entity {
constructor(x, y, options) {
...
this.goalRotation = 0;
}
}
update(){
this.handleControls();
this.rotation = Smooth(this.rotation, this.goalRotation, 8);
}
handleControls(){
if(game.isTouching){
this.pos.x = Smooth(this.pos.x, game.mouseX, 13);
this.keepInsideScreen();
const isTouchingFarEnough = Math.abs(this.pos.x - game.mouseX) > this.size / 2;
if (isTouchingFarEnough) {
const movingDirection = Math.sign(game.mouseX - this.pos.x);
this.goalRotation = movingDirection * game.radians(15);
}else{
this.goalRotation = 0;
}
}else{
this.goalRotation = 0;
}
}
keepInsideScreen() {
const limitLeft = this.size / 2;
const limitRight = game.width - this.size / 2;
this.pos.x = game.constrain(this.pos.x, limitLeft, limitRight);
}