cirnogodot/Scripts/Activables/BulletEmitter.cs

139 lines
3.4 KiB
C#
Raw Normal View History

2025-02-14 19:35:13 +01:00
using System;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.Activables;
public partial class BulletEmitter : Node2D, IActivable
{
[Export]
public BulletResource BulletResource { get; set; }
[Export]
2025-02-23 20:00:01 +01:00
public bool EmitOnStart { get; set; } = false;
[Export]
2025-03-14 23:04:59 +01:00
public bool InvertSignal { get; private set; } = false;
[Export]
public float EmitCoolDown { get; private set; } = 0.2f;
[Export] public float BurstCoolDown { get; private set; } = 2f;
2025-02-14 19:35:13 +01:00
[Export] public float Spread { get; set; } = 0f;
2025-02-23 20:00:01 +01:00
[Export] public int Count { get; set; } = 1;
2025-02-14 19:35:13 +01:00
2025-02-21 16:27:57 +01:00
[Export] public float EmissionRotation { get; set; } = 0f;
2025-03-14 23:04:59 +01:00
[Export] public float RotationSpeed { get; private set; } = 0f;
[Export] public int BulletsPerBurst { get; private set; } = 1;
[Signal]
public delegate void StateChangedEventHandler(bool isEmitting);
2025-02-14 19:35:13 +01:00
private BulletSpawner _bulletSpawner;
2025-02-23 20:00:01 +01:00
2025-03-06 17:05:45 +01:00
protected bool IsEmitting = false;
2025-02-23 20:00:01 +01:00
private double _emitTimer = 0f;
2025-02-14 19:35:13 +01:00
2025-03-14 23:04:59 +01:00
private double _reloadTimer = 0f;
private float _currentEmissionRotation = 0f;
private int _currentBullets = 0;
private bool _isReloading = false;
2025-02-14 19:35:13 +01:00
public override void _Ready()
{
_bulletSpawner = GetNode<BulletSpawner>("BulletSpawner");
2025-03-14 23:04:59 +01:00
_currentEmissionRotation = EmissionRotation;
_currentBullets = BulletsPerBurst;
2025-02-23 20:00:01 +01:00
if (EmitOnStart)
{
2025-03-06 17:05:45 +01:00
IsEmitting = true;
CallDeferred(MethodName.Shoot);
2025-02-23 20:00:01 +01:00
}
2025-03-14 23:04:59 +01:00
EmitSignal(SignalName.StateChanged, IsEmitting);
2025-02-23 20:00:01 +01:00
}
public override void _Process(double delta)
{
2025-03-06 17:05:45 +01:00
if (!IsEmitting) return;
2025-03-14 23:04:59 +01:00
if (_isReloading)
{
_reloadTimer += delta;
if (_reloadTimer >= BurstCoolDown)
{
_currentBullets = BulletsPerBurst;
_isReloading = false;
_reloadTimer = 0f;
}
return;
}
2025-02-23 20:00:01 +01:00
_emitTimer += delta;
if (_emitTimer >= EmitCoolDown)
{
Shoot();
_emitTimer = 0f;
}
2025-02-14 19:35:13 +01:00
}
2025-02-23 20:00:01 +01:00
public void Shoot()
{
2025-03-14 23:04:59 +01:00
_bulletSpawner.SpawnBullet(BulletResource.MakeBullet(this.GlobalPosition, Count, Spread, _currentEmissionRotation));
_currentEmissionRotation += RotationSpeed;
_currentBullets--;
if (_currentBullets <= 0)
{
_isReloading = true;
}
2025-02-23 20:00:01 +01:00
}
2025-03-09 21:58:25 +01:00
public bool Activate(ActivationType activationType = ActivationType.Toggle)
2025-02-14 19:35:13 +01:00
{
switch (activationType)
{
2025-02-23 20:00:01 +01:00
case ActivationType.Open:
2025-02-14 19:35:13 +01:00
case ActivationType.Enable:
2025-03-14 23:04:59 +01:00
IsEmitting = !InvertSignal;
2025-02-23 20:00:01 +01:00
_emitTimer = 0;
2025-02-14 19:35:13 +01:00
break;
2025-02-23 20:00:01 +01:00
case ActivationType.Close:
2025-02-14 19:35:13 +01:00
case ActivationType.Disable:
2025-03-14 23:04:59 +01:00
IsEmitting = InvertSignal;
2025-02-23 20:00:01 +01:00
_emitTimer = 0;
2025-02-14 19:35:13 +01:00
break;
case ActivationType.Use:
2025-02-23 20:00:01 +01:00
case ActivationType.Toggle:
2025-03-06 17:05:45 +01:00
IsEmitting = !IsEmitting;
2025-02-23 20:00:01 +01:00
_emitTimer = 0;
2025-02-14 19:35:13 +01:00
break;
case ActivationType.Destroy:
break;
}
2025-03-14 23:04:59 +01:00
EmitSignal(SignalName.StateChanged, IsEmitting);
2025-03-09 21:58:25 +01:00
return true;
2025-02-14 19:35:13 +01:00
}
2025-06-18 11:33:27 +02:00
public void Toggle()
{
this.Activate();
}
2025-02-14 19:35:13 +01:00
}