arkanoid/Assets/Scripts/Block.cs
2021-03-18 13:57:21 +01:00

57 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
public Material[] stageMat;
public int hitPoints = 1;
public GameObject powerupPrefab;
void Start()
{
setMat();
}
// Called by Bouncer when hit by ball
public void OnHit()
{
this.hitPoints--;
Gamemaster.instance.AddPoints(1);
if (this.hitPoints <= 0)
{
var randomNum = Random.Range(0,1f);
if (randomNum >= 0.2){
Instantiate(this.powerupPrefab,this.transform.position,Quaternion.identity);
}
// Delete block
Object.Destroy(this.gameObject);
}else
{
setMat();
}
}
private void setMat()
{
Material mat = null;
if (this.hitPoints <= 1)
{
mat = this.stageMat[0];
}else if (this.hitPoints <= 2)
{
mat = this.stageMat[1];
}else
{
mat = this.stageMat[2];
}
if (mat != null)
{
this.GetComponent<MeshRenderer>().material = mat;
}
}
}