All files / physics hitbox.js

0% Statements 0/23
0% Branches 0/3
0% Functions 0/4
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                     
import { Vector3 } from 'three';
import Box from '../entities/base/Box';
import Sphere from '../entities/base/Sphere';
import { COLLIDER_TYPES } from './constants';
 
const HIT_BOX_COLOR = 0xf368e0;
const HIT_BOX_INCREASE = .03;
 
const DEFAULT_HITBOX_OPTIONS = {
    shadowsEnabled: false
};
 
export const getBoxHitbox = element => {
    const size = new Vector3();
    element.boundingBox.getSize(size);
 
    const scaledSize = {
        x: size.x + HIT_BOX_INCREASE,
        y: size.y + HIT_BOX_INCREASE,
        z: size.z + HIT_BOX_INCREASE
    };
    const box = new Box(scaledSize.x, scaledSize.y, scaledSize.z, HIT_BOX_COLOR, DEFAULT_HITBOX_OPTIONS);
 
    //box.setQuaternion(quaternion);
    box.setWireframe(true);
    box.setWireframeLineWidth(2);
 
    return box;
}
 
export const getSphereHitbox = element => {
    const radius = element.boundingSphere.radius;
    const sphere = new Sphere(radius, HIT_BOX_COLOR, DEFAULT_HITBOX_OPTIONS);
 
    sphere.setWireframe(true);
    sphere.setWireframeLineWidth(2);
 
    return sphere;
};
 
export const mapColliderTypeToHitbox = (colliderType = COLLIDER_TYPES.BOX) => ({
    [COLLIDER_TYPES.BOX]: getBoxHitbox,
    [COLLIDER_TYPES.SPHERE]: getSphereHitbox
}[colliderType] || getBoxHitbox);
 
export const addHitBox = element => {
    const colliderType = element.getPhysicsOptions('colliderType');
    const getHitbox = mapColliderTypeToHitbox(colliderType)
 
    element.add(getHitbox(element));
};