Diretor:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Diretor : MonoBehaviour
{
[SerializeField]
private GameObject imagemGameOver;
public void FinalizarJogo()
{
Time.timeScale = 0;
this.imagemGameOver.SetActive(true);
}
private Aviao aviao;
private void Start()
{
this.aviao = GameObject.FindObjectOfType<Aviao>();
}
public void Reiniciar()
{
this.imagemGameOver.SetActive(false);
Time.timeScale = 1;
this.aviao.Reiniciar();
this.DestruirObstaculos();
}
private void DestruirObstaculos()
{
Obstaculo[] obstaculos = GameObject.FindObjectsOfType<Obstaculo>();
foreach (Obstaculo obstaculo in obstaculos)
{
obstaculo.Destruir();
}
}
}
Avião:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aviao : MonoBehaviour
{
private Rigidbody2D fisica;
[SerializeField]
private float forca = 4;
private Diretor diretor;
private Vector3 posicaoInicial;
private void Awake()
{
this.posicaoInicial = this.transform.position;
this.fisica = this.GetComponent<Rigidbody2D>();
}
private void Start()
{
this.diretor = GameObject.FindObjectOfType<Diretor>();
}
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
this.Impulsionar();
}
}
public void Reiniciar()
{
this.transform.position = this.posicaoInicial;
this.fisica.simulated = true;
}
private void Impulsionar()
{
this.fisica.velocity = Vector2.zero;
this.fisica.AddForce(Vector2.up * this.forca, ForceMode2D.Impulse);
}
private void OnCollisionEnter2D(Collision2D collision)
{
this.fisica.simulated = false;
this.diretor.FinalizarJogo();
}
}
Obstaculo:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstaculo : MonoBehaviour
{
[SerializeField]
private float velocidade = 0.15f;
[SerializeField]
private float Variacao;
private void Awake()
{
this.Variacao = 1f;
this.transform.Translate(Vector3.up * Random.Range(-Variacao, Variacao));
}
private void Update()
{
this.transform.Translate(Vector3.left * this.velocidade * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
this.Destruir();
}
public void Destruir()
{
GameObject.Destroy(this.gameObject);
}
}