2025-06-17 11:57:59 +02:00
|
|
|
|
using Cirno.Scripts.Controllers;
|
2025-08-13 16:51:56 +02:00
|
|
|
|
using Cirno.Scripts.Enums;
|
2025-06-17 11:57:59 +02:00
|
|
|
|
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
|
|
|
|
{
|
2025-08-13 16:51:56 +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();
|
2025-09-11 10:54:02 +02:00
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
|
public delegate void InitializedEventHandler();
|
2025-08-13 16:51:56 +02:00
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
[Signal]
|
|
|
|
|
|
public delegate void EvolvedEventHandler(WeaponResource newWeaponResource);
|
|
|
|
|
|
|
2025-08-13 16:51:56 +02:00
|
|
|
|
public int Ammo { get; set; } = 0;
|
|
|
|
|
|
|
|
|
|
|
|
private int _loadedAmmo;
|
|
|
|
|
|
|
|
|
|
|
|
public int LoadedAmmo
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _loadedAmmo;
|
|
|
|
|
|
private set
|
|
|
|
|
|
{
|
|
|
|
|
|
_loadedAmmo = value;
|
|
|
|
|
|
InventoryManager.Instance?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
|
|
|
|
|
|
|
|
|
|
|
|
private Timer _cooldownTimer;
|
|
|
|
|
|
private Timer _rechargeTimer;
|
|
|
|
|
|
|
|
|
|
|
|
private GameManager _gameManager;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly StringName _shieldAmmoType = "SHIELD";
|
|
|
|
|
|
private readonly StringName _batteryAmmoType = "BATTERY";
|
|
|
|
|
|
//private bool UsesBattery => WeaponData.AmmoKey == _shieldAmmoType;
|
|
|
|
|
|
|
|
|
|
|
|
private WeaponAmmoType _ammoType = WeaponAmmoType.Infinite;
|
|
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
// Runtime experience for this weapon instance (NOT stored in resources)
|
|
|
|
|
|
private int _currentExperience = 0;
|
|
|
|
|
|
|
2025-08-13 16:51:56 +02:00
|
|
|
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
|
{
|
|
|
|
|
|
_cooldownTimer = GetNode<Timer>("./ShootTimer");
|
|
|
|
|
|
|
|
|
|
|
|
//Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
SetAmmoType();
|
|
|
|
|
|
|
|
|
|
|
|
if (_ammoType is WeaponAmmoType.Battery && _rechargeTimer is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_rechargeTimer = new Timer();
|
|
|
|
|
|
this.AddChild(_rechargeTimer);
|
|
|
|
|
|
_rechargeTimer.Timeout += RechargeTimerOnTimeout;
|
|
|
|
|
|
|
|
|
|
|
|
_rechargeTimer.Start(WeaponData.RechargeTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start full
|
|
|
|
|
|
if (WeaponData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadedAmmo = WeaponData.BulletCapacity;
|
|
|
|
|
|
}
|
2025-09-11 10:54:02 +02:00
|
|
|
|
|
|
|
|
|
|
EmitSignalInitialized();
|
2025-08-13 16:51:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RechargeTimerOnTimeout()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (LoadedAmmo < WeaponData.BulletCapacity)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadedAmmo += WeaponData.RechargeAmount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_rechargeTimer.Start(WeaponData.RechargeTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SetAmmoType()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
_ammoType = WeaponAmmoType.Infinite;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (WeaponData.AmmoKey == _shieldAmmoType)
|
|
|
|
|
|
{
|
|
|
|
|
|
_ammoType = WeaponAmmoType.Shield;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (WeaponData.AmmoKey == _batteryAmmoType)
|
|
|
|
|
|
{
|
|
|
|
|
|
_ammoType = WeaponAmmoType.Battery;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ammoType = WeaponAmmoType.Ammo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Reload()
|
|
|
|
|
|
{
|
|
|
|
|
|
EmitSignalReloading();
|
|
|
|
|
|
|
|
|
|
|
|
_cooldownTimer.Start(WeaponData.ReloadTime);
|
|
|
|
|
|
|
|
|
|
|
|
if (_ammoType is WeaponAmmoType.Infinite)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadedAmmo = WeaponData.BulletCapacity;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var ammoToLoad =
|
|
|
|
|
|
InventoryManager.Instance.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity - LoadedAmmo);
|
|
|
|
|
|
|
|
|
|
|
|
if (ammoToLoad > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadedAmmo = ammoToLoad;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
EmitSignalEmpty();
|
|
|
|
|
|
//GD.Print("Out of ammo");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add experience to this weapon instance. When reaching the threshold defined on the WeaponResource,
|
|
|
|
|
|
/// evolve into the configured NextLevelWeapon. Resources are not mutated; the instance swaps its reference
|
|
|
|
|
|
/// to the next-tier Resource to reflect the new behavior.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="amount">Amount of experience to add (positive integer)</param>
|
|
|
|
|
|
public void GainExperience(int amount)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (amount <= 0) return;
|
|
|
|
|
|
if (WeaponData == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// If weapon has no progression, ignore
|
|
|
|
|
|
if (WeaponData.ExperienceToNextLevel <= 0 || WeaponData.NextLevelWeapon == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
_currentExperience += amount;
|
|
|
|
|
|
|
|
|
|
|
|
while (WeaponData.ExperienceToNextLevel > 0 && _currentExperience >= WeaponData.ExperienceToNextLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Evolve
|
|
|
|
|
|
_currentExperience -= WeaponData.ExperienceToNextLevel;
|
|
|
|
|
|
|
|
|
|
|
|
var next = WeaponData.NextLevelWeapon;
|
|
|
|
|
|
if (next == null) break;
|
|
|
|
|
|
|
|
|
|
|
|
WeaponData = next;
|
|
|
|
|
|
|
|
|
|
|
|
// Re-init to apply new capacities / rates
|
|
|
|
|
|
Init();
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignalEvolved(next);
|
|
|
|
|
|
|
|
|
|
|
|
// Continue loop in case the new tier also has immediate threshold
|
|
|
|
|
|
if (WeaponData.ExperienceToNextLevel <= 0 || WeaponData.NextLevelWeapon == null) break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 16:51:56 +02:00
|
|
|
|
private bool HandlePreShoot()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Waiting on reload or Rate of Fire cooldown?
|
|
|
|
|
|
if (!_cooldownTimer.IsStopped())
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle Shield powered weapons
|
|
|
|
|
|
if (_ammoType is WeaponAmmoType.Shield)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (GameController.Instance.Player.Storage.Shield.CurrentResource >= WeaponData.AmmoPerShot)
|
|
|
|
|
|
{
|
|
|
|
|
|
GameController.Instance.Player.Storage.Shield.CurrentResource -= WeaponData.AmmoPerShot;
|
2025-08-14 10:58:54 +02:00
|
|
|
|
return true;
|
2025-08-13 16:51:56 +02:00
|
|
|
|
}
|
2025-08-14 10:58:54 +02:00
|
|
|
|
|
|
|
|
|
|
EmitSignalEmpty();
|
2025-08-13 16:51:56 +02:00
|
|
|
|
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Out of ammo?
|
|
|
|
|
|
if (LoadedAmmo < WeaponData.AmmoPerShot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_ammoType is WeaponAmmoType.Ammo && WeaponData.AutoReload)
|
|
|
|
|
|
{
|
|
|
|
|
|
Reload();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignalEmpty();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_ammoType is WeaponAmmoType.Ammo or WeaponAmmoType.Battery)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadedAmmo -= WeaponData.AmmoPerShot;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Shoot(BulletOwner? ownerOverride = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!HandlePreShoot()) 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 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
// Associate the bullet with this weapon instance so kills can be attributed
|
|
|
|
|
|
bulletData.SourceWeapon = this;
|
|
|
|
|
|
|
2025-08-13 16:51:56 +02:00
|
|
|
|
bullet.Initialize(bulletData);
|
|
|
|
|
|
|
|
|
|
|
|
//bullet.SetDirection(ShootDirection);
|
|
|
|
|
|
bullet.SetDirection(spreadDirection);
|
|
|
|
|
|
bullet.Speed = WeaponData.BulletData.BulletSpeed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-14 10:58:54 +02:00
|
|
|
|
if (_ammoType is WeaponAmmoType.Ammo && WeaponData.AutoReload && LoadedAmmo < WeaponData.AmmoPerShot)
|
2025-08-13 16:51:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
Reload();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
public new void Hide()
|
2025-08-13 16:51:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-28 18:44:23 +01:00
|
|
|
|
public new void Show()
|
2025-08-13 16:51:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2025-06-17 11:57:59 +02:00
|
|
|
|
}
|