DeltaVR/Assets/Scripts/Breakout/BreakoutManager.cs
2021-03-15 02:08:53 +02:00

96 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Breakout;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;
public class BreakoutManager : MonoBehaviour
{
private static BreakoutManager _instance;
public static BreakoutManager Instance { get { return _instance; } }
public GameObject defaultBreakablePrefab;
public GameObject extraBallBreakablePrefab;
public List<Transform> spawnPoints;
public float timeBetweenRows = 5f;
public float startRowSpeed = 0.15f;
private float speedIncreasePerRow = 0.01f;
public float extraBallBreakableChance = 0.1f;
private float nextRowTime;
private float currentRowSpeed;
private bool gameStarted;
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
} else {
_instance = this;
}
gameStarted = true;
Events.OnBreakoutEndGame += EndGame;
nextRowTime = Time.time;
currentRowSpeed = startRowSpeed;
}
void OnDestroy()
{
Events.OnBreakoutEndGame -= EndGame;
}
void Update()
{
if (!gameStarted) return;
if (nextRowTime < Time.time)
{
SpawnRow();
}
}
void StartGame()
{
gameStarted = true;
SpawnRow();
}
void EndGame()
{
gameStarted = false;
}
void SpawnRow()
{
nextRowTime = Time.time + timeBetweenRows;
//currentRowSpeed += speedIncreasePerRow;
foreach (var point in spawnPoints)
{
GameObject box;
if (Random.value <= extraBallBreakableChance)
{
box = Instantiate(extraBallBreakablePrefab, point.position, Quaternion.identity, null);
}
else
{
box = Instantiate(defaultBreakablePrefab, point.position, Quaternion.identity, null);
}
var cube = box.GetComponent<BreakableCube>();
cube.speed.x = currentRowSpeed;
}
Events.BreakoutSetSpeed(currentRowSpeed);
}
}