Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Rocket : MonoBehaviour
- {
- [SerializeField] float rcsThrust = 100f;
- [SerializeField] float mainThrust = 100f;
- [SerializeField] float levelLoadDelay = 2f;
- [SerializeField] float loadCurrentLevel = 1f;
- [SerializeField] AudioClip mainEngine;
- [SerializeField] AudioClip success;
- [SerializeField] AudioClip death;
- [SerializeField] ParticleSystem mainEngineParticles;
- [SerializeField] ParticleSystem successParticles;
- [SerializeField] ParticleSystem deathParticles;
- Rigidbody rigidBody;
- AudioSource audioSource;
- bool isTransitioning = false;
- bool collisionsDisabled = false;
- // Use this for initialization
- void Start()
- {
- rigidBody = GetComponent<Rigidbody>();
- audioSource = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update()
- {
- if (!isTransitioning)
- {
- RespondToThrustInput();
- RespondToRotateInput();
- }
- if (Debug.isDebugBuild)
- {
- // todo only if debug on
- RespondToDebugKeys();
- }
- }
- private void RespondToDebugKeys()
- {
- if (Input.GetKeyDown(KeyCode.L))
- {
- LoadNextLevel();
- }
- else if (Input.GetKeyDown(KeyCode.C))
- {
- collisionsDisabled = !collisionsDisabled; // toggle
- }
- }
- void OnCollisionEnter(Collision collision)
- {
- if (isTransitioning || collisionsDisabled) { return; }
- switch (collision.gameObject.tag)
- {
- case "Friendly":
- // do nothing
- break;
- case "Finish":
- StartSuccessSequence();
- break;
- default:
- StartDeathSequence();
- break;
- }
- }
- private void StartSuccessSequence()
- {
- isTransitioning = true;
- audioSource.Stop();
- audioSource.PlayOneShot(success);
- successParticles.Play();
- Invoke("LoadCurrentLevel", levelLoadDelay);
- }
- private void StartDeathSequence()
- {
- isTransitioning = true;
- audioSource.Stop();
- audioSource.PlayOneShot(death);
- deathParticles.Play();
- Invoke("LoadCurrentLevel", levelLoadDelay);
- }
- private void LoadCurrentLevel()
- {
- int currentScene = SceneManager.GetActiveScene().buildIndex;
- SceneManager.LoadScene(currentScene);
- }
- private void LoadNextLevel()
- {
- int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
- int LoadCurrentLevel = currentSceneIndex + 1;
- if (LoadCurrentLevel == SceneManager.sceneCountInBuildSettings)
- {
- LoadCurrentLevel // loop back to level died in
- }
- SceneManager.LoadScene(nextSceneIndex);
- }
- private void LoadFirstLevel()
- {
- SceneManager.LoadScene(LoadCurrentLevel);
- }
- private void RespondToThrustInput()
- {
- if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
- {
- ApplyThrust();
- }
- else
- {
- StopApplyingThrust();
- }
- }
- private void StopApplyingThrust()
- {
- audioSource.Stop();
- mainEngineParticles.Stop();
- }
- private void ApplyThrust()
- {
- rigidBody.AddRelativeForce(Vector3.up * mainThrust);
- if (!audioSource.isPlaying) // so it doesn't layer
- {
- audioSource.PlayOneShot(mainEngine);
- }
- mainEngineParticles.Play();
- }
- private void RespondToRotateInput()
- {
- rigidBody.angularVelocity = Vector3.zero; // remove rotation due to pyhsics
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement