2022-11-24 16:03:01 +01:00
|
|
|
import { Cube } from "./Cube.js";
|
2022-11-24 17:34:00 +01:00
|
|
|
import { Rotation } from "./utils.js";
|
2022-12-02 16:32:52 +01:00
|
|
|
import { jumpDemo } from "./Game.js";
|
2022-11-24 16:03:01 +01:00
|
|
|
|
2022-11-24 18:58:45 +01:00
|
|
|
export class Player extends Cube {
|
2022-11-24 16:03:01 +01:00
|
|
|
constructor(color) {
|
2022-11-27 17:20:49 +01:00
|
|
|
super(color, "assets/images/player.jpg");
|
2022-11-24 16:03:01 +01:00
|
|
|
|
2022-11-24 17:56:26 +01:00
|
|
|
this.data.position.x = -2;
|
2022-11-24 16:03:01 +01:00
|
|
|
this.data.rotation.x = this.gameRotation;
|
2022-11-24 17:34:00 +01:00
|
|
|
|
2022-11-24 16:03:01 +01:00
|
|
|
this.movementData = new Rotation(this.data.position);
|
|
|
|
}
|
|
|
|
|
2022-11-27 18:09:56 +01:00
|
|
|
/**
|
|
|
|
* Play the player animation
|
|
|
|
* @param {*} listEnnemies
|
2022-12-02 16:32:52 +01:00
|
|
|
* @param {boolean} demo
|
2022-11-27 18:09:56 +01:00
|
|
|
* @returns boolean if the player is alive or not
|
|
|
|
*/
|
2022-12-02 16:32:52 +01:00
|
|
|
animation = (listEnnemies, demo) => {
|
|
|
|
if (demo) {
|
|
|
|
listEnnemies.forEach((ennemy) => {
|
|
|
|
const pos = ennemy.position.x - this.data.position.x;
|
2022-12-03 01:09:02 +01:00
|
|
|
if (pos > 0 && pos < Math.random() * 1.5 + 0.5) {
|
2022-12-02 16:32:52 +01:00
|
|
|
dispatchEvent(jumpDemo);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 16:03:01 +01:00
|
|
|
// If we jump
|
|
|
|
if (this.movementData.state) {
|
|
|
|
// Rotation
|
2022-12-02 23:11:25 +01:00
|
|
|
this.data.rotation.y -= this.movementData.rotationVelocity / 1.6;
|
2022-11-24 16:03:01 +01:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
2022-11-27 18:09:56 +01:00
|
|
|
|
|
|
|
return !this.checkCollision(listEnnemies);
|
2022-11-24 16:03:01 +01:00
|
|
|
};
|
|
|
|
|
2022-11-27 18:09:56 +01:00
|
|
|
/**
|
|
|
|
* Handle player's input
|
|
|
|
* @param {string} key key pressed
|
|
|
|
*/
|
2022-11-24 16:03:01 +01:00
|
|
|
controlUser = (key) => {
|
2022-12-02 14:29:12 +01:00
|
|
|
if (key.code == "Space" && !this.movementData.state) {
|
2022-12-02 23:11:25 +01:00
|
|
|
this.movementData.changeRotation(
|
|
|
|
this.data.rotation.y - Math.PI / 2
|
|
|
|
);
|
|
|
|
this.movementData.changeJump(
|
|
|
|
3 * this.data.position.y + Math.PI / 2
|
|
|
|
);
|
2022-12-02 14:29:12 +01:00
|
|
|
this.movementData.changeState();
|
2022-11-24 16:03:01 +01:00
|
|
|
}
|
|
|
|
};
|
2022-11-27 18:09:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check collisions with ennemies
|
|
|
|
* @param {*} listEnnemies
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
|
|
|
checkCollision = (listEnnemies) => {
|
|
|
|
for (
|
|
|
|
let vertexIndex = 0;
|
|
|
|
vertexIndex < this.data.geometry.attributes.position.array.length;
|
|
|
|
vertexIndex++
|
|
|
|
) {
|
|
|
|
// Retrieve the vertex from the player geometry
|
|
|
|
const localVertex = new THREE.Vector3()
|
|
|
|
.fromBufferAttribute(
|
|
|
|
this.data.geometry.attributes.position,
|
|
|
|
vertexIndex
|
|
|
|
)
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
// Apply the player matrix for the vector
|
|
|
|
const globalVertex = localVertex.applyMatrix4(this.data.matrix);
|
|
|
|
|
2022-11-28 20:06:07 +01:00
|
|
|
// Substract the position to obtain the position
|
2022-11-27 18:09:56 +01:00
|
|
|
const directionVector = globalVertex.sub(this.data.position);
|
|
|
|
|
|
|
|
// Create the raycaster for this vector from the player position
|
2022-12-03 01:09:02 +01:00
|
|
|
const rc = new THREE.Raycaster(
|
2022-11-27 18:09:56 +01:00
|
|
|
this.data.position,
|
|
|
|
directionVector.clone().normalize()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check if the raycaster hit ennemies
|
2022-12-03 01:09:02 +01:00
|
|
|
const collisionResults = rc.intersectObjects(listEnnemies, false);
|
2022-11-27 18:09:56 +01:00
|
|
|
if (
|
|
|
|
collisionResults.length > 0 &&
|
|
|
|
directionVector.length() < 1.5 &&
|
|
|
|
collisionResults[0].distance < directionVector.length()
|
|
|
|
) {
|
|
|
|
// Yes some ennemies are in the way
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No ennemies found
|
|
|
|
return false;
|
|
|
|
};
|
2022-11-24 16:03:01 +01:00
|
|
|
}
|