ReisenYoumuOuting/Assets/Scripts/Abilities/YoumuDeflect.cs
2026-06-27 04:59:03 -07:00

143 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;
public class YoumuDeflect : Ability
{
private List<Projectile> projectilesInRange = new();
public float deflectionAngleMin = -48;
public float deflectionAngleMax = 90;
[Header("SlashCharge")]
[SerializeField] private float mouseHoldTime;
private float currentMouseHoldTime;
private float currentChargeTime;
private bool isCharging;
[SerializeField] private float maxChargeTime;
[SerializeField] private float chargeCooldown;
private float currentChargeCooldown;
private float currentUnleashedChargeTime;
[Header("Effects")]
[SerializeField] private ParticleSystem deflectionParticles;
[SerializeField] private GameObject swordSweepEffect;
[SerializeField] private GameObject chargeSweepEffect;
private float currentDissipationTime = 0f;
[SerializeField] private float dissipationTime;
protected override void AbilityEffects()
{
base.AbilityEffects();
if (currentChargeCooldown > 0)
{
RegularDeflection();
}
else
{
currentMouseHoldTime = mouseHoldTime;
currentChargeTime = 0;
}
}
private void RegularDeflection()
{
//Debug.Log("Regular Deflection");
swordSweepEffect.gameObject.SetActive(true);
currentDissipationTime = dissipationTime;
DeflectProjectiles();
}
private void DeflectProjectiles()
{
foreach (Projectile projectile in projectilesInRange.ToList())
{
if (!projectile)
{
projectilesInRange.Remove(projectile);
continue;
}
projectile.transform.eulerAngles = new Vector3(0, 0, Random.Range(deflectionAngleMin, deflectionAngleMax));
projectile.direction = projectile.transform.right;
projectile.tag = tag;
ParticleSystem newParticles = Instantiate(deflectionParticles, projectile.transform);
projectilesInRange.Remove(projectile);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag(tag) && other.TryGetComponent(out Projectile isProjectile))
{
projectilesInRange.Add(isProjectile);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (!other.CompareTag(tag) && other.TryGetComponent(out Projectile isProjectile) && projectilesInRange.Contains(isProjectile))
{
projectilesInRange.Add(isProjectile);
}
}
protected override void Update()
{
base.Update();
if (currentDissipationTime > 0)
{
currentDissipationTime -= Time.deltaTime;
if (currentDissipationTime <= 0)
{
swordSweepEffect.SetActive(false);
}
}
if (currentMouseHoldTime >= 0)
{
currentMouseHoldTime -= Time.deltaTime;
if (currentMouseHoldTime <= 0 && Input.GetMouseButton(1) && currentChargeTime <= 0)
{
isCharging = true;
}
else if (Input.GetMouseButtonUp(1))
{
RegularDeflection();
}
}
if (isCharging && Input.GetMouseButton(1)) //this code REEKS
{
currentChargeTime += Time.deltaTime;
}
else if (isCharging && Input.GetMouseButtonUp(1))
{
Debug.Log("here");
chargeSweepEffect.SetActive(true);
currentUnleashedChargeTime = Math.Clamp(currentChargeTime, 0, maxChargeTime) * 2;
currentChargeCooldown = chargeCooldown;
isCharging = false;
}
else if (currentUnleashedChargeTime > 0)
{
currentUnleashedChargeTime -= Time.deltaTime;
if (currentUnleashedChargeTime < 0)
{
chargeSweepEffect.SetActive(false);
}
if (projectilesInRange.Count > 0)
{
DeflectProjectiles();
}
}
if (currentChargeCooldown > 0)
{
currentChargeCooldown -= Time.deltaTime;
}
Debug.Log(currentChargeTime);
}
}