Enemy spawner
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using Unity.VisualScripting.FullSerializer;
|
||||
using UnityEngine;
|
||||
|
||||
[
|
||||
@@ -18,6 +17,14 @@ public class Ennemy : MonoBehaviour
|
||||
|
||||
[SerializeField] private GameObject _orb;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (transform.parent != null)
|
||||
{
|
||||
transform.parent = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_sr = GetComponent<SpriteRenderer>();
|
||||
|
||||
@@ -18,6 +18,7 @@ GameObject:
|
||||
- component: {fileID: 7397454076540391921}
|
||||
- component: {fileID: 8339747699126584523}
|
||||
- component: {fileID: 2991086602376010793}
|
||||
- component: {fileID: 4761054675786854393}
|
||||
m_Layer: 0
|
||||
m_Name: Player
|
||||
m_TagString: Player
|
||||
@@ -267,9 +268,13 @@ MonoBehaviour:
|
||||
Speed: 2
|
||||
Exp: 0
|
||||
Level: 1
|
||||
OrbAbsorberRadius: 0.7
|
||||
BeamDamages: 50
|
||||
BeamFequency: 5
|
||||
BeamFrequency: 5
|
||||
BeamLife: 0.5
|
||||
SwordsHaloRadius: 1
|
||||
SwordsDamages: 25
|
||||
SwordsQuantity: 5
|
||||
ArrowDamages: 15
|
||||
ArrowRange: 5
|
||||
ArrowFrequency: 2
|
||||
@@ -295,6 +300,21 @@ MonoBehaviour:
|
||||
m_EditorClassIdentifier:
|
||||
laser: {fileID: 4893257253370123985, guid: 95cb513377bf0e248bd66d6f7ddae920, type: 3}
|
||||
arrow: {fileID: 6280786298679187013, guid: 856a449688373494987701d49b79a7af, type: 3}
|
||||
--- !u!114 &4761054675786854393
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1784956759114531697}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: e6db2d9573175344fa4455c70d440232, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
Mayor: {fileID: 0}
|
||||
Minautor: {fileID: 0}
|
||||
BringerOfDeath: {fileID: 0}
|
||||
--- !u!1001 &993819705047970090
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class Spawner : MonoBehaviour
|
||||
{
|
||||
public GameObject Mayor;
|
||||
public GameObject Minautor;
|
||||
public GameObject BringerOfDeath;
|
||||
|
||||
private float _radius = 5f;
|
||||
|
||||
private int spawnedMayors = 0;
|
||||
private int spawnedMinautor = 0;
|
||||
private int spawnedBringerOfDeath = 0;
|
||||
|
||||
private readonly int mayorsLimit = 300;
|
||||
private readonly int minautorsLimit = 15;
|
||||
private readonly int bringerOfDeathLimit = 5;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_radius = Camera.main.orthographicSize * Camera.main.aspect + 2f;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InvokeRepeating(nameof(SpawnWave), 5f, 5f);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
|
||||
Gizmos.DrawWireSphere(transform.position, Camera.main.orthographicSize * Camera.main.aspect + 2f);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
spawnedMayors = Array.FindAll(GameObject.FindGameObjectsWithTag("Ennemy"), (value) => value.name.Contains("Mayor")).Length;
|
||||
spawnedMinautor = Array.FindAll(GameObject.FindGameObjectsWithTag("Ennemy"), (value) => value.name.Contains("Minautor")).Length;
|
||||
spawnedBringerOfDeath = Array.FindAll(GameObject.FindGameObjectsWithTag("Ennemy"), (value) => value.name.Contains("Bringer of Death")).Length;
|
||||
}
|
||||
|
||||
private void SpawnWave()
|
||||
{
|
||||
// Determine how much enemies will spawn on this wave
|
||||
int enemiesAmount = Random.Range(20, 30);
|
||||
|
||||
for (int i = 0; i < enemiesAmount; i++)
|
||||
{
|
||||
GameObject enemyToSpawn = ChooseEnemy();
|
||||
string enemyName = enemyToSpawn.name.Split(' ')[0];
|
||||
|
||||
int retries = 0;
|
||||
|
||||
while (
|
||||
(enemyName == "Mayor" && spawnedMayors >= mayorsLimit) ||
|
||||
(enemyName == "Minautor" && spawnedMinautor >= minautorsLimit) ||
|
||||
(enemyName == "Bringer of Death" && spawnedBringerOfDeath >= bringerOfDeathLimit)
|
||||
)
|
||||
{
|
||||
enemyToSpawn = ChooseEnemy();
|
||||
enemyName = enemyToSpawn.name.Split(' ')[0];
|
||||
|
||||
retries++;
|
||||
if (retries >= 20)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (enemyName)
|
||||
{
|
||||
case "Mayor":
|
||||
spawnedMayors++;
|
||||
break;
|
||||
case "Minautor":
|
||||
spawnedMinautor++;
|
||||
break;
|
||||
case "Bringer of Death":
|
||||
spawnedBringerOfDeath++;
|
||||
break;
|
||||
}
|
||||
|
||||
// Determine the spawn position on a circle around the player
|
||||
float angle = Mathf.PI * 2f / Random.Range(0f, 40f);
|
||||
Instantiate(
|
||||
enemyToSpawn,
|
||||
transform.position + new Vector3(Mathf.Cos(Random.Range(0f, 10f) * angle) * _radius, Mathf.Sin(Random.Range(0f, 10f) * angle) * _radius, 0),
|
||||
Quaternion.identity,
|
||||
transform
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Determine which enemy to spawn based on chance percentage
|
||||
private GameObject ChooseEnemy()
|
||||
{
|
||||
int enemyChance = Random.Range(0, 101);
|
||||
if (enemyChance > 80 && enemyChance <= 98)
|
||||
{
|
||||
return Minautor;
|
||||
}
|
||||
else if (enemyChance > 98 && enemyChance <= 100)
|
||||
{
|
||||
return BringerOfDeath;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Mayor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e6db2d9573175344fa4455c70d440232
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user