48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using GameCore.Combat.Interfaces;
|
|
using GameCore.Events;
|
|
using GameCore.Input;
|
|
using GameCore.Movement;
|
|
|
|
namespace GameCore.Combat.Effects;
|
|
|
|
public class BulkProjectileEffect(string archetypeId, int count, float spreadAngle, float speed) : IEffect
|
|
{
|
|
private static readonly Random _random = new();
|
|
|
|
public void Execute(EffectContext context)
|
|
{
|
|
var ownerInput = context.World.GetComponent<InputStateComponent>(context.Owner);
|
|
if (ownerInput == null)
|
|
return;
|
|
|
|
var ownerRotation = context.World.GetComponent<RotationComponent>(context.Owner);
|
|
if (ownerRotation == null) return;
|
|
|
|
var spreadRadians = spreadAngle * (float)System.Math.PI / 180f;
|
|
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
var direction = ownerInput.MuzzleDirection;
|
|
|
|
if (spreadRadians > 0f)
|
|
{
|
|
var randomYaw = ((float)_random.NextDouble() * 2f - 1f) * spreadRadians;
|
|
var randomPitch = ((float)_random.NextDouble() * 2f - 1f) * spreadRadians;
|
|
|
|
direction = context.World.WorldQuery.RotateVectorByYaw(direction, randomYaw);
|
|
direction.Y += (float)System.Math.Sin(randomPitch);
|
|
direction = direction.Normalize();
|
|
}
|
|
|
|
var initialVelocity = direction * speed;
|
|
|
|
context.World.PublishEvent(new SpawnEntityEvent(
|
|
archetypeId,
|
|
ownerInput.MuzzlePosition,
|
|
ownerRotation.Rotation,
|
|
context.Owner,
|
|
initialVelocity
|
|
));
|
|
}
|
|
}
|
|
} |