70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
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;
|
|
[Export] private AudioStreamPlayer2D _audio;
|
|
private float _fireTimer = 0f;
|
|
|
|
public int Level { get; private set; } = 1;
|
|
public int UpgradeCost => 31 + (int)(31 * Math.Pow(Level, 1.75f));
|
|
|
|
public float FireRate => _fireRate + (Level - 1) * 0.05f;
|
|
public float Damage => 25 + (int)(10 + Math.Pow(Level, 1.5f));
|
|
|
|
private HashSet<Enemy> _enemiesInRange = new HashSet<Enemy>();
|
|
|
|
public void Upgrade()
|
|
{
|
|
Level++;
|
|
}
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
_Attackrange.AreaEntered += EnemyEntered;
|
|
_Attackrange.AreaExited += EnemyExited;
|
|
}
|
|
|
|
private void EnemyEntered(Area2D enemyHitBox)
|
|
{
|
|
if (enemyHitBox is EnemyArea earea)
|
|
{
|
|
_enemiesInRange.Add(earea.Enemy);
|
|
}
|
|
}
|
|
|
|
private void EnemyExited(Area2D enemyHitBox)
|
|
{
|
|
if (enemyHitBox is EnemyArea earea)
|
|
{
|
|
_enemiesInRange.Remove(earea.Enemy);
|
|
}
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_enemiesInRange.Count == 0) return;
|
|
_fireTimer += (float)delta;
|
|
if (!(_fireTimer >= 1f / FireRate)) return;
|
|
_fireTimer = 0;
|
|
_audio?.SetPitchScale(RandomHelper.Float(0.8f, 1.2f));
|
|
_audio?.Play();
|
|
var t = Helpers.GetClosest(_parent,_enemiesInRange.ToArray());
|
|
var dir = (t.GlobalPosition - _parent.GlobalPosition).Normalized();
|
|
var proj = _projectilePool.Get();
|
|
proj.Direction = dir;
|
|
proj.Damage = Damage;
|
|
proj.GlobalPosition = _parent.GlobalPosition;
|
|
proj.Rotation = dir.Angle();
|
|
ProjectileParent.Instance.AddChild(proj);
|
|
}
|
|
}
|