cirnogodot/Scripts/Actors/BulletSpawner3D.cs

70 lines
2.5 KiB
C#
Raw Normal View History

2025-06-20 15:39:19 +02:00
using Cirno.Scripts.Components;
using Cirno.Scripts.Controllers;
using Cirno.Scripts.Weapons;
using Godot;
namespace Cirno.Scripts.Actors;
public partial class BulletSpawner3D : Node3D
{
public void SpawnBullet(BulletInfo bulletInfo, Vector3 position)
{
int count = bulletInfo.BulletCount;
2025-06-21 10:42:11 +02:00
// Choose base direction (defaults to +X)
Vector2 baseDirection = bulletInfo.Direction == Vector2.Zero
? Vector2.Right
: bulletInfo.Direction.Normalized();
float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X); // angle in radians
float offsetRadians = Mathf.DegToRad(bulletInfo.RotationOffset);
// Calculate per-bullet spread angle step
2025-06-20 15:39:19 +02:00
float spreadRadians = Mathf.DegToRad(bulletInfo.Spread);
2025-06-21 10:42:11 +02:00
// 👇 Configure this depending on desired pattern
bool isFullCircle = Mathf.Abs(bulletInfo.Spread - 360f) < 0.01f;
float angleStep = isFullCircle
? spreadRadians / count // Full circle: divide evenly across bullets
: count > 1
? spreadRadians / (count - 1) // Fan spread: divide across gaps
: 0f;
// Determine starting angle for bullet pattern
float startAngle = isFullCircle
? baseAngle + offsetRadians // Full circle starts at baseAngle + offset
: baseAngle + offsetRadians - (spreadRadians / 2.0f); // Fan starts centered
// Spawn bullets
2025-06-20 15:39:19 +02:00
for (int i = 0; i < count; i++)
{
2025-06-21 10:42:11 +02:00
Bullet3D bullet = PoolingManager.Instance.SpawnBullet<Bullet3D>(bulletInfo.OriginalBulletResource);
2025-06-20 15:39:19 +02:00
bullet.GlobalPosition = position;
2025-06-21 10:42:11 +02:00
// Optionally apply modifiers (null-safe)
2025-06-20 15:39:19 +02:00
if (bulletInfo.Modifier is not null)
{
bulletInfo = bulletInfo.Modifier.ModifyBullet(bulletInfo, i, count);
}
2025-06-21 10:42:11 +02:00
2025-06-20 15:39:19 +02:00
bullet.Initialize(bulletInfo);
2025-06-21 10:42:11 +02:00
// Compute final angle for this bullet
float angle = startAngle + angleStep * i;
2025-06-20 15:39:19 +02:00
2025-06-21 10:42:11 +02:00
// Optional: Tiny jitter to avoid precision collapse
// angle += i * 0.0001f;
2025-06-20 15:39:19 +02:00
2025-06-21 10:42:11 +02:00
Vector2 bulletDirection = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)).Normalized();
2025-06-20 15:39:19 +02:00
bullet.SetDirection(bulletDirection);
2025-06-21 10:42:11 +02:00
// Optional: Debug offset to visualize all bullets
// bullet.GlobalPosition += new Vector3(i * 0.1f, 0, 0);
2025-06-21 18:54:14 +02:00
//GD.Print($"Bullet {i}: Angle={Mathf.RadToDeg(angle)}, Direction={bulletDirection}");
2025-06-20 15:39:19 +02:00
}
}
}