53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
using System;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
public partial class Projectile : Node2D
|
|
{
|
|
[Export] private Array<EnemyType> _superEffective;
|
|
[Export] private float _baseDamage;
|
|
[Export] private float _speed;
|
|
[Export] private Area2D _hurtBox;
|
|
|
|
private float _ttl = 10f;
|
|
|
|
public event Action<Projectile> OnDespawn;
|
|
public Vector2 Direction;
|
|
public float Damage;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_hurtBox.AreaEntered += AreaEntered;
|
|
_hurtBox.BodyEntered += BodyEntered;
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
GlobalPosition += Direction * _speed * (float)delta;
|
|
|
|
_ttl -= (float)delta;
|
|
if (_ttl > 0f) return;
|
|
CallDeferred(nameof(Despawn));
|
|
}
|
|
|
|
private void AreaEntered(Area2D area)
|
|
{
|
|
if (area is EnemyArea earea)
|
|
{
|
|
float isSe = _superEffective.Contains(earea.Enemy.Type) ? 2f : 1f;
|
|
earea.Enemy.Health.Substract(Damage * isSe);
|
|
}
|
|
|
|
CallDeferred(nameof(Despawn));
|
|
}
|
|
|
|
private void BodyEntered(Node2D node)
|
|
{
|
|
CallDeferred(nameof(Despawn));
|
|
}
|
|
|
|
private void Despawn()
|
|
{
|
|
OnDespawn?.Invoke(this);
|
|
}
|
|
} |