109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Utility;
|
|
|
|
public class ZombieSpawner : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
List<Transform> SpawnPoints;
|
|
[SerializeField]
|
|
public List<AudioClip> ZombieDeathByDisableSoundeffects;
|
|
[SerializeField]
|
|
GameObject ZombiePrefab;
|
|
[SerializeField]
|
|
private float _spawnRate = 2.0f;
|
|
[SerializeField]
|
|
private float _dontSpawnForLastSeconds = 3.0f;
|
|
[SerializeField, ShowOnly]
|
|
private float _spawnTimer;
|
|
[SerializeField, ShowOnly]
|
|
private float _enabledTimer = 0.0f;
|
|
|
|
private float _secondsPerAliveTime;
|
|
private bool _startedFading = false;
|
|
|
|
// Start wird aufgerufen, bevor das erste Frame gezeichnet wird
|
|
void Start()
|
|
{
|
|
// letzte _dontSpawnForLastSeconds sekunden keine Zombies mehr spawnen
|
|
_secondsPerAliveTime = GetComponentInParent<Zeitschaltuhr>().ActiveTimeSpanInSeconds - _dontSpawnForLastSeconds;
|
|
_spawnTimer = Random.Range(0.5f * _secondsPerAliveTime / _spawnRate, _secondsPerAliveTime / _spawnRate);
|
|
_enabledTimer = _secondsPerAliveTime + _dontSpawnForLastSeconds;
|
|
}
|
|
|
|
// Update wird einmal pro Frame aufgerufen
|
|
void Update()
|
|
{
|
|
_enabledTimer -= Time.deltaTime;
|
|
_spawnTimer -= Time.deltaTime;
|
|
|
|
// als übergang von Schrei zu Tröte
|
|
if (_enabledTimer < _dontSpawnForLastSeconds && !_startedFading)
|
|
{
|
|
_startedFading = true;
|
|
FadeOutNoises();
|
|
}
|
|
|
|
if (_spawnTimer <= 0)
|
|
{
|
|
SpawnZombie();
|
|
_spawnTimer = (_secondsPerAliveTime - _dontSpawnForLastSeconds) / _spawnRate;
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (_secondsPerAliveTime > 0)
|
|
{
|
|
_enabledTimer = _secondsPerAliveTime;
|
|
_startedFading = false;
|
|
}
|
|
if (GameManager.Instance != null)
|
|
{
|
|
if (GameManager.Instance.ContextBuffer != null)
|
|
GameManager.Instance.AddContext("The Developer informs Gottfried that Zombies appeared outside so Gottfried better not leave the office");
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (GameManager.Instance.ContextBuffer != null)
|
|
GameManager.Instance.RemoveContext("The Developer informs Gottfried that Zombies appeared outside so Gottfried better not leave the office");
|
|
// Destroy all children (zombies)
|
|
KillZombiesByDisable();
|
|
|
|
}
|
|
|
|
private void FadeOutNoises()
|
|
{
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
{
|
|
GameObject zombie = transform.GetChild(i).gameObject;
|
|
zombie.GetComponent<Zombie>().FadeOutNoise();
|
|
}
|
|
}
|
|
|
|
private void SpawnZombie()
|
|
{
|
|
if (transform.childCount > _spawnRate) return;
|
|
Transform pos = SpawnPoints[Random.Range(0, SpawnPoints.Count)];
|
|
Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 359), 0);
|
|
Instantiate(ZombiePrefab, pos.position, rotation, transform);
|
|
}
|
|
|
|
private void KillZombiesByDisable()
|
|
{
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
{
|
|
GameObject zombie = transform.GetChild(i).gameObject;
|
|
Vector3 zombiePosition = zombie.transform.position;
|
|
Destroy(zombie);
|
|
GameObject particleEffect = Instantiate(GameManager.Instance.ZombieDeathByDisableParticleEffect, zombiePosition, Quaternion.Euler(-90, 0, 0), GameManager.Instance.transform);
|
|
AudioSource audio = particleEffect.GetComponent<AudioSource>();
|
|
audio.clip = ZombieDeathByDisableSoundeffects[Random.Range(0, ZombieDeathByDisableSoundeffects.Count)];
|
|
audio.Play();
|
|
}
|
|
}
|
|
}
|