cirnogodot/Scripts/Weapons/Weapon3D.cs

228 lines
5.3 KiB
C#
Raw Normal View History

2025-06-17 11:57:59 +02:00
using Cirno.Scripts.Controllers;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
using Godot;
namespace Cirno.Scripts.Weapons;
2025-06-18 18:09:30 +02:00
public partial class Weapon3D : Node3D
2025-06-17 11:57:59 +02:00
{
[Export]
public WeaponResource WeaponData { get; set; }
[Export]
public PackedScene BulletScene { get; set; }
[Export]
public Marker3D Muzzle { get; set; }
[Export]
public Marker3D Pivot { get; set; }
[Export]
public Sprite3D Sprite { get; private set; }
[Export] public StringName PowerKey { get; set; } = "POWER";
[Signal]
public delegate void ShootingEventHandler();
[Signal]
public delegate void ReloadingEventHandler();
[Signal] public delegate void EmptyEventHandler();
public int Ammo { get; set; } = 0;
private int _loadedAmmo;
public int LoadedAmmo
{
get => _loadedAmmo;
private set
{
_loadedAmmo = value;
_inventoryManager?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
}
}
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
private Timer _cooldownTimer;
private GameManager _gameManager;
private InventoryManager _inventoryManager;
private readonly StringName _shieldAmmoType = "SHIELD";
private bool UsesBattery => WeaponData.AmmoKey == _shieldAmmoType;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_cooldownTimer = GetNode<Timer>("./ShootTimer");
// Start full
if (WeaponData != null)
{
LoadedAmmo = WeaponData.BulletCapacity;
}
}
public void Reload()
{
EmitSignalReloading();
_cooldownTimer.Start(WeaponData.ReloadTime);
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
{
LoadedAmmo = WeaponData.BulletCapacity;
}
else
{
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity - LoadedAmmo);
if (ammoToLoad > 0)
{
LoadedAmmo = ammoToLoad;
}
else
{
EmitSignalEmpty();
//GD.Print("Out of ammo");
}
}
}
public void Shoot(BulletOwner? ownerOverride = null)
{
// Waiting on reload or Rate of Fire cooldown?
if (!_cooldownTimer.IsStopped())
{
return;
}
// Check for battery if it's used
2025-06-18 18:09:30 +02:00
// if (UsesBattery)
// {
// if (GameManager.Instance.Player.Shield.CurrentResource >= WeaponData.AmmoPerShot)
// {
// GameManager.Instance.Player.Shield.CurrentResource -= WeaponData.AmmoPerShot;
// }
// else
// {
// EmitSignalEmpty();
// return;
// }
// }
2025-06-17 11:57:59 +02:00
// Out of ammo?
if (LoadedAmmo < WeaponData.AmmoPerShot)
{
if (WeaponData.AutoReload)
{
Reload();
}
EmitSignalEmpty();
return;
}
EmitSignalShooting();
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
float halfSpread = WeaponData.SpreadAngle / 2f;
float spreadStep = WeaponData.BulletsPerShot > 1 ? WeaponData.SpreadAngle / (WeaponData.BulletsPerShot - 1) : 0;
for (int i = 0; i < WeaponData.BulletsPerShot; i++)
{
// Calculate angle offset for this bullet
float spreadOffset = -halfSpread + (spreadStep * i);
// Add random spread
if (WeaponData.RandomSpread > 0)
{
// Gaussian with mean = 0, stddev = WeaponData.RandomSpread
spreadOffset += RandomStuff.GaussianClamped(
mean: 0f,
stdDev: WeaponData.RandomSpread, // tuning knob
min: -halfSpread,
max: halfSpread
);
}
// Rotate the ShootDirection by the spread angle
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
// Restore pooling
var bullet = PoolingManager.Instance.SpawnBullet<Bullet3D>(WeaponData.BulletData);
//var bullet = WeaponData.BulletData.BulletScene.Instantiate<Bullet3D>()
bullet.GlobalPosition = Muzzle.GlobalPosition;
//var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletData.BulletScene, _muzzle.GlobalPosition);
if (bullet == null)
{
GD.PrintErr("Bullet is null, not shooting");
return;
};
var bulletData = WeaponData.MakeBullet(Muzzle.GlobalPosition.ToVector2()); // TODO: Fix for 3D
if (ownerOverride.HasValue)
{
bulletData.Owner = ownerOverride.Value;
}
if (bulletData.Owner is BulletOwner.Player || ownerOverride is BulletOwner.Player)
{
// Apply the P multiplier
bulletData.Damage *=
GetBulletStrengthMultiplier(bulletData.Damage, bulletData.OriginalBulletResource.MaxDamage, 20);
}
2025-06-19 17:55:23 +02:00
bullet.Initialize(bulletData);
2025-06-17 11:57:59 +02:00
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);
bullet.Speed = WeaponData.BulletData.BulletSpeed;
}
if (!UsesBattery)
{
LoadedAmmo -= WeaponData.AmmoPerShot;
}
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
// {
// // Notify hud to decrease weapon
//
// }
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
}
private float GetBulletStrengthMultiplier(float baseDamage, float maxDamage, float maxPower)
{
var p = InventoryManager.Instance.GetItemCount(PowerKey);
float minMultiplier = 1.0f;
float maxMultiplier = maxDamage / baseDamage;
float normalizedPower = Mathf.Clamp((float)p / maxPower, 0f, 1f);
return Mathf.Lerp(minMultiplier, maxMultiplier, normalizedPower);
}
public void Hide()
{
}
public void Show()
{
}
}