using UnityEngine;
using UnityEngine.SceneManagement;
using PicaVoxel;
public class Rocket : MonoBehaviour {
Rigidbody rigidBody;
AudioSource audioSource;
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 1250f;
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip crashSound;
[SerializeField] AudioClip completedLevel;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem successParticles;
[SerializeField] ParticleSystem deathParticles;
public GameObject rocketProjectile;
private int currentLevel;
private int maxLevels;
private Exploder exploder;
private CollisionDetector collisionDetector;
enum State {Alive, Dying, Transcending };
State state = State.Alive;
bool collisionsDisabled = false;
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
currentLevel = SceneManager.GetActiveScene().buildIndex;
maxLevels = SceneManager.sceneCountInBuildSettings - 1;
exploder = transform.Find("Exploder").GetComponent<Exploder>();
Debug.Log("I have found " + exploder.name + " in Rocket script");
//Debug.Log("Current level is: " + currentLevel);
//Debug.Log("total amount of levels is: " + SceneManager.sceneCountInBuildSettings);
}
void Update ()
{
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
FireMissile();
}
if (Debug.isDebugBuild)
{
RespondToDebugKeys();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
private void RespondToDebugKeys()
{
if (Input.GetKeyDown(KeyCode.L))
{
LoadNextScene();
}
else if (Input.GetKeyDown(KeyCode.C))
{
collisionsDisabled = !collisionsDisabled; // this interchanges, if it's true, it will turn false and reversed, a toggle.
}
}
private void OnCollisionEnter(Collision collision)
{
if (state != State.Alive || collisionsDisabled) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
// Debug.Log("Friendly hit!");
break;
case "PicaVoxelVolume":
//Debug.Log("VOXEL BREAK?!");
// exploder.Explode(new Vector3(0f, 7f, 0f));
break;
case "Fuel":
//Debug.Log("You got some fuel!");
break;
case "Finish":
StartSuccessSequence();
break;
case "Exit":
Debug.Log("QUITING!!!!");
Application.Quit();
break;
default:
//StartDeathSequence();
Debug.Log("VOXEL BREAK?!");
exploder.Explode(new Vector3(0f, 7f, 0f));
break;
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
mainEngineParticles.Stop();
audioSource.PlayOneShot(completedLevel);
successParticles.Play();
Invoke("LoadNextScene", levelLoadDelay);
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
mainEngineParticles.Stop();
audioSource.PlayOneShot(crashSound);
deathParticles.Play();
Invoke("LoadNextScene", levelLoadDelay);
}
private void LoadNextScene()
{
switch (state)
{
case State.Dying:
if (currentLevel > 1)
{
SceneManager.LoadScene(1);
}
else
{
SceneManager.LoadScene(currentLevel);
}
break;
default:
if (currentLevel == maxLevels)
{
SceneManager.LoadScene(1);
} else
{
SceneManager.LoadScene(currentLevel + 1);
}
break;
}
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void FireMissile()
{
if (Input.GetKeyDown(KeyCode.F))
{
Vector3 firePoint = new Vector3(0f, 2f, 0f);
Instantiate(rocketProjectile, transform.position + firePoint, transform.rotation);
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
mainEngineParticles.Play();
}
}
private void RespondToRotateInput()
{
//rigidBody.freezeRotation = true; // Not needed anymore with angularvelocity. Take manual control of rotation, so if we turn left or right after a bump we can recover.
rigidBody.angularVelocity = Vector3.zero; // remove rotation due to physics.
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Q)) // Can not turn both sides at the same time.
{
transform.Rotate(Vector3.forward * rcsThrust * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rcsThrust * Time.deltaTime);
}
//rigidBody.freezeRotation = false; // Resume normal Unity physics calculations.
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.