mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 08:45:33 +00:00
290 lines
No EOL
7.9 KiB
C#
290 lines
No EOL
7.9 KiB
C#
using Cirno.Scripts.Controllers;
|
|
using Cirno.Scripts.Enums;
|
|
using Cirno.Scripts.Resources;
|
|
using Cirno.Scripts.Utils;
|
|
using Godot;
|
|
|
|
namespace Cirno.Scripts.Weapons;
|
|
|
|
public partial class Weapon3D : Node3D
|
|
{
|
|
[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();
|
|
|
|
[Signal]
|
|
public delegate void InitializedEventHandler();
|
|
|
|
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;
|
|
|
|
|
|
// 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;
|
|
}
|
|
|
|
EmitSignalInitialized();
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
return true;
|
|
}
|
|
|
|
EmitSignalEmpty();
|
|
_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);
|
|
}
|
|
|
|
bullet.Initialize(bulletData);
|
|
|
|
//bullet.SetDirection(ShootDirection);
|
|
bullet.SetDirection(spreadDirection);
|
|
bullet.Speed = WeaponData.BulletData.BulletSpeed;
|
|
}
|
|
|
|
|
|
|
|
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
|
|
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
|
|
// {
|
|
// // Notify hud to decrease weapon
|
|
//
|
|
// }
|
|
|
|
if (_ammoType is WeaponAmmoType.Ammo && WeaponData.AutoReload && LoadedAmmo < WeaponData.AmmoPerShot)
|
|
{
|
|
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);
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
}
|
|
|
|
public void Show()
|
|
{
|
|
}
|
|
} |