Skip to content

Commit

Permalink
started basic object pooling functionality
Browse files Browse the repository at this point in the history
Signed-off-by: rodude123 <[email protected]>
  • Loading branch information
rodude123 committed Mar 13, 2022
1 parent 6e61dd4 commit 2732260
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
24 changes: 22 additions & 2 deletions Assets/Scripts/Enemy/Enemy.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using Enemy.Configurators;
using Player;
using UnityEngine;
Expand All @@ -18,11 +20,14 @@ public class Enemy : MonoBehaviour
private Animator _anim;

private FPSPlayer _player;

private EnemyManager _enemyManager;
private double _previousAttackTime;

private void Start()
{
_player = EnemyManager.Instance.player;
_enemyManager = EnemyManager.Instance;
_player = _enemyManager.player;
_agent = GetComponent<NavMeshAgent>();
_anim = GetComponent<Animator>();
}
Expand Down Expand Up @@ -63,8 +68,23 @@ public void TakeDamage(float damage)
}
_anim.SetBool(zombieAiConfig.dieParameterName, true);
_agent.isStopped = true;
Destroy(gameObject, 15f);
StartCoroutine(nameof(Disable));
Debug.Log("Enemy died");
}

private IEnumerator Disable()
{
yield return new WaitForSeconds(15f);
gameObject.SetActive(false);
}

public void OnEnable()
{
gameObject.SetActive(true);
_agent.isStopped = false;
_agent.SetDestination(_player.transform.position);
transform.position = _enemyManager.spawnPoints[Random.Range(0, _enemyManager.spawnPoints.Count)].transform.position;
}

}
}
52 changes: 39 additions & 13 deletions Assets/Scripts/Enemy/EnemyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public class EnemyManager : MonoBehaviour

[ConditionalHide(true, false, "spawnMode", "spawnSystem", "enableGUI")]
public Color32 textColour;
private GameObject counterGb;
private int currRound;
private GameObject _counterGb;
private int _currRound;

private readonly List<GameObject> enemiesSpawned = new List<GameObject>();
private readonly List<GameObject> _enemiesSpawned = new List<GameObject>();

public static EnemyManager Instance
{
Expand Down Expand Up @@ -110,21 +110,36 @@ private void Update()
{
if (enableGUI)
{
counterGb.GetComponent<TextMeshProUGUI>().text = "Round: " + (currRound + 1);
_counterGb.GetComponent<TextMeshProUGUI>().text = "Round: " + (_currRound + 1);
}
}

public void SpawnEnemy()
{
enemiesSpawned.Add(Instantiate(enemiesToSpawn[Random.Range(0, enemiesToSpawn.Count)],
if (_enemiesSpawned.Count > 5)
{
for (var i = 0; i < _enemiesSpawned.Count; i++)
{
if (_enemiesSpawned[i].activeInHierarchy == false)
{
_enemiesSpawned[i].SetActive(true);
break;
}
}
}

_enemiesSpawned.Add(Instantiate(enemiesToSpawn[Random.Range(0, enemiesToSpawn.Count)],
spawnPoints[Random.Range(0, spawnPoints.Count)].transform.position, Quaternion.identity));
}

private IEnumerator SpawnEnemies()
{
while (spawnSystem == SpawnSystem.Continuous)
{
for (var i = 0; i < 10; i++) SpawnEnemy();
for (var i = 0; i < 10; i++)
{
SpawnEnemy();
}
yield return new WaitForSeconds(timeBetweenSpawnsSeconds);
}
}
Expand All @@ -135,23 +150,34 @@ private IEnumerator RoundBased()
{
for (var i = 0; i < numberOfRounds; i++)
{
currRound = i;
for (var j = 0; j < enemiesPerRound; j++) SpawnEnemy();
_currRound = i;
for (var j = 0; j < enemiesPerRound; j++)
{
SpawnEnemy();
}

if (useTime)
{
yield return new WaitForSeconds(timeBetweenSpawnsSeconds);
}
}
yield break;
}
else

if (numberOfRounds == 0)
{
while (true)
{
for (var i = 0; i < enemiesPerRound; i++) SpawnEnemy();
for (var i = 0; i < enemiesPerRound; i++)
{
SpawnEnemy();
}
yield return new WaitForSeconds(timeBetweenSpawnsSeconds);
}
}



}

private IEnumerator RoundCounterUI()
Expand All @@ -174,10 +200,10 @@ private IEnumerator RoundCounterUI()
yield return new WaitForSecondsRealtime(1f);
Time.timeScale = 1;
Destroy(countInGb);
counterGb = new GameObject();
var counterTextMesh = CreateTextElement(counterGb,
_counterGb = new GameObject();
var counterTextMesh = CreateTextElement(_counterGb,
new Vector2(Screen.width * 0.125f, Screen.height * 0.875f), 32, "Round: 1");
counterGb.transform.SetParent(canvasUI.transform);
_counterGb.transform.SetParent(canvasUI.transform);
}

private TextMeshProUGUI CreateTextElement(GameObject gameObj, Vector2 pos, int fontSize, string textStr)
Expand Down

0 comments on commit 2732260

Please sign in to comment.