2024-04-06 18:30:26 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Utility;
|
|
|
|
|
|
|
|
|
|
public class ZombieSpawner : MonoBehaviour
|
|
|
|
|
{
|
2024-04-07 01:22:42 +02:00
|
|
|
|
[SerializeField]
|
|
|
|
|
List<Transform> SpawnPoints;
|
|
|
|
|
[SerializeField]
|
2024-04-08 18:57:32 +02:00
|
|
|
|
public List<AudioClip> ZombieDeathByDisableSoundeffects;
|
2024-04-06 18:30:26 +02:00
|
|
|
|
[SerializeField]
|
|
|
|
|
GameObject ZombiePrefab;
|
|
|
|
|
[SerializeField]
|
2024-04-07 00:41:10 +02:00
|
|
|
|
private float _spawnRate = 2.0f;
|
2024-04-07 03:38:32 +02:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private float _dontSpawnForLastSeconds = 3.0f;
|
2024-04-06 18:30:26 +02:00
|
|
|
|
[SerializeField, ShowOnly]
|
|
|
|
|
private float _spawnTimer;
|
2024-04-07 03:38:32 +02:00
|
|
|
|
[SerializeField, ShowOnly]
|
|
|
|
|
private float _enabledTimer = 0.0f;
|
2024-04-06 18:30:26 +02:00
|
|
|
|
|
2024-04-07 00:41:10 +02:00
|
|
|
|
private float _secondsPerAliveTime;
|
2024-04-07 03:38:32 +02:00
|
|
|
|
private bool _startedFading = false;
|
2024-04-07 00:12:15 +02:00
|
|
|
|
|
2024-04-06 18:30:26 +02:00
|
|
|
|
// Start wird aufgerufen, bevor das erste Frame gezeichnet wird
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2024-04-07 03:38:32 +02:00
|
|
|
|
// letzte _dontSpawnForLastSeconds sekunden keine Zombies mehr spawnen
|
|
|
|
|
_secondsPerAliveTime = GetComponentInParent<Zeitschaltuhr>().ActiveTimeSpanInSeconds - _dontSpawnForLastSeconds;
|
2024-04-07 00:41:10 +02:00
|
|
|
|
_spawnTimer = Random.Range(0.5f * _secondsPerAliveTime / _spawnRate, _secondsPerAliveTime / _spawnRate);
|
2024-04-07 03:38:32 +02:00
|
|
|
|
_enabledTimer = _secondsPerAliveTime + _dontSpawnForLastSeconds;
|
2024-04-06 18:30:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update wird einmal pro Frame aufgerufen
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2024-04-07 03:38:32 +02:00
|
|
|
|
_enabledTimer -= Time.deltaTime;
|
2024-04-06 18:30:26 +02:00
|
|
|
|
_spawnTimer -= Time.deltaTime;
|
|
|
|
|
|
2024-04-07 03:38:32 +02:00
|
|
|
|
// als <20>bergang von Schrei zu Tr<54>te
|
|
|
|
|
if (_enabledTimer < _dontSpawnForLastSeconds && !_startedFading)
|
|
|
|
|
{
|
|
|
|
|
_startedFading = true;
|
|
|
|
|
FadeOutNoises();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-06 18:30:26 +02:00
|
|
|
|
if (_spawnTimer <= 0)
|
|
|
|
|
{
|
2024-04-07 01:22:42 +02:00
|
|
|
|
SpawnZombie();
|
2024-04-07 03:38:32 +02:00
|
|
|
|
_spawnTimer = (_secondsPerAliveTime - _dontSpawnForLastSeconds) / _spawnRate;
|
2024-04-07 00:12:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2024-04-07 03:38:32 +02:00
|
|
|
|
if (_secondsPerAliveTime > 0)
|
|
|
|
|
{
|
|
|
|
|
_enabledTimer = _secondsPerAliveTime;
|
|
|
|
|
_startedFading = false;
|
|
|
|
|
}
|
2024-04-07 00:12:15 +02:00
|
|
|
|
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)
|
2024-04-07 01:22:42 +02:00
|
|
|
|
KillZombiesByDisable();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 03:38:32 +02:00
|
|
|
|
private void FadeOutNoises()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
GameObject zombie = transform.GetChild(i).gameObject;
|
|
|
|
|
zombie.GetComponent<Zombie>().FadeOutNoise();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 01:22:42 +02:00
|
|
|
|
private void SpawnZombie()
|
|
|
|
|
{
|
2024-04-07 03:38:32 +02:00
|
|
|
|
if (transform.childCount > _spawnRate) return;
|
2024-04-07 01:22:42 +02:00
|
|
|
|
Transform pos = SpawnPoints[Random.Range(0, SpawnPoints.Count)];
|
2024-04-08 18:57:32 +02:00
|
|
|
|
Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 359), 0);
|
|
|
|
|
Instantiate(ZombiePrefab, pos.position, rotation, transform);
|
2024-04-06 18:30:26 +02:00
|
|
|
|
}
|
2024-04-07 00:41:10 +02:00
|
|
|
|
|
|
|
|
|
private void KillZombiesByDisable()
|
|
|
|
|
{
|
2024-04-07 01:22:42 +02:00
|
|
|
|
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();
|
|
|
|
|
}
|
2024-04-07 00:41:10 +02:00
|
|
|
|
}
|
2024-04-06 18:30:26 +02:00
|
|
|
|
}
|