69 lines
2.4 KiB
JavaScript
69 lines
2.4 KiB
JavaScript
|
|
import * as THREE from 'three';
|
|
|
|
export class Player {
|
|
constructor(scene, color, position, type, team) {
|
|
this.scene = scene;
|
|
this.color = color;
|
|
this.velocity = new THREE.Vector3(0, 0, 0);
|
|
this.type = type;
|
|
this.team = team;
|
|
this.radius = 1.5;
|
|
this.hasBall = false;
|
|
this.createPlayer(new THREE.Vector3(position.x, position.y, position.z));
|
|
}
|
|
|
|
createPlayer(initialPosition) {
|
|
const playerGeometry = new THREE.CapsuleGeometry(1, 2, 4, 8);
|
|
const playerMaterial = new THREE.MeshStandardMaterial({ color: this.color });
|
|
this.mesh = new THREE.Mesh(playerGeometry, playerMaterial);
|
|
this.mesh.position.copy(initialPosition);
|
|
this.mesh.initialPosition = initialPosition.clone();
|
|
this.mesh.castShadow = true;
|
|
this.mesh.lastVelocity = new THREE.Vector3();
|
|
this.mesh.team = this.team;
|
|
this.scene.add(this.mesh);
|
|
}
|
|
|
|
shoot(ball) {
|
|
if (ball.mesh.possessedBy === this.mesh) {
|
|
ball.mesh.possessedBy = null;
|
|
const shootPower = 1.5;
|
|
ball.mesh.velocity.copy(this.mesh.lastVelocity).normalize().multiplyScalar(shootPower);
|
|
}
|
|
}
|
|
|
|
pass(ball, teammates) {
|
|
if (ball.mesh.possessedBy === this.mesh) {
|
|
ball.mesh.possessedBy = null;
|
|
const passPower = 0.8;
|
|
const passAngle = Math.PI / 6; // 30 degrees
|
|
|
|
let targetTeammate = null;
|
|
let minAngle = passAngle;
|
|
|
|
const playerDirection = this.mesh.lastVelocity.clone().normalize();
|
|
|
|
for (const teammate of teammates) {
|
|
const toTeammate = teammate.mesh.position.clone().sub(this.mesh.position);
|
|
const distanceToTeammate = toTeammate.length();
|
|
toTeammate.normalize();
|
|
|
|
const angle = playerDirection.angleTo(toTeammate);
|
|
|
|
if (angle < minAngle && distanceToTeammate < 40) { // Max pass distance
|
|
minAngle = angle;
|
|
targetTeammate = teammate;
|
|
}
|
|
}
|
|
|
|
if (targetTeammate) {
|
|
const direction = targetTeammate.mesh.position.clone().sub(this.mesh.position).normalize();
|
|
ball.mesh.velocity.copy(direction).multiplyScalar(passPower);
|
|
} else {
|
|
ball.mesh.velocity.copy(playerDirection).multiplyScalar(passPower);
|
|
}
|
|
}
|
|
}
|
|
}
|