Skip to content

Instantly share code, notes, and snippets.

@stonin
Created March 21, 2019 01:52
Show Gist options
  • Save stonin/a4e2b063497c5270ab803bf1fc410f69 to your computer and use it in GitHub Desktop.
Save stonin/a4e2b063497c5270ab803bf1fc410f69 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource audioSource;
enum State { Alive, Dying, Transcending } // aqui eu criei 3 estados diferentes (enumerei estes 3 estados pra eles passarem a existir)
State state = State.Alive; // e aqui eu criei uma variável (letra minúscula) chamada state pra dizer que neste momento o estado atual é Alive
// (a variável state neste momento no início é o State.Alive)
[SerializeField] float rscThrust = 100f;
[SerializeField] float mainThrust = 180f;
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] bool debugMode = false;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip death;
[SerializeField] AudioClip success;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem deathParticles;
[SerializeField] ParticleSystem successParticles;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
}
if (Debug.isDebugBuild)
{
RespondToDebugKeys();
}
}
private void RespondToDebugKeys()
{
if (Input.GetKeyDown(KeyCode.L))
{
LoadNextLevel();
}
else if (Input.GetKeyDown(KeyCode.C))
{
debugMode = !debugMode;
}
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Fuel":
print("Fuel");
break;
case "Finish":
StartSuccessSequence();
break;
case "Dead":
StartDeathSequence();
break;
}
}
private void StartDeathSequence()
{
if (debugMode) { return; }
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
deathParticles.Play();
Invoke("RestartGame", levelLoadDelay);
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
successParticles.Play();
Invoke("LoadNextLevel", levelLoadDelay);
}
private void LoadNextLevel()
{
state = State.Transcending;
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
if (currentSceneIndex <= 2)
{
int nextSceneIndex = currentSceneIndex + 1;
SceneManager.LoadScene(nextSceneIndex);
}
else
{
SceneManager.LoadScene(0);
}
}
private void RestartGame()
{
SceneManager.LoadScene(0);
}
private void RespondToThrustInput()
{
float ThrustThisFrame = mainThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust(ThrustThisFrame);
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust(float ThrustThisFrame)
{
rigidBody.AddRelativeForce(Vector3.up * ThrustThisFrame);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
mainEngineParticles.Play();
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true; // take manual control of rotation.
float RotationThisFrame = rscThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(-Vector3.forward * RotationThisFrame);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.forward * RotationThisFrame);
}
rigidBody.freezeRotation = false; // resume physics control of rotation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment