Sure I’ll label them as I go too:
Music Player
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
void Awake()
{
if (instance != null)
{
DestroyObject(gameObject);
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
}
Paddle
using UnityEngine;
public class Paddle : MonoBehaviour {
public bool autoplay = false;
private Ball ball;
void Start()
{
ball = FindObjectOfType<Ball>();
}
// Update is called once per frame
void Update()
{
if (!autoplay)
{
MoveWithMouse();
}else{
AutoPlay();
}
}
void AutoPlay()
{
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
Vector3 ballPos = ball.transform.position;
paddlePos.x = Mathf.Clamp(ballPos.x, 0.5f, 15.5f);
this.transform.position = paddlePos;
}
void MoveWithMouse(){
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp (mousePosInBlocks, 0.5f, 15.5f);
this.transform.position = paddlePos;
}
}
Ball
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
// Use this for initialization
void Start () {
paddle = FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update() {
if (!hasStarted) {
this.transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0)) {
hasStarted = true;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
// Ball does not trigger sound when brick is destoyed.
// Not 100% sure why, possibly because brick isn't there.
Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
if (hasStarted)
{
GetComponent<AudioSource>().Play();
gameObject.GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
Brick
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public AudioClip crack;
public Sprite[] hitSprites;
private int timesHit;
private LevelManager levelManager;
public static int breakableCount = 0;
public GameObject smoke;
private bool isBreakable;
// Use this for initialization
void Start () {
isBreakable = (this.tag == "Breakable");
// Keep track of Breakable Bricks
if (isBreakable){
breakableCount++;
}
timesHit = 0;
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D collision){
AudioSource.PlayClipAtPoint(crack, transform.position, 5f);
if (isBreakable){
HandleHits();
}
}
void HandleHits () {
timesHit++;
int maxHit = hitSprites.Length + 1;
if (timesHit >= maxHit) {
breakableCount--;
levelManager.BrickDestroys();
PuffSmoke();
DestroyObject(gameObject);
}
else {
LoadSprites();
}
}
void PuffSmoke()
{
GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
smokePuff.GetComponent<ParticleSystem>().startColor = gameObject.GetComponent<SpriteRenderer>().color;
}
void LoadSprites() {
int spriteIndex = timesHit - 1;
if (hitSprites[spriteIndex]){
GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
}
//TODO Delete later
void SimulateWin() {
levelManager.LoadNextLevel();
}
}
Lose Collider
using UnityEngine;
using System.Collections;
using System;
public class LoseCollider : MonoBehaviour {
private LevelManager levelManager;
void start() {
}
void OnTriggerEnter2D(Collider2D trigger) {
}
void OnCollisionEnter2D(Collision2D collision){
levelManager = FindObjectOfType<LevelManager>();
levelManager.LoadLevel("Lose");
}
void update()
{
}
}
Level Manager
using UnityEngine;
using System.Collections;
using UnityEditor.SceneManagement;
public class LevelManager : MonoBehaviour {
private int Lives;
private LoseCollider loseCollider;
public GameObject playerLives;
void Start(){
loseCollider = FindObjectOfType<LoseCollider>();
Lives = 5;
}
public void LoadLevel(string name){
Brick.breakableCount = 0;
Application.LoadLevel(name);
}
public void QuitRequest(){
Application.Quit();
}
public void LoadNextLevel() {
Brick.breakableCount = 0;
Application.LoadLevel(Application.loadedLevel + 1);
}
public void BrickDestroys()
{
if (Brick.breakableCount <= 0) {
LoadNextLevel();
}
}
public void LivesCount(){
if (Lives <= 0) {
Application.LoadLevel("Lose");
}
}
}
Smoke Destroyer (This is a different script I’ve been having trouble with. I’m trying to prevent the hiarachy filling up with smoke instances, leading to possible slow-down on the game and machine and it’s been semi-effective for the moment. The lives is the major thing I want to get covered first though.)
That should be every script. I apologise if most of them you didn’t need, but I thought it best to include all of them as you stated.