Skip to main content

ProjectileEntityFactory

ProjectileEntityFactory creates a single projectile entity. It is called by WeaponSystem each time the player fires.

Usage

ProjectileEntityFactory(
from: position,
aimAt: direction,
speed: 400,
effectiveRange: 300,
owner: playerEntity
).make(in: world)

Parameters

ParameterTypeDescription
positionSIMD2<Float>Spawn position (typically the weapon's current position)
directionSIMD2<Float>Normalised aim vector
speedFloatInitial speed (applied as direction × speed to VelocityComponent)
effectiveRangeFloatMax distance the projectile travels before being destroyed
ownerEntityThe entity that fired the projectile (used to avoid self-collision)

Components Added

ComponentInitial Value
TransformComponentposition, rotation derived from direction, scale 1
VelocityComponentdirection × speed
SpriteComponent"normalHandgunBullet", layer .projectile
ProjectileComponentDamage 10, owner
EffectiveRangeComponentbase: effectiveRange
CollisionBoxComponent6 × 6

Rotation

The bullet's rotation is computed from the aim direction so the sprite points in the direction of travel:

  • Travelling right (direction.x >= 0): atan2(direction.y, direction.x)
  • Travelling left: -atan2(direction.y, -direction.x)