mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 12:35:53 +00:00
Implemented battery weapons
This commit is contained in:
parent
d5c22045b7
commit
54fa750bca
20 changed files with 372 additions and 264 deletions
|
|
@ -6,5 +6,6 @@ public partial class IsoPlayerStateMachine : StateMachineBase<PlayerState,Charac
|
|||
{
|
||||
[Export] public override PlayerState InitialState { get; protected set; } = PlayerState.Init;
|
||||
|
||||
[Export] public IsoPlayerStorageModule Storage { get; private set; }
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,14 @@
|
|||
using Godot;
|
||||
using Cirno.Scripts.Components.Actors;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
public partial class IsoPlayerStorageModule : Node
|
||||
{
|
||||
[Export] public IsoPlayerFSMProxy Root { get; private set; }
|
||||
|
||||
[Export] public ActorResourceProvider Shield { get; private set; }
|
||||
|
||||
public Node RootAsNode => Root;
|
||||
|
||||
public Vector2 FacingDirection { get; set; } = Vector2.Down;
|
||||
|
|
|
|||
|
|
@ -229,7 +229,7 @@ public partial class PlayerWeaponProvider3D : Node
|
|||
weapon.WeaponData = startingItem.WeaponData3D;
|
||||
|
||||
weapon.Sprite.Texture = startingItem.InventorySprite;
|
||||
|
||||
weapon.Init();
|
||||
this.AddWeapon(weapon);
|
||||
return weapon;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ public partial class Shooting : EnemyStateBase3D
|
|||
//DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
|
||||
|
||||
EquippedWeapon.WeaponData = Storage.Root.EnemyResource.Weapon;
|
||||
EquippedWeapon.Init();
|
||||
|
||||
_currentStrafeTarget = null;
|
||||
|
||||
|
|
|
|||
9
Scripts/Enums/WeaponAmmoType.cs
Normal file
9
Scripts/Enums/WeaponAmmoType.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
namespace Cirno.Scripts.Enums;
|
||||
|
||||
public enum WeaponAmmoType
|
||||
{
|
||||
Infinite,
|
||||
Ammo,
|
||||
Shield,
|
||||
Battery,
|
||||
}
|
||||
1
Scripts/Enums/WeaponAmmoType.cs.uid
Normal file
1
Scripts/Enums/WeaponAmmoType.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://0ouvmqwpq4xa
|
||||
|
|
@ -34,6 +34,11 @@ public partial class WeaponResource : Resource
|
|||
|
||||
[Export] public StringName ItemKey;
|
||||
[Export] public StringName AmmoKey;
|
||||
|
||||
[ExportCategory("Battery Recharge")]
|
||||
[Export] public double RechargeTime = 0.5d;
|
||||
[Export]public int RechargeAmount = 1;
|
||||
|
||||
#region Bullet spawn data
|
||||
[ExportCategory("Bullet Spawn Data")]
|
||||
[Export] public int BulletsPerShot = 1;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Cirno.Scripts.Controllers;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
|
@ -7,220 +8,279 @@ namespace Cirno.Scripts.Weapons;
|
|||
|
||||
public partial class Weapon3D : Node3D
|
||||
{
|
||||
[Export]
|
||||
public WeaponResource WeaponData { get; set; }
|
||||
[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 PackedScene BulletScene { get; set; }
|
||||
|
||||
[Export] public StringName PowerKey { get; set; } = "POWER";
|
||||
|
||||
[Signal]
|
||||
public delegate void ShootingEventHandler();
|
||||
[Export] public Marker3D Muzzle { get; set; }
|
||||
|
||||
[Signal]
|
||||
public delegate void ReloadingEventHandler();
|
||||
|
||||
[Signal] public delegate void EmptyEventHandler();
|
||||
|
||||
public int Ammo { get; set; } = 0;
|
||||
[Export] public Marker3D Pivot { get; set; }
|
||||
|
||||
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 GameManager _gameManager;
|
||||
[Export] public Sprite3D Sprite { get; private set; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
[Export] public StringName PowerKey { get; set; } = "POWER";
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
EmitSignalReloading();
|
||||
|
||||
_cooldownTimer.Start(WeaponData.ReloadTime);
|
||||
|
||||
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
[Signal]
|
||||
public delegate void ShootingEventHandler();
|
||||
|
||||
public void Shoot(BulletOwner? ownerOverride = null)
|
||||
{
|
||||
// Waiting on reload or Rate of Fire cooldown?
|
||||
if (!_cooldownTimer.IsStopped())
|
||||
{
|
||||
return;
|
||||
}
|
||||
[Signal]
|
||||
public delegate void ReloadingEventHandler();
|
||||
|
||||
// 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
|
||||
// {
|
||||
// EmitSignalEmpty();
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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?
|
||||
[Signal]
|
||||
public delegate void EmptyEventHandler();
|
||||
|
||||
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);
|
||||
public int Ammo { get; set; } = 0;
|
||||
|
||||
// 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));
|
||||
private int _loadedAmmo;
|
||||
|
||||
// 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);
|
||||
public int LoadedAmmo
|
||||
{
|
||||
get => _loadedAmmo;
|
||||
private set
|
||||
{
|
||||
_loadedAmmo = value;
|
||||
InventoryManager.Instance?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
|
||||
|
||||
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;
|
||||
}
|
||||
private Timer _cooldownTimer;
|
||||
private Timer _rechargeTimer;
|
||||
|
||||
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 GameManager _gameManager;
|
||||
|
||||
private float GetBulletStrengthMultiplier(float baseDamage, float maxDamage, float maxPower)
|
||||
{
|
||||
var p = InventoryManager.Instance.GetItemCount(PowerKey);
|
||||
private readonly StringName _shieldAmmoType = "SHIELD";
|
||||
private readonly StringName _batteryAmmoType = "BATTERY";
|
||||
//private bool UsesBattery => WeaponData.AmmoKey == _shieldAmmoType;
|
||||
|
||||
float minMultiplier = 1.0f;
|
||||
float maxMultiplier = maxDamage / baseDamage;
|
||||
private WeaponAmmoType _ammoType = WeaponAmmoType.Infinite;
|
||||
|
||||
float normalizedPower = Mathf.Clamp((float)p / maxPower, 0f, 1f);
|
||||
return Mathf.Lerp(minMultiplier, maxMultiplier, normalizedPower);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
|
||||
}
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
_cooldownTimer = GetNode<Timer>("./ShootTimer");
|
||||
|
||||
//Init();
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
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()
|
||||
{
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue