Untitled
unknown
plain_text
a year ago
5.1 kB
6
Indexable
using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using Unity.VisualScripting; using UnityEngine; public class CarSpawner : MonoBehaviour { [SerializeField] float timeTillNextSpawn = 1f; private LaneManager Lm; PickupManager pm; ObjectPooler op; // List<EnemyMovement> em; public GameObject[] carPrefab; private GameObject[] selectedPrefab; [SerializeField] private float minSpawnDistance = 2f; // Minimum allowed distance between colliders [SerializeField] LayerMask layersEnemyCannotSpawnOn; [SerializeField] float radius = 1f; Vector2 spawnPos = Vector2.zero; bool canSpawnHere = false; int randomPrefabIndex = 0; int randomLaneIndex = 0; float laneWidth; float middleLanePosition; float maxSpawnY = 50f; void Awake() { Lm = FindObjectOfType<LaneManager>(); pm = FindObjectOfType<PickupManager>(); op = FindObjectOfType<ObjectPooler>(); //op = ObjectPooler.instance; if (Lm == null) { Debug.LogError("LaneManager component not found!"); } } void Start() { op = FindObjectOfType<ObjectPooler>(); if (Lm != null) { StartCoroutine(SpawnCars()); } else Debug.Log("LM NOT FOUND IN START"); } public void Update() { if(pm.increaseTraffic == true) { maxSpawnY -= 30f; timeTillNextSpawn = 0f; } else { //maxSpawnY = 50f; timeTillNextSpawn = 2f; } } private IEnumerator SpawnCars() { while (true) { randomPrefabIndex = Random.Range(0, carPrefab.Length); GetRandomSpawnPosition(); canSpawnHere = CheckForValidPosition(spawnPos); if (canSpawnHere) { //Instantiate(carPrefab[randomPrefabIndex], spawnPos, Quaternion.identity); // Debug.Log("Tag:" + carPrefab[randomPrefabIndex].tag); string tag = carPrefab[randomPrefabIndex].tag; if(op != null) { op = ObjectPooler.instance; op.SpawnFromPool(tag, spawnPos); } else { Debug.Log("OP NULL"); } } float time = 0f; yield return new WaitForSeconds(time); } } private Vector2 GetRandomSpawnPosition() { randomLaneIndex = Random.Range(0, Lm.GetCurrentLaneCount()); Debug.Log("LaneIndex:" + randomLaneIndex); laneWidth = Lm.GetCurrentLaneWidth(randomLaneIndex); middleLanePosition = Lm.GetLanePosition(randomLaneIndex) + laneWidth / 2f; spawnPos = new Vector2(middleLanePosition, Random.Range(10.25f, maxSpawnY) + minSpawnDistance); Debug.Log("MLP:" + middleLanePosition); return spawnPos; } bool CheckForValidPosition(Vector2 spawnPosition) { bool isSpawnPosValid = false; int attemptCount = 0; int maxAttempts = 200; Collider2D[] colliders; while (!isSpawnPosValid && attemptCount < maxAttempts) { bool isInvalidCollision = false; colliders = Physics2D.OverlapCircleAll(spawnPos, radius); foreach (Collider2D col in colliders) { if (((1 << col.gameObject.layer) & layersEnemyCannotSpawnOn) != 0) { if (col.gameObject.tag == "Enemy") { // Check distance to existing car float distanceToCar = Vector2.Distance(spawnPosition, col.transform.position); if (distanceToCar < minSpawnDistance) { isInvalidCollision = true; while(distanceToCar > minSpawnDistance) { GetRandomSpawnPosition(); } } } else { isInvalidCollision = true; break; } } } if (!isInvalidCollision) { isSpawnPosValid = true; } attemptCount++; } // If no invalid collisions found, spawn position is valid if (!isSpawnPosValid) { Debug.Log("No valid spawn"); return false; } return true; } }
Editor is loading...
Leave a Comment