Advertisement
gizoncio

Breakout aula 18 - bola.cs

Sep 6th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. //////////////////////////////////////////////
  2. // Créditos Équilibré Cursos e Treinamentos //
  3. // http://www.equilibrecursos.net           //
  4. // http://www.youtube.com/equilibrecursos   //
  5. //////////////////////////////////////////////
  6.  
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using System.Collections;
  10.  
  11. public class bola : MonoBehaviour {
  12.  
  13.     public float velBola;
  14.     public Transform jogador;
  15.     public Rigidbody2D rb2D;
  16.  
  17.     public float posicaoBolinha;
  18.  
  19.     private bool comecou;
  20.  
  21.     public GameObject[] powerUps;
  22.  
  23.     public float porcentagem;
  24.  
  25.     private player scriptPlayer;
  26.  
  27.     public GameObject explosao;
  28.  
  29.     private gameManager gManagerScript;
  30.  
  31.     public Image goverImage;
  32.  
  33.     // Use this for initialization
  34.     void Start () {
  35.         //rigidbody2D.velocity = new Vector2(Random.Range(-2, 2), velBola);
  36.  
  37.         gManagerScript = GameObject.Find("GameManager").GetComponent<gameManager>();
  38.         scriptPlayer = GameObject.Find("player").GetComponent<player>();
  39.         rb2D = GetComponent<Rigidbody2D>();
  40.     }
  41.  
  42.     void Update()
  43.     {
  44.         if (Input.GetKeyDown(KeyCode.Space) && !comecou)
  45.         {
  46.             rb2D.velocity = new Vector2(Random.Range(-2f, 2f), velBola);
  47.             comecou = true;
  48.         }
  49.  
  50.         if (!comecou)
  51.         {
  52.             transform.position = new Vector2(jogador.position.x, transform.position.y);
  53.         }
  54.  
  55.     }
  56.  
  57.     void FixedUpdate () {
  58.  
  59.         if (comecou)
  60.         {
  61.             if (rb2D.velocity.y < 2 && GetComponent<Rigidbody2D>().velocity.y > -2)
  62.             {
  63.                 rb2D.gravityScale = 3;
  64.             }
  65.             else
  66.             {
  67.                 rb2D.gravityScale = 0;
  68.             }
  69.         }
  70.     }
  71.  
  72.     float colisaoBolinha (Vector2 posicaoBolinha, Vector2 posicaoJogador, float larguraJogador)
  73.     {
  74.         return (posicaoBolinha.x - posicaoJogador.x) / larguraJogador;
  75.     }
  76.  
  77.     void OnCollisionEnter2D(Collision2D outro)
  78.     {
  79.         if (outro.gameObject.tag == "bloco_verde" || outro.gameObject.tag == "bloco_laranja" || outro.gameObject.tag == "bloco_azul" || outro.gameObject.tag == "bloco_amarelo")
  80.         {
  81.             if (scriptPlayer.ativaExplosao)
  82.                 Instantiate(explosao, outro.transform.position, Quaternion.identity);
  83.  
  84.             if (Random.Range(0f, 1f) <= porcentagem)
  85.             {
  86.                 Instantiate(powerUps[Random.Range(0,powerUps.Length)], outro.gameObject.transform.position, Quaternion.identity);
  87.             }
  88.  
  89.             Destroy(outro.gameObject);
  90.         }
  91.  
  92.         if (outro.gameObject.tag == "Player")
  93.         {
  94.             float resultadoCalculo = colisaoBolinha(transform.position, outro.transform.position, ((BoxCollider2D)outro.collider).size.x);
  95.  
  96.             Vector2 novaDirecao = new Vector2(resultadoCalculo, 1).normalized;
  97.  
  98.             rb2D.velocity = novaDirecao * velBola;
  99.         }
  100.  
  101.         if (outro.gameObject.tag == "colisorBaixo")
  102.         {
  103.             if (gManagerScript.vidas > 1)
  104.             {
  105.                 gManagerScript.vidas -= 1;
  106.                 transform.position = new Vector2(jogador.position.x, jogador.position.y + posicaoBolinha);
  107.                 comecou = false;
  108.                 rb2D.velocity = Vector2.zero;
  109.             } else
  110.             {
  111.                 goverImage.enabled = true;
  112.                 gManagerScript.vidas -= 1;
  113.                 Destroy(gameObject);
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement