using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using UnityEngine; public class SpellVisualEffects : MonoBehaviour { public enum EffectLocation { SelfLocation, TargettedLocation, TravelSelfToTarget, TravelTargetToSelf, AllSide, AllSideEachLocation, AllRightLocations, AllRightCenter, AllLeftLocations, AllLeftCenter, TopOfScreen, CenterOfScreen, BottomOfScreen } [Header("Effect Objects")] [SerializeField] private GameObject[] effectSequence; [SerializeField] private EffectLocation[] effectLocations; [SerializeField] private AudioClip[] soundEffects; private Transform[][] locationSequence; private PositionHandler positionHandler; private void Awake() { positionHandler = PositionHandler.Instance; } public void GatherLocationSequence(int castingLocation = 0, int targetLocation = 0, bool wasEnemyCast = false, bool isTargettingSameSide = false) { locationSequence = new Transform[effectLocations.Length][]; for (int i = 0; i < effectLocations.Length; ++i) { locationSequence[i] = new Transform[positionHandler.GatherLocationCount((int)effectLocations[i], wasEnemyCast)]; locationSequence[i] = positionHandler.GatherLocation(effectLocations[i], castingLocation, targetLocation, wasEnemyCast, isTargettingSameSide).ToArray(); } } public async Task BeginEffectSequence(bool isEnemyCast) { for (int i = 0; i < effectSequence.Length; ++i) await EffectCompletion(locationSequence[i], effectSequence[i], soundEffects[i], isEnemyCast); Destroy(this.gameObject); } private async Task EffectCompletion(Transform[] spawnArea, GameObject effect, AudioClip soundEffect, bool isEnemyCast) { GameObject[] instancedEffects = new GameObject[spawnArea.Length]; for (int i = 0; i < spawnArea.Length; ++i) { instancedEffects[i] = Instantiate(effect, spawnArea[i]); if (instancedEffects[i].TryGetComponent(out Rigidbody2D effectBody)) { if (isEnemyCast) { instancedEffects[i].GetComponent<SpriteRenderer>().flipX = true; effectBody.velocity = new Vector2(-4.75f, 0f); } else effectBody.velocity = new Vector2(4.75f, 0f); } } // All of the effects instanced should be the same length SoundManager.Instance.PlaySoundFX(soundEffect); float animationDuration = instancedEffects[0].GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length; await Task.Delay((int)(animationDuration * 1000)); foreach (GameObject garbageObject in instancedEffects) Destroy(garbageObject); } }