ReisenYoumuOuting/Assets/Scripts/Abilities/CirnoFreezeDeflect.cs

104 lines
2.9 KiB
C#

using System.Collections.Generic;
using Core.Extensions;
using UnityEngine;
public class CirnoFreezeDeflect : EnemyAbility
{
[Header("Direction")]
private Vector3 playerPos;
[Header("Deflection")]
[SerializeField] private float deflectionSpeed;
[SerializeField] private float deflectionSpeedVariance;
[SerializeField] private float deflectionAngleMin = -48;
[SerializeField] private float deflectionAngleMax = 90;
public List<Projectile> projectilesInRange = new();
public List<Projectile> projectilesAffected = new();
[SerializeField] private float deflectionDelay;
private float currentDelay;
[Header("Effects")]
[SerializeField] private ParticleSystem deflectionParticles;
[SerializeField] private AudioClip deflectionSound;
[SerializeField] private float sweepDissipationTime;
private float currentDissipationTime;
[SerializeField] private GameObject swordSweepEffect;
[SerializeField] private Color freezeColor;
protected override void Start()
{
base.Start();
playerPos = WaveManager.instance.GetRandomPlayerPoint();
}
protected override void AbilityEffects()
{
projectilesAffected = projectilesInRange;
foreach (Projectile projectile in projectilesAffected.ToArray())
{
if (!projectile)
{
projectilesInRange.Remove(projectile);
continue;
}
projectile.sprite.color = freezeColor;
projectile.speed = 0;
}
currentDelay = deflectionDelay;
playerPos = WaveManager.instance.GetRandomPlayerPoint();
}
protected override void Update()
{
base.Update();
transform.Lookat2D(playerPos);
if (currentDelay > 0)
{
currentDelay -= Time.deltaTime;
if (currentDelay <= 0)
{
DeflectProjectiles();
currentDissipationTime = sweepDissipationTime;
swordSweepEffect.SetActive(true);
}
}
if (currentDissipationTime > 0)
{
currentDissipationTime -= Time.deltaTime;
if (currentDissipationTime <= 0)
{
swordSweepEffect.SetActive(false);
}
}
}
private void DeflectProjectiles()
{
GameManager.instance.deflections += projectilesInRange.Count;
foreach (Projectile projectile in projectilesAffected.ToArray())
{
if (!projectile)
{
projectilesAffected.Remove(projectile);
continue;
}
projectile.transform.eulerAngles = new Vector3(0, 0, Random.Range(deflectionAngleMin, deflectionAngleMax));
projectile.direction = projectile.transform.right;
projectile.tag = tag;
projectile.speed = deflectionSpeed + Random.Range(-deflectionSpeedVariance, deflectionSpeedVariance);
projectile.Deflected();
Instantiate(deflectionParticles, projectile.transform);
}
projectilesAffected.Clear();
WaveManager.instance.audioSource.PlayOneShot(deflectionSound, volume);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag(tag))
{
if (other.TryGetComponent(out Projectile isProjectile) && isProjectile.deflectable)
{
projectilesInRange.Add(isProjectile);
}
}
}
}