cirnogodot/Scripts/Weapon.cs

262 lines
6.5 KiB
C#
Raw Normal View History

2024-11-15 16:32:26 +01:00
using Godot;
using System;
2025-01-27 17:13:26 +01:00
using System.Diagnostics;
2024-11-15 16:32:26 +01:00
using Cirno.Scripts;
2025-02-09 23:20:49 +01:00
using Cirno.Scripts.Components;
2025-06-08 16:33:38 +02:00
using Cirno.Scripts.Controllers;
2025-02-09 23:20:49 +01:00
using Cirno.Scripts.Resources;
2025-05-01 11:59:32 +02:00
using Cirno.Scripts.Utils;
2024-11-15 16:32:26 +01:00
2025-05-06 15:07:22 +02:00
public partial class Weapon : Node2D
2024-11-15 16:32:26 +01:00
{
2025-02-09 23:20:49 +01:00
[Export]
public WeaponResource WeaponData { get; set; }
2024-11-15 16:32:26 +01:00
[Export]
public PackedScene BulletScene { get; set; }
[Export]
public Marker2D Muzzle { get; set; }
2025-05-06 15:07:22 +02:00
[Export]
public Marker2D Pivot { get; set; }
[Export]
public Sprite2D Sprite { get; private set; }
2025-05-20 15:57:35 +02:00
[Export] public StringName PowerKey { get; set; } = "POWER";
2025-05-06 15:07:22 +02:00
2025-04-08 17:59:20 +02:00
[Signal]
public delegate void ShootingEventHandler();
[Signal]
public delegate void ReloadingEventHandler();
[Signal] public delegate void EmptyEventHandler();
2024-11-15 16:32:26 +01:00
public int Ammo { get; set; } = 0;
2025-03-05 18:55:30 +01:00
private int _loadedAmmo;
public int LoadedAmmo
{
get => _loadedAmmo;
private set
{
_loadedAmmo = value;
_inventoryManager?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
}
}
2024-11-15 16:32:26 +01:00
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
private Timer _cooldownTimer;
private Marker2D _muzzle;
2025-01-27 17:13:26 +01:00
private Node2D _bulletsContainer;
private GameManager _gameManager;
2025-02-11 11:50:45 +01:00
private InventoryManager _inventoryManager;
2025-05-02 13:10:38 +02:00
private readonly StringName _shieldAmmoType = "SHIELD";
private bool UsesBattery => WeaponData.AmmoKey == _shieldAmmoType;
2024-11-15 16:32:26 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_muzzle = GetNode<Marker2D>("./Muzzle");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
2025-01-27 17:13:26 +01:00
2025-02-11 11:50:45 +01:00
_gameManager = this.GetGameManager();
_inventoryManager = this.GetInventoryManager();
2025-03-04 11:33:29 +01:00
// Start full
if (WeaponData != null)
{
LoadedAmmo = WeaponData.BulletCapacity;
}
2024-11-15 16:32:26 +01:00
}
public void Reload()
{
2025-04-08 17:59:20 +02:00
EmitSignalReloading();
2025-02-09 23:20:49 +01:00
_cooldownTimer.Start(WeaponData.ReloadTime);
2024-11-15 16:32:26 +01:00
2025-02-11 11:50:45 +01:00
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
2024-11-15 16:32:26 +01:00
{
2025-02-09 23:20:49 +01:00
LoadedAmmo = WeaponData.BulletCapacity;
2024-11-15 16:32:26 +01:00
}
else
{
2025-05-02 13:10:38 +02:00
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity - LoadedAmmo);
2025-03-05 18:55:30 +01:00
2025-02-11 11:50:45 +01:00
if (ammoToLoad > 0)
{
LoadedAmmo = ammoToLoad;
}
else
{
2025-04-08 17:59:20 +02:00
EmitSignalEmpty();
//GD.Print("Out of ammo");
2025-02-11 11:50:45 +01:00
}
2024-11-15 16:32:26 +01:00
}
}
public void Shoot(BulletOwner? ownerOverride = null)
2024-11-15 16:32:26 +01:00
{
// Waiting on reload or Rate of Fire cooldown?
if (!_cooldownTimer.IsStopped())
{
return;
}
2025-05-02 13:10:38 +02:00
// Check for battery if it's used
if (UsesBattery)
{
if (GameManager.Instance.Player.Shield.CurrentResource >= WeaponData.AmmoPerShot)
{
GameManager.Instance.Player.Shield.CurrentResource -= WeaponData.AmmoPerShot;
}
else
{
2025-05-08 10:46:02 +02:00
EmitSignalEmpty();
2025-05-02 13:10:38 +02:00
return;
}
}
2024-11-15 16:32:26 +01:00
// Out of ammo?
2025-05-02 13:10:38 +02:00
if (LoadedAmmo < WeaponData.AmmoPerShot)
2024-11-15 16:32:26 +01:00
{
2025-02-09 23:20:49 +01:00
if (WeaponData.AutoReload)
2024-11-15 16:32:26 +01:00
{
Reload();
}
2025-05-08 10:46:02 +02:00
EmitSignalEmpty();
2024-11-15 16:32:26 +01:00
return;
}
2025-04-08 17:59:20 +02:00
EmitSignalShooting();
2024-11-15 16:32:26 +01:00
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
2025-01-27 17:13:26 +01:00
2025-02-09 23:20:49 +01:00
float halfSpread = WeaponData.SpreadAngle / 2f;
float spreadStep = WeaponData.BulletsPerShot > 1 ? WeaponData.SpreadAngle / (WeaponData.BulletsPerShot - 1) : 0;
2025-01-28 10:43:35 +01:00
2025-02-09 23:20:49 +01:00
for (int i = 0; i < WeaponData.BulletsPerShot; i++)
2025-01-27 17:13:26 +01:00
{
2025-01-28 10:43:35 +01:00
// Calculate angle offset for this bullet
float spreadOffset = -halfSpread + (spreadStep * i);
2025-05-01 11:59:32 +02:00
// 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
);
}
2025-01-28 10:43:35 +01:00
// Rotate the ShootDirection by the spread angle
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
2025-06-08 16:33:38 +02:00
2025-06-17 11:57:59 +02:00
var bullet = PoolingManager.Instance.SpawnBullet<Bullet>(WeaponData.BulletData);
2025-06-08 16:33:38 +02:00
bullet.GlobalPosition = _muzzle.GlobalPosition;
2025-01-28 10:43:35 +01:00
2025-06-08 16:33:38 +02:00
//var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletData.BulletScene, _muzzle.GlobalPosition);
2025-01-28 10:43:35 +01:00
if (bullet == null)
{
GD.PrintErr("Bullet is null, not shooting");
return;
};
var bulletData = WeaponData.MakeBullet(_muzzle.GlobalPosition);
if (ownerOverride.HasValue)
{
bulletData.Owner = ownerOverride.Value;
}
2025-05-20 15:57:35 +02:00
if (bulletData.Owner is BulletOwner.Player || ownerOverride is BulletOwner.Player)
{
// Apply the P multiplier
bulletData.Damage *=
GetBulletStrengthMultiplier(bulletData.Damage, bulletData.OriginalBulletResource.MaxDamage, 20);
}
2025-02-11 19:00:01 +01:00
bullet.Initialize(bulletData, _gameManager);
2025-01-27 17:13:26 +01:00
2025-01-28 10:43:35 +01:00
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);
2025-02-12 16:20:55 +01:00
bullet.Speed = WeaponData.BulletData.BulletSpeed;
2025-01-28 10:43:35 +01:00
}
2024-11-15 16:32:26 +01:00
2025-05-02 13:10:38 +02:00
if (!UsesBattery)
{
LoadedAmmo -= WeaponData.AmmoPerShot;
}
2025-03-05 18:55:30 +01:00
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
// {
// // Notify hud to decrease weapon
//
// }
2024-11-15 16:32:26 +01:00
2025-03-05 18:55:30 +01:00
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
2024-11-15 16:32:26 +01:00
}
2025-05-06 15:07:22 +02:00
2025-05-20 15:57:35 +02:00
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);
}
2025-05-06 15:07:22 +02:00
public void RotateWeapon(Vector2 facingDirection, Vector2 leftPosition, Vector2 rightPosition)
{
bool facingLeft = facingDirection.X < 0;
if (facingLeft)
{
Sprite.Position = leftPosition - Pivot.Position;
Sprite.FlipH = true;
}
else
{
Sprite.Position = rightPosition - Pivot.Position;
Sprite.FlipH = false;
}
// var angle = Mathf.Atan2(StorageModule.FacingDirection.X, StorageModule.FacingDirection.Y);
//
// // Weapon is drawn pointing right, but we want it to point "up" along aim vector.
// // So we apply a 90° offset (π/2), but...
// // If facing left, we also add 180° (π) to "un-mirror" it visually.
// float rotationOffset = (Mathf.Pi / 2f);
//
// if (facingLeft)
// {
// EquippedWeapon.Sprite.Rotation = -(angle + rotationOffset);// + rotationOffset + Mathf.Pi;
// EquippedWeapon.Sprite.Position = WeaponLeftOffset;
// EquippedWeapon.Sprite.FlipH = true; // disable FlipH to avoid messing with rotation
// EquippedWeapon.Sprite.FlipV = false;
// }
// else
// {
// EquippedWeapon.Sprite.Rotation = angle;// + rotationOffset;
// EquippedWeapon.Sprite.Position = WeaponRightOffset;
// EquippedWeapon.Sprite.FlipH = false;
// EquippedWeapon.Sprite.FlipV = true;
// }
}
2024-11-15 16:32:26 +01:00
}