-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmechanics.js
166 lines (144 loc) · 5.61 KB
/
mechanics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// player mechanics.js
// Contains Player Movement, Gun and Bullet mechanics
import {getHeightAtPosition} from './groundMechanics.js';
export function updateMovement(ego, keysPressed, yaw, bullets, bulletSpeed, gravity, groundY, deltaTime, absGround) {
console.log(`updateMovement: isJumping=${ego.isJumping}, isFreeFalling=${ego.isFreeFalling}, onGround=${ego.onGround}`);
const pace = keysPressed['shift'] ? 133 : 90;
const cosYaw = Math.cos(yaw);
const sinYaw = Math.sin(yaw);
if (keysPressed['d']) {
ego.x += pace * cosYaw;
ego.z += pace * sinYaw;
}
if (keysPressed['a']) {
ego.x -= pace * cosYaw;
ego.z -= pace * sinYaw;
}
if (keysPressed['w']) {
ego.x += pace * sinYaw;
ego.z -= pace * cosYaw;
}
if (keysPressed['s']) {
ego.x -= pace * sinYaw;
ego.z += pace * cosYaw;
}
// Update vertical movement and ground logic
if (ego.z < 0) {
groundY = getHeightAtPosition(ego.x, ego.z, ego.y + 1900, absGround);
console.log(`groundY: ${groundY}`);
if (!isNaN(groundY)) {
// Adjust player's vertical position if below the ground
if (ego.y + 1900 > groundY) {
ego.y = groundY - 1900;
ego.velocityY = 0; // Reset velocity when landing
ego.isJumping = false;
ego.isFreeFalling = false;
ego.onGround = true;
console.log("Player landed on the ground");
}
}
}
// Check if the player is in free-fall or on the ground
if (groundY - (ego.y + 1900) > 0) {
ego.onGround = false;
console.log(`Player above ground : ${groundY - (ego.y + 1900)} mm`);
if (!ego.isJumping) ego.isFreeFalling = true; // Only free-fall if not jumping
} else {
ego.onGround = true;
ego.isFreeFalling = false; // Stop free-fall when on the ground
}
// Handle jumping and free-fall mechanics
if (ego.isJumping) {
console.log("Calling jump function");
jump(ego, gravity, deltaTime);
} else if (ego.isFreeFalling) {
console.log("Calling freeFall function");
freeFall(ego, gravity, groundY, deltaTime);
}
updateBullets(bullets, ego, bulletSpeed);
}
export function updateBullets(bullets, ego, bulletSpeed) {
bullets.forEach((bullet, index) => {
bullet.position.x += bullet.direction.x * bulletSpeed;
bullet.position.y += bullet.direction.y * bulletSpeed;
bullet.position.z += bullet.direction.z * bulletSpeed;
const maxDistance = 50000;
if (
Math.abs(bullet.position.x - ego.x) > maxDistance ||
Math.abs(bullet.position.y - ego.y) > maxDistance ||
Math.abs(bullet.position.z - ego.z) > maxDistance
) {
bullets.splice(index, 1);
}
});
}
export function resetMovement() {
pace = 0;
}
export function initiateJump(ego, jumpHeight, gravity) {
console.log(`ego is jumping? : ${ego.isJumping}`);
if (ego.isJumping) {
console.log("Jump initiation skipped: Already jumping or free-falling");
return;
};
ego.isJumping = true;
ego.isFreeFalling = false;
ego.velocityY = Math.sqrt(-2 * gravity * jumpHeight);
// console.log(`Jump initiated: velocityY=${ego.velocityY}, isJumping=${ego.isJumping}`);
}
export function jump(ego, gravity, deltaTime) {
if ( ego.isFreeFalling === false) {
ego.velocityY += gravity * deltaTime; // Gravity reduces upward velocity
ego.y -= ego.velocityY * deltaTime; // Move up based on velocity
// console.log(`Updated jump position: velocityY=${ego.velocityY}, ego.y=${ego.y}`);
}
}
export function freeFall(ego, gravity, groundY, deltaTime) {
// Continue falling if above ground
if (ego.onGround === false) {
ego.velocityY += gravity * deltaTime;
ego.y -= ego.velocityY * deltaTime;
console.log(`Updated free-fall position: velocityY=${ego.velocityY}, ego.y=${ego.y}`);
} else { //if overshot to go below ground
// console.log("Already on ground");
ego.y = groundY - 1900; // Land on the ground
ego.velocityY = 0;
}
}
export function shoot(isCharged, ego, bullets, bullet, yaw, pitch, chargedBulletScale) {
const scale = isCharged ? chargedBulletScale : 1;
const gunBarrelOffset = vec3.fromValues(300, 320, 0);
const startPosition = vec3.create();
const rotationMatrix = mat4.create();
mat4.rotateY(rotationMatrix, rotationMatrix, -yaw);
mat4.rotateX(rotationMatrix, rotationMatrix, -pitch);
vec3.transformMat4(startPosition, gunBarrelOffset, rotationMatrix);
vec3.add(startPosition, startPosition, [ego.x, ego.y, ego.z]);
const direction = vec3.fromValues(0, 0, -1);
vec3.transformMat4(direction, direction, rotationMatrix);
vec3.normalize(direction, direction);
const scaledBulletShape = bullet.map(point => ({
x: point.x * scale,
y: point.y * scale,
z: point.z * scale
}));
bullets.push({
shape: JSON.parse(JSON.stringify(scaledBulletShape)),
position: { x: startPosition[0], y: startPosition[1], z: startPosition[2] },
direction: { x: direction[0], y: direction[1], z: direction[2] }
});
}
export function createPlayerHitbox(ego, dimensions) {
return {
min: {
x: ego.x - dimensions.width / 2,
y: ego.y + dimensions.height - 100,
z: ego.z - dimensions.depth / 2,
},
max: {
x: ego.x + dimensions.width / 2,
y: ego.y - 300,
z: ego.z + dimensions.depth / 2,
}
};
}