2025-05-13 19:22:01 +08:00

96 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Bullets/SoulBullet.cs")]
public partial class SoulBullet : Sprite2D
{
[Export(PropertyHint.None, "")]
private Area2D area;
public Vector2 dir = Vector2.Right;
private static readonly AudioStream impact = GD.Load<AudioStream>("res://Audio/Sounds/snd_bomb.wav");
private static readonly AudioStream blocked = GD.Load<AudioStream>("res://Audio/Sounds/snd_bell_ch1.wav");
public override void _Ready()
{
area.AreaEntered += Collide;
}
public override void _Process(double delta)
{
base.GlobalPosition += dir * Main.deltaTime * 2f;
}
private void Collide(Node other)
{
if (!(other.GetParent() is IBullet bullet))
{
return;
}
if (bullet.type == IBullet.Type.Yellow)
{
bullet.HP--;
if (bullet.HP <= 0)
{
Coroutine.Start(DestroyBullet(bullet.node), bullet.node);
}
else
{
bullet.ShotAt();
}
if (BattleDR.current.TP < 100 && BattleDR.current.grazeCD <= 0f)
{
BattleDR.current.TP++;
BattleDR.current.grazeCD = 10f;
}
Audio.PlaySound(impact);
bullet.onShot?.Invoke(bullet.node);
QueueFree();
}
else if (bullet.type == IBullet.Type.Block)
{
Audio.PlaySound(blocked);
QueueFree();
}
}
private static IEnumerator DestroyBullet(Node2D bullet)
{
if (!GodotObject.IsInstanceValid(bullet))
{
yield break;
}
bullet.ProcessMode = ProcessModeEnum.Disabled;
Vector2 s = bullet.Scale;
Vector2 ts = s * 2f;
Color c = bullet.Modulate;
float a = 0f;
for (float b = 15f; a < b; a += Main.deltaTime)
{
if (!GodotObject.IsInstanceValid(bullet))
{
yield break;
}
bullet.Modulate = c.Lerp(Main.colorClear, a / b);
bullet.Scale = s.Lerp(ts, a / b);
yield return null;
}
if (GodotObject.IsInstanceValid(bullet))
{
bullet.QueueFree();
}
}
public override void _ExitTree()
{
BattleDR.current.shots.Remove(this);
}
}