Weapon
new Weapon(views : Object, type : string, index : number, mesh : THREE.Mesh, physics : Physics, properties : Object)Weapon - Base weapon proxy class
Provides a Three.js-style API for weapons, mirroring the Body proxy pattern. Each weapon instance wraps SharedArrayBuffer access and provides event handling through Three.js EventDispatcher pattern.
Constructor Parameters
viewsObjecttypestringindexnumbermeshoptionalTHREE.MeshDefault is
null.propertiesoptionalObjectDefault is
{}.Example
import { Physics, WeaponType, WeaponEvent } from '@three-blocks/pro';
const weapon = await physics.addWeapon(WeaponType.HITSCAN, null, {
damage: 25,
maxDistance: 1000,
});
weapon.weapon.addEventListener(WeaponEvent.HIT, (event) => {
console.log('Hit at:', event.hitPoint);
});
weapon.weapon.fireFromCamera();
Properties
# .damage : number
Get weapon damage value.
# .damage : number
Set weapon damage value.
# .index : number
Get weapon index.
# .type : string
Get weapon type.
# .mesh : THREE.Mesh
Get associated mesh (or first mesh from pool for projectile weapons).
# .meshPool : THREE.InstancedMesh|null
Get mesh pool (for projectile weapons with multiple projectiles). Must be an InstancedMesh created via physics.createMeshPool().
Methods
addEventListener#
addEventListener(type : string, listener : function)Add an event listener.
Parameters
typestringlistenerfunctionremoveEventListener#
removeEventListener(type : string, listener : function)Remove an event listener.
Parameters
typestringlistenerfunctionfire#
fire(shooter : number|Object, options : Object)Fire weapon from body position.
Parameters
shooternumber | ObjectoptionsoptionalObjectDefault is
{}.originoptionalTHREE.Vector3|Array<number>Custom origin as Vector3 or [x, y, z]directionTHREE.Vector3|Array<number>Fire direction as Vector3 or [x, y, z] (required)
fireFromCamera#
fireFromCamera(options : Object)Fire weapon from camera position and direction.
Parameters
optionsoptionalObjectDefault is
{}.dispose#
dispose()Dispose weapon and clean up resources. Removes all event listeners and mesh associations. Called automatically by Physics.removeWeapon().