Projectile Component
ProjectileComponent marks an entity as a projectile and stores its damage value and the entity that fired it.
ProjectileComponent
struct ProjectileComponent: Component {
var damage: Float
var owner: Entity
}
| Property | Type | Description |
|---|---|---|
damage | Float | Hit-point damage dealt on impact |
owner | Entity | The entity that fired this projectile (used to avoid self-damage) |
Creating a Projectile Entity
Projectiles are created via EntityFactory.makeProjectile:
EntityFactory.makeProjectile(
from: ownerTransform.position,
aimAt: fireDirection,
speed: speedWhenFired,
owner: ownerEntity,
in: world
)
These components attache to the new entity (the projectile):
| Component | Value |
|---|---|
TransformComponent | Spawns at the given position; rotation derived from direction |
VelocityComponent | direction * speed |
SpriteComponent | "normalHandgunBullet", z-position 5 |
ProjectileComponent | damage in factory method: 10, owner: ownerEntity |
EffectiveRangeComponent | base in factory method: 400 units |
CollisionBoxComponent | 6 × 6 point hitbox |
Lifetime
A projectile is destroyed when either:
- Range is exhausted —
EffectiveRangeComponent.value.currentreaches 0 (base range: 400 units). - It hits a solid — the
CollisionEventBufferemits aprojectileHitSolidevent for it.
Both cases enqueue the entity in DestructionQueue, which flushes at the end of ProjectileSystem.update.
In future iterations, we want to add a third case for hitting a damageable entity (e.g. an enemy), but currently projectiles pass through enemies without collision.
ProjectileSystem
ProjectileSystem runs at priority: 60 — after WeaponSystem (priority 50) so any newly spawned projectiles are already in the world when movement is applied.
Each frame it:
- Moves every projectile by
velocity × deltaTime. - Decrements
EffectiveRangeComponentby the distance traveled; enqueues expired projectiles. - Reads
CollisionEventBuffer.projectileHitSolidand enqueues all hit projectiles. - Flushes
DestructionQueue.
Dependencies
| Dependency | Role |
|---|---|
VelocityComponent | Stores the projectile's linear velocity vector |
EffectiveRangeComponent | Tracks remaining travel distance before auto-destruction |
CollisionBoxComponent | Defines the hitbox for wall/solid collision detection |
CollisionEventBuffer | Source of projectileHitSolid events |
DestructionQueue | Deferred entity removal to avoid mutating the world mid-iteration |
EntityFactory | Factory method for creating projectile entities |