UnityProjekt/Assets/Scripts/EnemySpawner.cs

51 lines
1.6 KiB
C#
Raw Normal View History

2020-11-06 22:00:49 +00:00
using System;
using System.Collections.Generic;
2020-11-06 22:00:49 +00:00
using UnityEngine;
using Random = System.Random;
public class EnemySpawner : MonoBehaviour
{
public Transform spawnPos;
public float timeBetweenSpawns;
public short maxEnemyCount;
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();
private ObjectPool _pool;
private PlatformSpawner _spawner;
2020-11-06 22:00:49 +00:00
private void Start()
{
_diff = spawnPos.position - target.position;
2020-11-06 22:00:49 +00:00
enemyPrefab.GetComponent<EnemyController>().target = target;
_pool = new ObjectPool(enemyPrefab.gameObject, 10);
_spawner = GetComponent<PlatformSpawner>();
2020-11-06 22:00:49 +00:00
}
private void FixedUpdate()
{
//int timeMultiplier = _spawner.maxLevel - _spawner.Level;
int countMultiplier = _spawner.Level + 1;
2020-11-06 22:00:49 +00:00
if (Time.fixedTime - _lastSpawn > timeBetweenSpawns && _random.Next(2) == 1)
{
short count = (short) _random.Next(maxEnemyCount * countMultiplier);
2020-11-06 22:00:49 +00:00
for (int i = 0; i < count; i++)
{
var pos = target.position + _diff;
var enemy = _pool.GetObject(true);
if (enemy is null) break;
enemy.transform.position = pos;
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;
}
}
}