Fix Vive controllers, add repeating game loop to breakout

This commit is contained in:
Toomas Tamm
2021-03-28 19:02:41 +03:00
parent 12343127d0
commit aeea9359af
29 changed files with 319 additions and 86 deletions

View File

@@ -19,14 +19,19 @@ namespace Breakout
private void Awake()
{
Events.OnBreakoutEndGame += DestroyCube;
Events.OnBreakoutEndGame += EventDestroyCube;
Events.OnBreakoutSetSpeed += SetSpeed;
_hasSpawnedBall = false;
}
private void EventDestroyCube()
{
DestroyCube(false);
}
private void OnDestroy()
{
Events.OnBreakoutEndGame -= DestroyCube;
Events.OnBreakoutEndGame -= EventDestroyCube;
Events.OnBreakoutSetSpeed -= SetSpeed;
}
@@ -40,13 +45,13 @@ namespace Breakout
BreakoutBall ball = other.gameObject.GetComponent<BreakoutBall>();
if (ball == null) return;
DestroyCube();
DestroyCube(true);
}
public virtual void DestroyCube()
private void DestroyCube(bool allowBallSpawn)
{
if (spawnBall & !_hasSpawnedBall)
if (spawnBall & !_hasSpawnedBall & allowBallSpawn)
{
_hasSpawnedBall = true;
var ball = Instantiate(ballPrefab, transform.position, Quaternion.identity, null);
@@ -57,6 +62,8 @@ namespace Breakout
Random.Range(-2.0f, 2.0f),
Random.Range(-2.0f, 2.0f)
));
Events.AddBalls();
}
speed.x = 0;

View File

@@ -36,5 +36,14 @@ namespace Breakout
{
Destroy(gameObject);
}
private void OnCollisionEnter(Collision other)
{
KinematicSpeedTransfer kst = other.gameObject.GetComponent<KinematicSpeedTransfer>();
if (kst == null) return;
Events.BreakoutStartGame();
}
}
}

View File

@@ -8,49 +8,55 @@ 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 GameObject startBallSpawnPoint;
public GameObject ballPrefab;
public List<Transform> spawnPoints;
public float timeBetweenRows = 5f;
public float startRowSpeed = 0.15f;
private float speedIncreasePerRow = 0.01f;
//private float _speedIncreasePerRow = 0.01f;
public float extraBallBreakableChance = 0.1f;
private float nextRowTime;
private float currentRowSpeed;
private float _nextRowTime;
private float _currentRowSpeed;
private bool gameStarted;
private int _ballCount;
private bool _gameStarted;
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
} else {
_instance = this;
}
gameStarted = true;
_gameStarted = false;
_ballCount = 1;
_nextRowTime = Time.time;
_currentRowSpeed = startRowSpeed;
Events.OnBreakoutEndGame += EndGame;
nextRowTime = Time.time;
currentRowSpeed = startRowSpeed;
Events.OnBreakoutStartGame += StartGame;
Events.OnBreakoutReduceBalls += ReduceBalls;
Events.OnBreakoutAddBalls += AddBalls;
}
void OnDestroy()
{
Events.OnBreakoutEndGame -= EndGame;
Events.OnBreakoutStartGame -= StartGame;
Events.OnBreakoutReduceBalls -= ReduceBalls;
Events.OnBreakoutAddBalls -= AddBalls;
}
void Update()
{
if (!gameStarted) return;
if (!_gameStarted) return;
if (nextRowTime < Time.time)
if (_nextRowTime < Time.time)
{
SpawnRow();
}
@@ -58,24 +64,35 @@ public class BreakoutManager : MonoBehaviour
void StartGame()
{
gameStarted = true;
SpawnRow();
if (_gameStarted) return;
_gameStarted = true;
_nextRowTime = Time.time;
}
private void SpawnBall()
{
Instantiate(ballPrefab, startBallSpawnPoint.transform.position, Quaternion.identity, null);
_ballCount += 1;
}
void EndGame()
{
gameStarted = false;
if (!_gameStarted) return;
_gameStarted = false;
SpawnBall();
_ballCount = 1;
}
void SpawnRow()
{
nextRowTime = Time.time + timeBetweenRows;
_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);
@@ -87,9 +104,25 @@ public class BreakoutManager : MonoBehaviour
var cube = box.GetComponent<BreakableCube>();
cube.speed.x = currentRowSpeed;
cube.speed.x = _currentRowSpeed;
}
Events.BreakoutSetSpeed(currentRowSpeed);
Events.BreakoutSetSpeed(_currentRowSpeed);
}
void ReduceBalls()
{
_ballCount -= 1;
if (_ballCount <= 0)
{
Events.BreakoutEndGame();
}
}
void AddBalls()
{
_ballCount += 1;
}
}

View File

@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using Breakout;
using UnityEngine;
public class DestroyBallForcefield : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
BreakoutBall ball = other.gameObject.GetComponent<BreakoutBall>();
if (ball == null) return;
Events.ReduceBalls();
Destroy(ball.gameObject);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9acb76ffa96cc784e9f873c1c22c695c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,8 +4,17 @@ public static class Events
{
public static event Action OnBreakoutEndGame;
public static void BreakoutEndGame() => OnBreakoutEndGame?.Invoke();
public static event Action OnBreakoutStartGame;
public static void BreakoutStartGame() => OnBreakoutStartGame?.Invoke();
public static event Action<float> OnBreakoutSetSpeed;
public static void BreakoutSetSpeed(float speed) => OnBreakoutSetSpeed?.Invoke(speed);
public static event Action OnBreakoutReduceBalls;
public static void ReduceBalls() => OnBreakoutReduceBalls?.Invoke();
public static event Action OnBreakoutAddBalls;
public static void AddBalls() => OnBreakoutAddBalls?.Invoke();
}