save
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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 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(_baseDamage * isSe);
|
||||
}
|
||||
|
||||
CallDeferred(nameof(Despawn));
|
||||
}
|
||||
|
||||
private void BodyEntered(Node2D node)
|
||||
{
|
||||
CallDeferred(nameof(Despawn));
|
||||
}
|
||||
|
||||
private void Despawn()
|
||||
{
|
||||
OnDespawn?.Invoke(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user