import { Cube } from "./Cube.js"; class Rotation { constructor(position) { this.default = position.clone(); this.state = false; this.rotation = 0; this.jump = 0; this.rotationVelocity = 0.05; this.jumpVelocity = 0.1; this.lastJump = 0; } /** * Change the state of the animation and update the latest jump time * @param {number} time */ changeState = (time) => { this.state = !this.state; if (time) { this.lastJump = time; } }; /** * Change the final rotation * @param {number} rotation */ changeRotation = (rotation) => { this.rotation = rotation; }; /** * Change the final position * @param {number} jump */ changeJump = (jump) => { this.jump = jump; }; /** * Return the state of the jump * @returns boolean representing if the object is going up or down */ falling = () => { return this.jump == this.default.y; }; } export class Joueur extends Cube { constructor(color) { super(color); this.data.position.y = 0.5; 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 > 400 ) { this.movementData.changeRotation( this.data.rotation.y - Math.PI / 2 ); this.movementData.changeJump(this.data.position.y + Math.PI / 2); this.movementData.changeState(now); } }; }