protected override void OnUpdate() {
var deltaTime = Time.DeltaTime;
var ecb = _ecbSystem.CreateCommandBuffer().AsParallelWriter();
Entities.WithAll<SpaceshipTag>().ForEach(
(int nativeThreadIndex, ref ShootingComponent shooting, in TargetingComponent target,
in Translation pos, in MovementComponent movement, in TeamComponent team) => {
shooting.SecondsFromLastShot += deltaTime; // Increase shooting timer
// Decide if it is the right moment to shoot
if (!target.TargetLocked || shooting.SecondsFromLastShot < shooting.SecondsBetweenShots ||
!ShouldShoot(movement.Heading, pos.Value, target.TargetPosition, shooting.AimAngle))
return;
shooting.SecondsFromLastShot = 0; // Reset shooting timer
// Instantiate a new projectile and set its components
var projectileEntity = ecb.Instantiate(nativeThreadIndex, shooting.Prefab);
ecb.SetComponent(nativeThreadIndex, projectileEntity, new Translation {Value = pos.Value});
ecb.SetComponent(nativeThreadIndex, projectileEntity,
new MovementComponent {Heading = movement.Heading, MaxSpeed = shooting.ProjectileSpeed});
ecb.SetComponent(nativeThreadIndex, projectileEntity,
new TeamComponent {Team = team.Team, TeamColor = team.TeamColor});
}).ScheduleParallel();