Rigidbody rigidBody;
AudioSource audioSource;
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip finishClip;
[SerializeField] AudioClip crashClip;
enum State { Alive, Dying, Transcending }
State state = State.Alive;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if (state == State.Alive)
{
RespondToRotateInput();
RespondToThrustInput();
}
}
private void OnCollisionEnter(Collision collision)
{
if (state != State.Alive){return;}
switch (collision.gameObject.tag)
{
case "Friendly":
Friendly();
break;
case "Finish":
FinishLevel();
break;
default:
Crash();
break;
}
}
private void Crash()
{
print("dead");
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(crashClip);
Invoke("LoadFirstScene", 1f);
}
private void FinishLevel()
{
print("hit finish");
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(finishClip);
Invoke("LoadNextScene", 1f);
}
private void Friendly()
{
print("Okay");
}
private void LoadFirstScene()
{
SceneManager.LoadScene(0);
}
private void LoadNextScene()
{
SceneManager.LoadScene(1);
}
private void RespondToThrustInput()
{
float rotationThisFrame = mainThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audioSource.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true;
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
}
}