cirnogodot/Scripts/Activables/BulletEmitter.cs

80 lines
2 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]
public float EmitCoolDown { get; private set; } = 1f;
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-02-14 19:35:13 +01:00
private BulletSpawner _bulletSpawner;
2025-02-23 20:00:01 +01:00
private bool _isEmitting = false;
private double _emitTimer = 0f;
2025-02-14 19:35:13 +01:00
public override void _Ready()
{
_bulletSpawner = GetNode<BulletSpawner>("BulletSpawner");
2025-02-23 20:00:01 +01:00
if (EmitOnStart)
{
_isEmitting = true;
}
}
public override void _Process(double delta)
{
if (!_isEmitting) return;
_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()
{
_bulletSpawner.SpawnBullet(BulletResource.MakeBullet(this.GlobalPosition, Count, Spread, EmissionRotation));
}
2025-02-14 19:35:13 +01:00
public void Activate(ActivationType activationType = ActivationType.Toggle)
{
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-02-23 20:00:01 +01:00
_isEmitting = true;
_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-02-23 20:00:01 +01:00
_isEmitting = false;
_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:
_isEmitting = !_isEmitting;
_emitTimer = 0;
2025-02-14 19:35:13 +01:00
break;
case ActivationType.Destroy:
break;
}
}
}