save
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using fgj26.Scripts.Common;
|
||||
using fgj26.Scripts.Helpers;
|
||||
|
||||
public partial class Turret : Node
|
||||
{
|
||||
[Export] private Area2D _Attackrange;
|
||||
[Export] private float _fireRate;
|
||||
[Export] private Node2D _parent;
|
||||
[Export] private ProjectilePool _projectilePool;
|
||||
private float _fireTimer = 0f;
|
||||
|
||||
private HashSet<Enemy> _enemiesInRange = new HashSet<Enemy>();
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
_Attackrange.AreaEntered += EnemyEntered;
|
||||
_Attackrange.AreaExited += EnemyExited;
|
||||
}
|
||||
|
||||
private void EnemyEntered(Area2D enemyHitBox)
|
||||
{
|
||||
if (enemyHitBox is EnemyArea earea)
|
||||
{
|
||||
_enemiesInRange.Add(earea.Enemy);
|
||||
GD.Print(earea.Name);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnemyExited(Area2D enemyHitBox)
|
||||
{
|
||||
if (enemyHitBox is EnemyArea earea)
|
||||
{
|
||||
_enemiesInRange.Remove(earea.Enemy);
|
||||
GD.Print(earea.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_enemiesInRange.Count == 0) return;
|
||||
_fireTimer += (float)delta;
|
||||
if (!(_fireTimer >= 1f / _fireRate)) return;
|
||||
_fireTimer = 0;
|
||||
var t = Helpers.GetClosest(_parent,_enemiesInRange.ToArray());
|
||||
var dir = (t.GlobalPosition - _parent.GlobalPosition).Normalized();
|
||||
var proj = _projectilePool.Get();
|
||||
proj.Direction = dir;
|
||||
proj.GlobalPosition = _parent.GlobalPosition;
|
||||
proj.Rotation = dir.Angle();
|
||||
ProjectileParent.Instance.AddChild(proj);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user