2020-11-06 22:00:49 +00:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
using Random = System.Random;
|
|
|
|
|
|
|
|
public class EnemySpawner : MonoBehaviour
|
|
|
|
{
|
|
|
|
public Transform spawnPos;
|
|
|
|
public float timeBetweenSpawns;
|
|
|
|
public short maxEnemyCount;
|
2020-11-06 23:51:23 +00:00
|
|
|
public GameObject enemyPrefab;
|
2020-11-06 22:00:49 +00:00
|
|
|
public Transform target;
|
|
|
|
private Vector3 _diff;
|
|
|
|
private float _lastSpawn;
|
|
|
|
private readonly Random _random = new Random();
|
2020-11-06 23:51:23 +00:00
|
|
|
private ObjectPool _pool;
|
2020-11-06 22:00:49 +00:00
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_diff = spawnPos.position - transform.position;
|
|
|
|
enemyPrefab.GetComponent<EnemyController>().target = target;
|
2020-11-06 23:51:23 +00:00
|
|
|
_pool = new ObjectPool(enemyPrefab.gameObject, 10);
|
2020-11-06 22:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (Time.fixedTime - _lastSpawn > timeBetweenSpawns && _random.Next(2) == 1)
|
|
|
|
{
|
|
|
|
short count = (short) _random.Next(maxEnemyCount);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
var pos = transform.position + _diff;
|
2020-11-06 23:51:23 +00:00
|
|
|
var enemy = _pool.GetObject();
|
|
|
|
enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z);
|
|
|
|
var rb = enemy.GetComponent<Rigidbody2D>();
|
|
|
|
rb.mass = 1f;
|
|
|
|
rb.gravityScale = 1f;
|
|
|
|
rb.rotation = 0f;
|
|
|
|
rb.freezeRotation = true;
|
|
|
|
enemy.SetActive(true);
|
|
|
|
rb.rotation = 0f;
|
2020-11-06 22:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_lastSpawn = Time.fixedTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|