GameVsJam/3d Prototyp/Assets/Scripts/ZombieSpawner.cs

78 lines
2.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utility;
public class ZombieSpawner : MonoBehaviour
{
[SerializeField]
List<Transform> SpawnPoints;
[SerializeField]
List<AudioClip> ZombieDeathByDisableSoundeffects;
[SerializeField]
GameObject ZombiePrefab;
[SerializeField]
2024-04-07 00:41:10 +02:00
private float _spawnRate = 2.0f;
[SerializeField, ShowOnly]
private float _spawnTimer;
2024-04-07 00:41:10 +02:00
private float _secondsPerAliveTime;
2024-04-07 00:12:15 +02:00
// Start wird aufgerufen, bevor das erste Frame gezeichnet wird
void Start()
{
2024-04-07 00:41:10 +02:00
_secondsPerAliveTime = GetComponentInParent<Zeitschaltuhr>().ActiveTimeSpanInSeconds;
_spawnTimer = Random.Range(0.5f * _secondsPerAliveTime / _spawnRate, _secondsPerAliveTime / _spawnRate);
}
// Update wird einmal pro Frame aufgerufen
void Update()
{
_spawnTimer -= Time.deltaTime;
if (_spawnTimer <= 0)
{
SpawnZombie();
2024-04-07 00:41:10 +02:00
_spawnTimer = _secondsPerAliveTime / _spawnRate;
2024-04-07 00:12:15 +02:00
}
}
private void OnEnable()
{
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 SpawnZombie()
{
Transform pos = SpawnPoints[Random.Range(0, SpawnPoints.Count)];
Instantiate(ZombiePrefab, pos.position, Quaternion.identity, transform);
}
2024-04-07 00:41:10 +02:00
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();
}
2024-04-07 00:41:10 +02:00
}
}