Files
uni/year3/semester1/CT3536: Games Programming/labs/Weeks 4-8/Asteroids/Assets/Resources/Asteroid.cs
2024-03-09 19:25:59 +00:00

53 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
// inspector settings
public Rigidbody rigidBody;
//
// Use this for initialization
void Start () {
// randomise velocity
rigidBody.velocity = new Vector3(Random.Range(-10f,10f), 0f, Random.Range (-10f, 10f));
rigidBody.angularVelocity = new Vector3(Random.Range(-4f,4f), Random.Range (-4f, 4f), Random.Range (-4f, 4f));
}
public void SetScale(float min, float max) {
transform.localScale = new Vector3(Random.Range(min,max), Random.Range(min,max), Random.Range(min,max));
rigidBody.mass = transform.localScale.x * transform.localScale.y * transform.localScale.z;
}
void OnCollisionEnter(Collision collision) {
if (!collision.gameObject.name.Contains("asteroid")) {
Spaceship ss = collision.gameObject.GetComponent<Spaceship> ();
if (ss != null && ss.isInvulnerable)
return;
if (collision.gameObject.name.Contains("Bullet")) {
GameManager.currentScore = GameManager.currentScore + 10; // assuming same points no matter how big asteroid is
}
// we've collided with something other than another asteroid
Destroy(collision.gameObject); // if it's the player spaceship, the Spaceship scripts OnDestroy will look after re-creating it
Destroy(this.gameObject);
if (rigidBody.mass > 0.00015f) {
float minScale = rigidBody.mass * 50f;
float maxScale = minScale * 2f;
for (int i = 0; i < 3; i++) {
GameObject go = Instantiate (GameManager.instance.asteroidPrefab) as GameObject;
go.transform.position = transform.position;
go.GetComponent<Asteroid> ().SetScale (minScale, maxScale);
}
}
// if there are no more asteroids left, start next level
if (GameObject.FindGameObjectsWithTag("asteroid").Length == 0) {
GameManager.StartNextLevel();
}
}
}
}