Add breakout balls, cubes, materials, scripts.
This commit is contained in:
86
Assets/Scripts/Breakout/BreakableCube.cs
Normal file
86
Assets/Scripts/Breakout/BreakableCube.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Oculus.Platform.Models;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions.Must;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Breakout
|
||||
{
|
||||
public class BreakableCube : MonoBehaviour
|
||||
{
|
||||
public Vector3 speed;
|
||||
public bool spawnBall;
|
||||
public GameObject ballPrefab;
|
||||
public float fadeTime = 2f;
|
||||
|
||||
private bool _hasSpawnedBall;
|
||||
private static readonly int Dissolve = Shader.PropertyToID("Vector1_b97048ce40a9495091dbb3eb2c84769e"); // From Dissolve Shader Graph
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Events.OnBreakoutEndGame += DestroyCube;
|
||||
Events.OnBreakoutSetSpeed += SetSpeed;
|
||||
_hasSpawnedBall = false;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Events.OnBreakoutEndGame -= DestroyCube;
|
||||
Events.OnBreakoutSetSpeed -= SetSpeed;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.position += (speed * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
BreakoutBall ball = other.gameObject.GetComponent<BreakoutBall>();
|
||||
|
||||
if (ball == null) return;
|
||||
DestroyCube();
|
||||
}
|
||||
|
||||
public virtual void DestroyCube()
|
||||
{
|
||||
|
||||
if (spawnBall & !_hasSpawnedBall)
|
||||
{
|
||||
_hasSpawnedBall = true;
|
||||
var ball = Instantiate(ballPrefab, transform.position, Quaternion.identity, null);
|
||||
|
||||
ball.GetComponent<Rigidbody>().AddForce(
|
||||
new Vector3(
|
||||
Random.Range(-2.0f, 2.0f),
|
||||
Random.Range(-2.0f, 2.0f),
|
||||
Random.Range(-2.0f, 2.0f)
|
||||
));
|
||||
}
|
||||
|
||||
speed.x = 0;
|
||||
|
||||
StartCoroutine(FadeMaterial(GetComponent<MeshRenderer>().material, fadeTime));
|
||||
|
||||
//Destroy(gameObject, Time.maximumDeltaTime);
|
||||
}
|
||||
|
||||
private void SetSpeed(float newspeed)
|
||||
{
|
||||
speed.x = newspeed;
|
||||
}
|
||||
|
||||
private IEnumerator FadeMaterial(Material material, float fadeTime)
|
||||
{
|
||||
while (material.GetFloat(Dissolve) < 0.9)
|
||||
{
|
||||
var newDissolve = Mathf.MoveTowards(material.GetFloat(Dissolve), 1, fadeTime * Time.deltaTime);
|
||||
material.SetFloat(Dissolve, newDissolve);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Breakout/BreakableCube.cs.meta
Normal file
11
Assets/Scripts/Breakout/BreakableCube.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 937cad764c6bfd649a75d521269ae995
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
40
Assets/Scripts/Breakout/BreakoutBall.cs
Normal file
40
Assets/Scripts/Breakout/BreakoutBall.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Breakout
|
||||
{
|
||||
public class BreakoutBall : MonoBehaviour
|
||||
{
|
||||
public float speed = 2f;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Events.OnBreakoutEndGame += DestroyBall;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Events.OnBreakoutEndGame -= DestroyBall;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
// Fix velocity on X axis so ball is more predictable.
|
||||
var velocity = _rigidbody.velocity;
|
||||
velocity.x = Math.Sign(velocity.x) * speed;
|
||||
velocity = velocity.normalized * (speed * 1.5f);
|
||||
_rigidbody.velocity = velocity;
|
||||
}
|
||||
|
||||
private void DestroyBall()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
95
Assets/Scripts/Breakout/BreakoutManager.cs
Normal file
95
Assets/Scripts/Breakout/BreakoutManager.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Breakout/BreakoutManager.cs.meta
Normal file
11
Assets/Scripts/Breakout/BreakoutManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f772d08c29771b8478a4d0a9f4ba07eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,18 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EndForcefield : MonoBehaviour
|
||||
namespace Breakout
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
public class EndForcefield : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
BreakableCube breakableCube = other.gameObject.GetComponent<BreakableCube>();
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
if (breakableCube != null)
|
||||
{
|
||||
Events.BreakoutEndGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class RigidbodySpeedLimiter : MonoBehaviour
|
||||
{
|
||||
public float maxSpeed = 5f;
|
||||
public float minSpeed = 2f;
|
||||
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (_rigidbody.IsSleeping()) return;
|
||||
|
||||
float velocity = _rigidbody.velocity.magnitude;
|
||||
|
||||
Debug.Log(velocity);
|
||||
|
||||
if (velocity < minSpeed)
|
||||
{
|
||||
_rigidbody.velocity = _rigidbody.velocity.normalized * minSpeed;
|
||||
}
|
||||
else if (velocity > maxSpeed)
|
||||
{
|
||||
_rigidbody.velocity = Vector3.ClampMagnitude(_rigidbody.velocity, maxSpeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Events.cs
Normal file
11
Assets/Scripts/Events.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
public static class Events
|
||||
{
|
||||
public static event Action OnBreakoutEndGame;
|
||||
public static void BreakoutEndGame() => OnBreakoutEndGame?.Invoke();
|
||||
|
||||
public static event Action<float> OnBreakoutSetSpeed;
|
||||
public static void BreakoutSetSpeed(float speed) => OnBreakoutSetSpeed?.Invoke(speed);
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Events.cs.meta
Normal file
11
Assets/Scripts/Events.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 477321c71b8018040b003b5fa6f17c34
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user