162 lines
4.7 KiB
C#
162 lines
4.7 KiB
C#
|
using Assets.Scripts;
|
||
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using static GameHandler;
|
||
|
|
||
|
public class LevelSpawner : SingletonMB<LevelSpawner>
|
||
|
{
|
||
|
|
||
|
#region PROPERTIES
|
||
|
#endregion
|
||
|
#region VARIABLES
|
||
|
private GameHandler _gameHandler;
|
||
|
|
||
|
|
||
|
[SerializeField] private GameObject[] patternsPrefabs;
|
||
|
[SerializeField] private Transform levelSpawner;
|
||
|
[SerializeField] private float patternInterval;
|
||
|
[SerializeField] private GameObject PlayersContainer;
|
||
|
[SerializeField] private GameObject missilePrefab;
|
||
|
[SerializeField] private GameObject missileAlertPrefab;
|
||
|
[SerializeField] private float dangerMinX = -25f;
|
||
|
|
||
|
private List<MissileAlertInfo> missileAlertList = new List<MissileAlertInfo>();
|
||
|
private List<Transform> spawnedGameElements = new List<Transform>();
|
||
|
private GameObject prefabParent;
|
||
|
private float patternLength;
|
||
|
private ObstaclePatternBehaviour lastSpawnedPattern;
|
||
|
private float rewardValue = 0f;
|
||
|
|
||
|
#endregion
|
||
|
#region EVENTS
|
||
|
|
||
|
public event EventHandler<PrefabSpawnedEventArgs> OnPrefabSpawned;
|
||
|
|
||
|
|
||
|
public class PrefabSpawnedEventArgs : EventArgs
|
||
|
{
|
||
|
public ObstaclePatternBehaviour pattern;
|
||
|
public PrefabSpawnedEventArgs(ObstaclePatternBehaviour pattern)
|
||
|
{
|
||
|
this.pattern = pattern;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void ResetLevel_OnAttemptEnding(object sender, AttemptEventArgs e)
|
||
|
{
|
||
|
foreach (Transform gameElement in spawnedGameElements)
|
||
|
{
|
||
|
if (gameElement != null)
|
||
|
{
|
||
|
Destroy(gameElement.gameObject);
|
||
|
}
|
||
|
}
|
||
|
if(lastSpawnedPattern)Destroy(lastSpawnedPattern.gameObject);
|
||
|
lastSpawnedPattern = null;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
#region METHODS
|
||
|
|
||
|
void PrefabSpawnerBehaviour()
|
||
|
{
|
||
|
if (lastSpawnedPattern == null)
|
||
|
{
|
||
|
SpawnObstacle();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SpawnObstacle()
|
||
|
{
|
||
|
GameObject obstacleToSpawn = patternsPrefabs[UnityEngine.Random.Range(0, patternsPrefabs.Length)];
|
||
|
patternLength = obstacleToSpawn.transform.lossyScale.x;
|
||
|
lastSpawnedPattern = Instantiate(obstacleToSpawn, transform.position, Quaternion.identity, prefabParent.transform)
|
||
|
.GetComponent<ObstaclePatternBehaviour>();
|
||
|
|
||
|
OnPrefabSpawned?.Invoke(this, new PrefabSpawnedEventArgs(lastSpawnedPattern));
|
||
|
}
|
||
|
|
||
|
//IEnumerator SpawnMissileAfterDelay(GameObject missileAlert, GameObject player)
|
||
|
//{
|
||
|
// yield return new WaitForSeconds(3f);
|
||
|
// Vector3 missileSpawnPosition = missileAlert.transform.position + new Vector3(2.5f, 0, 0);
|
||
|
// Destroy(missileAlert);
|
||
|
// GameObject newMissile = Instantiate(missilePrefab, missileSpawnPosition, Quaternion.identity, prefabParent.transform);
|
||
|
// MissileScript missileScript = newMissile.GetComponent<MissileScript>();
|
||
|
// missileScript.Target = player;
|
||
|
// spawnedGameElements.Add(newMissile.transform);
|
||
|
//}
|
||
|
|
||
|
private void RemoveExpiredGameElements()
|
||
|
{
|
||
|
List<Transform> gameElementToRemove = new List<Transform>();
|
||
|
|
||
|
foreach (Transform gameElementTransform in spawnedGameElements)
|
||
|
{
|
||
|
if (gameElementTransform == null)
|
||
|
{
|
||
|
gameElementToRemove.Add(gameElementTransform);
|
||
|
continue;
|
||
|
}
|
||
|
if (gameElementTransform.position.x < dangerMinX)
|
||
|
{
|
||
|
gameElementToRemove.Add(gameElementTransform);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach (Transform gameElement in gameElementToRemove)
|
||
|
{
|
||
|
if (gameElement != null)
|
||
|
{
|
||
|
Destroy(gameElement.gameObject);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//private void UpdateMissileAlertPositions()
|
||
|
//{
|
||
|
// missileAlertList.RemoveAll(item => item.alertTransform == null);
|
||
|
// foreach (MissileAlertInfo Element in missileAlertList)
|
||
|
// {
|
||
|
// Vector3 newPosition = new Vector3(Element.alertTransform.position.x, Element.player.transform.position.y + 0.544f, 0);
|
||
|
// Element.alertTransform.position = newPosition;
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
#endregion
|
||
|
#region LIFECYCLE
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
prefabParent = GameObject.Find("prefab");
|
||
|
}
|
||
|
|
||
|
protected override void Awake()
|
||
|
{
|
||
|
_gameHandler = GameHandler.Instance;
|
||
|
|
||
|
_gameHandler.OnAttemptEnding += ResetLevel_OnAttemptEnding;
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (!GameController.Instance.IsExiting)
|
||
|
{
|
||
|
PrefabSpawnerBehaviour();
|
||
|
RemoveExpiredGameElements();
|
||
|
|
||
|
}
|
||
|
//UpdateMissileAlertPositions();
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
public struct MissileAlertInfo
|
||
|
{
|
||
|
public Transform alertTransform;
|
||
|
public GameObject player;
|
||
|
}
|
||
|
|
||
|
}
|