Update EnemySpawner.cs
This commit is contained in:
parent
7800d171e7
commit
2790611e58
|
@ -6,6 +6,7 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Unity.VisualScripting;
|
using Unity.VisualScripting;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.AI;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
public class EnemySpawner : MonoBehaviour
|
public class EnemySpawner : MonoBehaviour
|
||||||
|
@ -91,7 +92,7 @@ public class EnemySpawner : MonoBehaviour
|
||||||
{
|
{
|
||||||
while (difficulty > decendingList[i].Difficulty)
|
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;
|
difficulty -= decendingList[i].Difficulty;
|
||||||
|
|
||||||
enemy.GetComponent<EnemyPathFinding>().targets = players.Select(x=>x.transform).ToArray();
|
enemy.GetComponent<EnemyPathFinding>().targets = players.Select(x=>x.transform).ToArray();
|
||||||
|
@ -112,22 +113,34 @@ public class EnemySpawner : MonoBehaviour
|
||||||
int variant = UnityEngine.Random.Range(0, enemyToSpawn.prefabs.Length);
|
int variant = UnityEngine.Random.Range(0, enemyToSpawn.prefabs.Length);
|
||||||
GameObject variantToSpawn = enemyToSpawn.prefabs[variant];
|
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<EnemyPathFinding>().targets = players.Select(x => x.transform).ToArray();
|
enemy.GetComponent<EnemyPathFinding>().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);
|
Vector3 point;
|
||||||
float radians = Mathf.Deg2Rad * angle;
|
do
|
||||||
|
{
|
||||||
|
float angle = UnityEngine.Random.Range(0f, 360f);
|
||||||
|
float radians = Mathf.Deg2Rad * angle;
|
||||||
|
|
||||||
Vector3 position = transform.position;
|
Vector3 position = transform.position;
|
||||||
float x = position.x + radius * Mathf.Cos(radians);
|
float x = position.x + radius * Mathf.Cos(radians);
|
||||||
float y = position.y + radius * Mathf.Sin(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
|
#if UNITY_EDITOR
|
||||||
|
@ -138,7 +151,7 @@ public class EnemySpawner : MonoBehaviour
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
Gizmos.DrawWireSphere(GetRandomPointOnCircle(transform.position, SpawnRadius), 0.25f);
|
Gizmos.DrawWireSphere(GetRandomPointOnCircle(SpawnRadius), 0.25f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cd53cfafc9fa2ae49b54519de9b569c8
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c4df8d80eeaafb345befbd4a6c551b54
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -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;
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: aaa6f7237ea22dc49a4eb9e830e9d69a
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -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>();
|
||||||
|
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<SoundSource> AliveSources = new List<SoundSource>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d07fc52b48ce953488ecef079872f5b7
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue