/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Ball.js :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: edbernar diffTop) speed *= -1; if (this.interval) clearInterval(this.interval); this.interval = setInterval(() => { this.object.position.y += speed; if ((speed > 0 && this.object.position.y >= this.limits.up) || (speed < 0 && this.object.position.y <= this.limits.down)) { clearInterval(this.interval); this.interval = null; if (speed > 0) this.setPosition(this.object.position.x, this.limits.up, this.object.position.z); else this.setPosition(this.object.position.x, this.limits.down, this.object.position.z); ballIsOnJumper.can = true; } speed -= speed * slower; }, 10); } /*---------------- FUNCTION FOR TEST ----------------*/ initMoveBallTmp() { console.warn("Don't forget to remove function initMoveBallTmp"); const speedBallTmp = 0.1; let warn = false; document.addEventListener('keypress', (e) => { if (!this.object && !warn) { console.warn("EventListener in initMoveBallTmp() is still here"); warn = true; return ; } if (e.key == '4') this.object.position.x -= speedBallTmp; if (e.key == '6') this.object.position.x += speedBallTmp; if (e.key == '8') this.object.position.z -= speedBallTmp; if (e.key == '2') this.object.position.z += speedBallTmp; if (e.key == '9') this.changeGravity(); }); } /*---------------------------------------------------*/ updatePos(content) { // {action: 5, pos: [ball.object.position.x, ball.object.position.z], velocity: [ball.object.velocity.x, ball.object.velocity.z]} this.lastTime = new Date().getTime(); // this.ballBody.position.x = content.pos[0]; // this.ballBody.position.z = content.pos[1]; this.lastPos = [content.pos[0], this.object.position.y, content.pos[1]]; this.velocity = content.velocity; // this.ballBody.velocity.set(content.velocity[0], 0, content.velocity[1]); } update() { const deltaTime = ((new Date().getTime()) - this.lastTime) / 1000; const x = this.lastPos[0] + (deltaTime * this.velocity[0]); const z = this.lastPos[2] + (deltaTime * this.velocity[1]); this.object.position.set(x, this.object.position.y, z); // this.object.position.copy(this.ballBody.position); // this.world.step(timeStep); } dispose() { if (this.interval) clearInterval(this.interval); this.interval = null; if (this.object) { if (this.object.geometry) this.object.geometry.dispose(); if (this.object.material) this.object.material.dispose(); if (this.object.texture) this.object.texture.dispose(); } this.object = null; } } export { Ball };