diff --git a/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs b/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs index 0d88a5c..ba19bdb 100644 --- a/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs +++ b/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using Unity.VisualScripting; using UnityEngine; +using UnityEngine.AI; using UnityEngine.UIElements; public class EnemySpawner : MonoBehaviour @@ -91,7 +92,7 @@ public class EnemySpawner : MonoBehaviour { while (difficulty > decendingList[i].Difficulty) { - GameObject enemy = Instantiate(decendingList[i].prefabs[UnityEngine.Random.Range(0, decendingList[i].prefabs.Length)], GetRandomPointOnCircle(mainCam.transform.position, SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform); + GameObject enemy = Instantiate(decendingList[i].prefabs[UnityEngine.Random.Range(0, decendingList[i].prefabs.Length)], GetRandomPointOnCircle(SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform); difficulty -= decendingList[i].Difficulty; enemy.GetComponent().targets = players.Select(x=>x.transform).ToArray(); @@ -112,22 +113,34 @@ public class EnemySpawner : MonoBehaviour int variant = UnityEngine.Random.Range(0, enemyToSpawn.prefabs.Length); GameObject variantToSpawn = enemyToSpawn.prefabs[variant]; - GameObject enemy = Instantiate(variantToSpawn, GetRandomPointOnCircle(mainCam.transform.position, SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform); + GameObject enemy = Instantiate(variantToSpawn, GetRandomPointOnCircle(SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform); enemy.GetComponent().targets = players.Select(x => x.transform).ToArray(); } } - public Vector3 GetRandomPointOnCircle(Vector3 location, float radius) + public Vector3 GetRandomPointOnCircle(float radius) { - float angle = UnityEngine.Random.Range(0f, 360f); - float radians = Mathf.Deg2Rad * angle; + Vector3 point; + do + { + float angle = UnityEngine.Random.Range(0f, 360f); + float radians = Mathf.Deg2Rad * angle; - Vector3 position = transform.position; - float x = position.x + radius * Mathf.Cos(radians); - float y = position.y + radius * Mathf.Sin(radians); + Vector3 position = transform.position; + float x = position.x + radius * Mathf.Cos(radians); + float y = position.y + radius * Mathf.Sin(radians); - return new Vector3(x, y, position.z); + point = new Vector3(x, y, position.z); + } while (IsPointOnNavMesh(point)); + + return point; + } + + bool IsPointOnNavMesh(Vector3 point) + { + NavMeshHit hit; + return NavMesh.SamplePosition(point, out hit, 0.1f, NavMesh.AllAreas); } #if UNITY_EDITOR @@ -138,7 +151,7 @@ public class EnemySpawner : MonoBehaviour for (int i = 0; i < 10; i++) { - Gizmos.DrawWireSphere(GetRandomPointOnCircle(transform.position, SpawnRadius), 0.25f); + Gizmos.DrawWireSphere(GetRandomPointOnCircle(SpawnRadius), 0.25f); } } #endif diff --git a/Assets/Scripts/Managers.meta b/Assets/Scripts/Managers.meta new file mode 100644 index 0000000..ec7787e --- /dev/null +++ b/Assets/Scripts/Managers.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cd53cfafc9fa2ae49b54519de9b569c8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Managers/AudioManager.meta b/Assets/Scripts/Managers/AudioManager.meta new file mode 100644 index 0000000..048d81b --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c4df8d80eeaafb345befbd4a6c551b54 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs b/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs new file mode 100644 index 0000000..556a284 --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs @@ -0,0 +1,10 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO.Enumeration; +using UnityEngine; + +[CreateAssetMenu(fileName = "New AudioLibrary", menuName = "ZUtility/AudioLibrary")] +public class AudioLibraryObject : ScriptableObject +{ + public AudioClip[] Clips; +} diff --git a/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs.meta b/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs.meta new file mode 100644 index 0000000..b8d7da9 --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager/AudioLibraryObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aaa6f7237ea22dc49a4eb9e830e9d69a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Managers/AudioManager/AudioManager.cs b/Assets/Scripts/Managers/AudioManager/AudioManager.cs new file mode 100644 index 0000000..6954455 --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager/AudioManager.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + + +//Photon/pun 2 lightly dependent +public class AudioManager : MonoBehaviour +{ + public static AudioManager Instance; + public AudioLibraryObject audioLibrary; + + + private void OnEnable() + { + if (Instance is null) + { + Instance = this; + // TODO: manager itself should not handle this + // DontDestroyOnLoad(this); + } + else + { + Destroy(this); + } + } + + private void OnDisable() + { + Instance = null; + } + + public static AudioClip FindAudioClip(string nameOfClip) + { + return Instance.audioLibrary.Clips.ToList().Find(x => x.name == nameOfClip); + } + + public static AudioSource PlaySound(string nameOfClip, Vector3 sourcePos, bool is3D, bool loop = false, bool fadeIn = false) + { + if (Instance == null) + { + Debug.LogWarning("AudioManager has not been initialized. Please initialize it before use."); + return null; + } + + var sourceObject = new GameObject("SoundSource"); + var audioSource = sourceObject.AddComponent(); + audioSource.loop = loop; + audioSource.clip = FindAudioClip(nameOfClip); + sourceObject.transform.position = sourcePos; + + if (is3D) + { + audioSource.rolloffMode = AudioRolloffMode.Linear; + audioSource.spatialBlend = 1f; + } + + Instance.StartCoroutine(Instance.StartSound(audioSource, fadeIn)); + return audioSource; + } + + public static List AliveSources = new List(); + IEnumerator StartSound(AudioSource audioSource, bool fadeIn = false) + { + AliveSources.Add(new SoundSource(audioSource)); + + if (fadeIn) + { + audioSource.volume = 0; + StartCoroutine(StartMusicFadeIn(audioSource)); + } + + + audioSource.Play(); + yield return new WaitUntil(() => !audioSource.isPlaying); + Destroy(audioSource.gameObject); + } + + public IEnumerator StartMusicFadeIn(AudioSource AS) + { + while (true) + { + if (AS.volume >= 1) + { + break; + } + AS.volume += 0.01f; + yield return new WaitForSecondsRealtime(0.05f); + } + } +} + +public class SoundSource +{ + public string Id { get; set; } + public AudioSource source { get; set; } + public SoundSource(AudioSource originalSource) + { + source = originalSource; + } +} diff --git a/Assets/Scripts/Managers/AudioManager/AudioManager.cs.meta b/Assets/Scripts/Managers/AudioManager/AudioManager.cs.meta new file mode 100644 index 0000000..bdb117a --- /dev/null +++ b/Assets/Scripts/Managers/AudioManager/AudioManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d07fc52b48ce953488ecef079872f5b7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: