190 lines
6.4 KiB
C#
190 lines
6.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class MapCellCalculator : MonoBehaviour{
|
|
|
|
[SerializeField] bool cutscene = false;
|
|
[SerializeField] Vector3[] enemySpawnerPositions = new Vector3[9];
|
|
|
|
[Space]
|
|
[SerializeField] Vector3 bottomLeftCorner = Vector3.zero;
|
|
[SerializeField] Vector2 size = Vector2.zero;
|
|
[SerializeField] LayerMask whatIsWall = -1;
|
|
|
|
[SerializeField] GameObject coin = default;
|
|
[SerializeField, Range(0, 1)] float coinHeight = 1;
|
|
|
|
List<Vector3> positions;
|
|
List<Vector3> availablePositions;
|
|
|
|
GameController gameController;
|
|
|
|
[Header("Powerups")]
|
|
[SerializeField, Range(1, 10f)] float timeToSpawn = 5f;
|
|
float currentTime;
|
|
[SerializeField] bool canSpawnEnergy = true;
|
|
[SerializeField] Vector2 energySpawnSize = Vector2.zero;
|
|
[SerializeField] Vector3 energySpawnBottomLeftCorner = Vector3.zero;
|
|
List<Vector3> energyPos;
|
|
List<Vector3> energyAvailablePos;
|
|
|
|
[Space]
|
|
[SerializeField] Vector3[] bombSpawnPositions = new Vector3[4];
|
|
[SerializeField] GameObject bombSpawner = default;
|
|
|
|
void Awake(){
|
|
gameController = FindObjectOfType<GameController>();
|
|
gameController.spawnerPositions = new List<Vector3>();
|
|
foreach(Vector3 v in enemySpawnerPositions){
|
|
gameController.spawnerPositions.Add(v);
|
|
}
|
|
|
|
positions = new List<Vector3>();
|
|
availablePositions = new List<Vector3>();
|
|
for (int i = 0; i < size.x; i++){
|
|
for (int j = 0; j < size.y; j++){
|
|
positions.Add(bottomLeftCorner + new Vector3(i, 0, j));
|
|
}
|
|
}
|
|
positions = positions.Distinct().ToList();
|
|
|
|
foreach (Vector3 v in positions){
|
|
if (!Physics.CheckSphere(v, .3f, whatIsWall)){
|
|
availablePositions.Add(v);
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < availablePositions.Count; i++){
|
|
Vector3 temp = availablePositions[i];
|
|
int randomIndex = Random.Range(i, availablePositions.Count);
|
|
availablePositions[i] = availablePositions[randomIndex];
|
|
availablePositions[randomIndex] = temp;
|
|
}
|
|
|
|
gameController.numberOfCoins = availablePositions.Count;
|
|
|
|
StartCoroutine(SpawnCoins());
|
|
|
|
foreach (Vector3 v in bombSpawnPositions){
|
|
Instantiate(bombSpawner, v, Quaternion.identity);
|
|
}
|
|
|
|
energyPos = new List<Vector3>();
|
|
energyAvailablePos = new List<Vector3>();
|
|
for (int i = 0; i < energySpawnSize.x; i++){
|
|
for (int j = 0; j < energySpawnSize.y; j++){
|
|
energyPos.Add(energySpawnBottomLeftCorner + new Vector3(i, 0, j));
|
|
}
|
|
}
|
|
energyPos = energyPos.Distinct().ToList();
|
|
|
|
foreach (Vector3 v in energyPos){
|
|
if (!Physics.CheckSphere(v, .3f, whatIsWall)){
|
|
energyAvailablePos.Add(v);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Start(){
|
|
gameController.enemies = new List<EnemyController>();
|
|
foreach (Vector3 v in enemySpawnerPositions){
|
|
GameObject enemy = ObjectPooler.instance.SpawnFromPool("Enemy", v + Vector3.up, Quaternion.identity);
|
|
gameController.enemies.Add(enemy.GetComponent<EnemyController>());
|
|
}
|
|
}
|
|
|
|
public void CanSpawnEnergy(){
|
|
canSpawnEnergy = true;
|
|
}
|
|
|
|
void Update(){
|
|
if (canSpawnEnergy){
|
|
currentTime -= Time.deltaTime;
|
|
if(currentTime <= 0){
|
|
ObjectPooler.instance.SpawnFromPool("BulletPickup", energyAvailablePos[Random.Range(0, energyAvailablePos.Count)] + Vector3.up * coinHeight, Quaternion.identity);
|
|
currentTime = timeToSpawn;
|
|
canSpawnEnergy = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator SpawnCoins(){
|
|
List<CoinController> coins = new List<CoinController>();
|
|
foreach (Vector3 v in availablePositions){
|
|
GameObject coinController = Instantiate(coin, v + Vector3.up * coinHeight, Quaternion.identity, transform);
|
|
coins.Add(coinController.GetComponent<CoinController>());
|
|
}
|
|
|
|
if (cutscene){
|
|
foreach (CoinController c in coins){
|
|
yield return new WaitForSeconds(10 / availablePositions.Count);
|
|
//Instantiate(coin, v + Vector3.up * coinHeight, Quaternion.identity, transform);
|
|
c.Disable();
|
|
}
|
|
}else{
|
|
foreach (CoinController c in coins){
|
|
c.Disable();
|
|
}
|
|
}
|
|
yield break;
|
|
}
|
|
|
|
void OnDrawGizmos(){
|
|
if (!Application.isPlaying){
|
|
Gizmos.color = Color.yellow;
|
|
|
|
positions = new List<Vector3>();
|
|
availablePositions = new List<Vector3>();
|
|
for (int i = 0; i < size.x; i++){
|
|
for (int j = 0; j < size.y; j++){
|
|
positions.Add(bottomLeftCorner + new Vector3(i, 0, j));
|
|
}
|
|
}
|
|
positions = positions.Distinct().ToList();
|
|
|
|
foreach (Vector3 v in positions){
|
|
if (!Physics.CheckSphere(v, .3f, whatIsWall)){
|
|
availablePositions.Add(v);
|
|
}
|
|
}
|
|
|
|
foreach (Vector3 v in availablePositions){
|
|
Gizmos.DrawSphere(v + Vector3.up * coinHeight, .15f);
|
|
}
|
|
|
|
|
|
energyPos = new List<Vector3>();
|
|
energyAvailablePos = new List<Vector3>();
|
|
for (int i = 0; i < energySpawnSize.x; i++){
|
|
for (int j = 0; j < energySpawnSize.y; j++){
|
|
energyPos.Add(energySpawnBottomLeftCorner + new Vector3(i, 0, j));
|
|
}
|
|
}
|
|
energyPos = energyPos.Distinct().ToList();
|
|
|
|
foreach (Vector3 v in energyPos){
|
|
if (!Physics.CheckSphere(v, .3f, whatIsWall)){
|
|
energyAvailablePos.Add(v);
|
|
}
|
|
}
|
|
|
|
Gizmos.color = Color.blue;
|
|
foreach(Vector3 v in energyAvailablePos){
|
|
Gizmos.DrawSphere(v + Vector3.up * coinHeight, .15f);
|
|
}
|
|
|
|
Gizmos.color = Color.red;
|
|
foreach(Vector3 v in bombSpawnPositions){
|
|
Gizmos.DrawSphere(v + Vector3.up * coinHeight, .25f);
|
|
}
|
|
|
|
|
|
Gizmos.color = Color.magenta;
|
|
foreach (Vector3 v in enemySpawnerPositions){
|
|
Gizmos.DrawSphere(v + Vector3.up * coinHeight, .15f);
|
|
}
|
|
}
|
|
}
|
|
}
|