62 lines
2 KiB
JavaScript
62 lines
2 KiB
JavaScript
import { Cube } from "./Cube.js";
|
|
import { Rotation } from "./utils.js";
|
|
|
|
export class Player extends Cube {
|
|
constructor(color) {
|
|
super(color, "assets/images/player.png");
|
|
|
|
this.data.position.x = -2;
|
|
this.data.rotation.x = this.gameRotation;
|
|
|
|
this.movementData = new Rotation(this.data.position);
|
|
}
|
|
|
|
animation = () => {
|
|
// If we jump
|
|
if (this.movementData.state) {
|
|
// Rotation
|
|
this.data.rotation.y -= this.movementData.rotationVelocity;
|
|
|
|
// If we're falling (2nd part of the jump)
|
|
if (this.movementData.falling()) {
|
|
// Gravity!
|
|
this.data.position.y -= this.movementData.jumpVelocity;
|
|
} else {
|
|
// If we're jumping (1st part of the jump) : jumping
|
|
this.data.position.y += this.movementData.jumpVelocity;
|
|
|
|
// Check if the jump stop and we need to goes down
|
|
if (this.data.position.y >= this.movementData.jump) {
|
|
this.movementData.changeJump(this.movementData.default.y);
|
|
}
|
|
}
|
|
|
|
// End of the rotation
|
|
if (this.data.rotation.y <= this.movementData.rotation) {
|
|
// Force the final rotation
|
|
this.data.rotation.y = this.movementData.rotation;
|
|
|
|
// Force the end of the jump
|
|
this.data.position.y = this.movementData.default.y;
|
|
|
|
// End
|
|
this.movementData.changeState();
|
|
}
|
|
}
|
|
};
|
|
|
|
controlUser = (key) => {
|
|
const now = Date.now();
|
|
if (
|
|
key.code == "Space" &&
|
|
!this.movementData.state &&
|
|
now - this.movementData.lastJump > 300
|
|
) {
|
|
this.movementData.changeRotation(
|
|
this.data.rotation.y - Math.PI / 2
|
|
);
|
|
this.movementData.changeJump(this.data.position.y + Math.PI / 2);
|
|
this.movementData.changeState(now);
|
|
}
|
|
};
|
|
}
|