-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleCollisionUtils.js
More file actions
133 lines (110 loc) · 4.05 KB
/
VehicleCollisionUtils.js
File metadata and controls
133 lines (110 loc) · 4.05 KB
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
import { Health } from '../../../components/Health.js';
export function handleVehicleDestruction(state, vehicle) {
if (!vehicle.health || vehicle.health.isAlive()) return;
if (state.explosionSystem) {
state.explosionSystem.createExplosion(state, vehicle.pos);
}
if (state.scoringSystem) {
state.scoringSystem.addCrime(state, 'destroy_vehicle', vehicle);
}
const vehicleIndex = state.entities.indexOf(vehicle);
if (vehicleIndex > -1) {
state.entities.splice(vehicleIndex, 1);
}
if (state.control?.vehicle === vehicle) {
const deathSystem = state._engine?.systems?.death ||
state.deathSystem ||
state._engine?.deathSystem;
if (deathSystem && deathSystem.handlePlayerDeath) {
deathSystem.handlePlayerDeath(state);
}
}
}
export function addDamageIndicator(state, pos, damage) {
if (!state.damageTexts) state.damageTexts = [];
const actualDamage = damage || 0;
const indicator = {
type: 'damage_indicator',
pos: { x: pos.x, y: pos.y },
text: `-${actualDamage}`,
color: '#ff3333',
age: 0,
lifetime: 1.5,
size: 14
};
state.damageTexts.push(indicator);
}
export function smoothCollisionNormal(normal, objA, objB) {
const centerToCenter = {
x: objB.pos.x - objA.pos.x,
y: objB.pos.y - objA.pos.y
};
const len = Math.hypot(centerToCenter.x, centerToCenter.y);
if (len > 0) {
centerToCenter.x /= len;
centerToCenter.y /= len;
}
const blendFactor = 0.8;
return {
x: normal.x * blendFactor + centerToCenter.x * (1 - blendFactor),
y: normal.y * blendFactor + centerToCenter.y * (1 - blendFactor)
};
}
export function applyCollisionDamping(objA, objB) {
if (typeof objA.angularVelocity === 'number') {
objA.angularVelocity *= 0.7;
}
if (typeof objB.angularVelocity === 'number') {
objB.angularVelocity *= 0.7;
}
let dampingFactor = 0.85;
if (objB && objA?.vel && objB?.vel) {
const rel = Math.hypot((objA.vel.x||0)-(objB.vel.x||0),(objA.vel.y||0)-(objB.vel.y||0));
const a = (x)=>((x%(2*Math.PI))+2*Math.PI)%(2*Math.PI);
const hDiff = (objA.rot!=null && objB.rot!=null) ? Math.abs(((a(objA.rot)-a(objB.rot)+Math.PI)%(2*Math.PI))-Math.PI) : Math.PI;
if (rel < 0.6 && hDiff < 0.5) dampingFactor = 0.97;
}
if (objA.vel) {
objA.vel.x *= dampingFactor;
objA.vel.y *= dampingFactor;
}
if (objB.vel) {
objB.vel.x *= dampingFactor;
objB.vel.y *= dampingFactor;
}
}
export function getVelocityDirection(entity) {
const speed = Math.hypot(entity.vel.x, entity.vel.y);
if (speed < 0.01) return { x: 0, y: 0 };
return { x: entity.vel.x / speed, y: entity.vel.y / speed };
}
export function calculateBounceNormal(velocityDir, contactNormal) {
if (velocityDir.x === 0 && velocityDir.y === 0) {
return contactNormal;
}
const dot = velocityDir.x * contactNormal.x + velocityDir.y * contactNormal.y;
const reflected = {
x: velocityDir.x - 2 * dot * contactNormal.x,
y: velocityDir.y - 2 * dot * contactNormal.y
};
const length = Math.hypot(reflected.x, reflected.y);
if (length < 0.01) return contactNormal;
return { x: reflected.x / length, y: reflected.y / length };
}
export function applyImpactDamage(state, v, damageMultiplier) {
const now = Date.now();
const canDamage = now - (v.lastDamageTime || 0) >= this.system.damageCooldown;
if (canDamage) {
const impactSpeed = Math.hypot(v.vel?.x || 0, v.vel?.y || 0);
if (impactSpeed > this.system.collisionDamageThreshold) {
if (!v.health) v.health = new Health(v.maxHealth || 100);
const damage = Math.max(1, Math.round(impactSpeed * damageMultiplier));
v.health.takeDamage(damage);
v.lastDamageTime = now;
const impactSound = ['impact02', 'impact03'][Math.floor(Math.random() * 2)];
state.audio?.playSfxAt?.(impactSound, v.pos, state, { volume: 0.3 });
handleVehicleDestruction(state, v);
addDamageIndicator(state, v.pos, damage);
}
}
}