This repository has been archived on 2022-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
GeometryDash3D/js/Player.js

125 lines
4 KiB
JavaScript
Raw Normal View History

import { Cube } from "./Cube.js";
import { Rotation } from "./utils.js";
2022-12-02 16:32:52 +01:00
import { jumpDemo } from "./Game.js";
2022-11-24 18:58:45 +01:00
export class Player extends Cube {
constructor(color) {
2022-11-27 17:20:49 +01:00
super(color, "assets/images/player.jpg");
2022-11-24 17:56:26 +01:00
this.data.position.x = -2;
this.data.rotation.x = this.gameRotation;
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;
if (pos < Math.random() * 2.8 + 0.2 && pos > 0) {
dispatchEvent(jumpDemo);
}
});
}
// 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();
}
}
2022-11-27 18:09:56 +01:00
return !this.checkCollision(listEnnemies);
};
2022-11-27 18:09:56 +01:00
/**
* Handle player's input
* @param {string} key key pressed
*/
controlUser = (key) => {
2022-12-02 14:29:12 +01:00
if (key.code == "Space" && !this.movementData.state) {
this.movementData.changeRotation(this.data.rotation.y - Math.PI);
this.movementData.changeJump(this.data.position.y + Math.PI);
this.movementData.changeState();
}
};
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
const ray = new THREE.Raycaster(
this.data.position,
directionVector.clone().normalize()
);
// Check if the raycaster hit ennemies
const collisionResults = ray.intersectObjects(listEnnemies, false);
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;
};
}