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;
|
|
|
|
|
using Cirno.Scripts.Resources;
|
2024-11-15 16:32:26 +01:00
|
|
|
|
|
|
|
|
public partial class Weapon : Node2D
|
|
|
|
|
{
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
public int Ammo { get; set; } = 0;
|
|
|
|
|
|
|
|
|
|
public int LoadedAmmo { get; private set; }
|
|
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
|
_gameManager = GetNode<GameManager>("/root/GameScene");
|
2024-11-15 16:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reload()
|
|
|
|
|
{
|
2025-02-09 23:20:49 +01:00
|
|
|
_cooldownTimer.Start(WeaponData.ReloadTime);
|
2024-11-15 16:32:26 +01:00
|
|
|
|
2025-02-09 23:20:49 +01:00
|
|
|
if (WeaponData.InfiniteAmmo)
|
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
|
|
|
|
|
{
|
|
|
|
|
// TODO: Calculate subtraction, etc
|
2025-02-09 23:20:49 +01:00
|
|
|
LoadedAmmo = WeaponData.BulletCapacity;
|
2024-11-15 16:32:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Shoot()
|
|
|
|
|
{
|
|
|
|
|
// Waiting on reload or Rate of Fire cooldown?
|
|
|
|
|
if (!_cooldownTimer.IsStopped())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Out of ammo?
|
|
|
|
|
if (LoadedAmmo <= 0)
|
|
|
|
|
{
|
2025-02-09 23:20:49 +01:00
|
|
|
if (WeaponData.AutoReload)
|
2024-11-15 16:32:26 +01:00
|
|
|
{
|
|
|
|
|
Reload();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// Rotate the ShootDirection by the spread angle
|
|
|
|
|
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
|
|
|
|
|
|
2025-02-09 23:20:49 +01:00
|
|
|
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletScene, _muzzle.GlobalPosition);
|
2025-01-28 10:43:35 +01:00
|
|
|
|
|
|
|
|
if (bullet == null)
|
|
|
|
|
{
|
|
|
|
|
GD.PrintErr("Bullet is null, not shooting");
|
|
|
|
|
return;
|
|
|
|
|
};
|
2025-01-27 17:13:26 +01:00
|
|
|
|
2025-01-28 10:43:35 +01:00
|
|
|
//bullet.SetDirection(ShootDirection);
|
|
|
|
|
bullet.SetDirection(spreadDirection);
|
2025-02-09 23:20:49 +01:00
|
|
|
bullet.Speed = WeaponData.BulletSpeed;
|
2025-01-28 10:43:35 +01:00
|
|
|
}
|
2024-11-15 16:32:26 +01:00
|
|
|
|
|
|
|
|
LoadedAmmo -= 1;
|
|
|
|
|
|
2025-02-09 23:20:49 +01:00
|
|
|
_cooldownTimer.Start(WeaponData.RateOfFire);
|
2024-11-15 16:32:26 +01:00
|
|
|
}
|
|
|
|
|
}
|