Advertisement
HenryKw

Bee_Controller

Oct 28th, 2021
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6.  
  7. public class BeeController : MonoBehaviour
  8. {
  9.  
  10.     Rigidbody2D rig;
  11.     public int health; // vida do player
  12.     public TMP_Text txt_health;
  13.     public float speed; // velocidade do player
  14.  
  15.     // PowerUps
  16.     public GameObject pwUp_HUD; // Hud do PowerUp
  17.     public Slider pwUp_slider_duracao; // Duração do PowerUp
  18.     public TMP_Text pwUp_txt_nome;
  19.  
  20.     public bool onPowerUp; // PowerUp ativo
  21.     public float pwUp_duracao; // Duração do PowerUp
  22.  
  23.     public bool escudo; // Escudo para abelha
  24.  
  25.     void Start()
  26.     {
  27.         rig = this.GetComponent<Rigidbody2D>();
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         txt_health.text = health.ToString();
  33.         Move();
  34.  
  35.         if(onPowerUp)
  36.         {
  37.             pwUp_slider_duracao.value -= Time.deltaTime;
  38.         }
  39.     }
  40.  
  41.     void FixedUpdate()
  42.     {
  43.         rig.velocity = new Vector2(speed, rig.velocity.y);
  44.     }
  45.  
  46.     //SISTEMA DE DANO;
  47.    
  48.     //FUTURAMENTE, MELHORAR ESSE SISTEMA
  49.     public void OnHit(int dmg)
  50.     {
  51.         if(!escudo)
  52.         {
  53.             health -= dmg;
  54.         }
  55.         else
  56.         {
  57.             dmg = 0;
  58.         }
  59.  
  60.         if(health <= 0)
  61.         {
  62.             GameController.instance.ShowGameOver();
  63.         }    
  64.  
  65.     }
  66.  
  67.     // CONTROLE DE MOVIMENTAÇÃO DA BEEZU
  68.     void Move()
  69.     {
  70.         if(Input.GetAxis("Vertical") != 0)
  71.         {
  72.             rig.velocity = new Vector2(0, Input.GetAxis("Vertical") * speed);
  73.         }
  74.         else
  75.         {
  76.             rig.velocity = Vector2.zero;
  77.         }
  78.     }
  79.  
  80.  
  81.     // Método de PowerUp de Velocidade
  82.     public IEnumerator PowerUpVelocidade(float duracao)
  83.     {
  84.  
  85.         pwUp_slider_duracao.maxValue = duracao;
  86.         pwUp_slider_duracao.value = duracao;
  87.         pwUp_duracao = duracao;
  88.  
  89.         pwUp_HUD.SetActive(true); // Ativa o HUD
  90.         onPowerUp = true; // Ativa o pwUp
  91.         pwUp_txt_nome.text = "Velocidade x2";
  92.  
  93.         float temp = speed;
  94.         speed = speed * 2; // Velocidade multiplicado por 2
  95.  
  96.         yield return new WaitForSeconds(pwUp_duracao); // Duração do pwUp
  97.         speed = temp;
  98.         onPowerUp = false; // Desativa o pwUp
  99.         pwUp_HUD.SetActive(false); // Desativa o HUD
  100.  
  101.     }
  102.  
  103.     public IEnumerator PowerUpEscudo(float duracao)
  104.     {
  105.         pwUp_slider_duracao.maxValue = duracao;
  106.         pwUp_slider_duracao.value = duracao;
  107.         pwUp_duracao = duracao;
  108.  
  109.         pwUp_HUD.SetActive(true); // Ativa o HUD
  110.         onPowerUp = true; // Ativa o pwUp
  111.         pwUp_txt_nome.text = "Invulnerável";
  112.        
  113.         escudo = true; // Ativa o Escudo
  114.         yield return new WaitForSeconds(pwUp_duracao); // Duração do pwUp
  115.         escudo = false; // Desativa o escudo
  116.         onPowerUp = false; // Desativa o pwUp
  117.         pwUp_HUD.SetActive(false); // Desativa o HUD
  118.  
  119.     }
  120.  
  121.     //Método de colisão com os coletáveis
  122.     void OnTriggerEnter2D(Collider2D col)
  123.     {
  124.         if(col.gameObject.tag == "Coletavel") // Passar de fase
  125.         {
  126.             GameController.instance.Coletou();
  127.             Destroy(col.gameObject);
  128.         }
  129.  
  130.         if(col.gameObject.tag == "pwUp_velocidade") // Aumenta a velocidade
  131.         {
  132.             Destroy(col.gameObject);
  133.             StartCoroutine(PowerUpVelocidade(5f));
  134.         }
  135.  
  136.         if(col.gameObject.tag == "pwUp_escudo") // Deixa Invulnerável
  137.         {
  138.             Destroy(col.gameObject);
  139.             StartCoroutine(PowerUpEscudo(5f));
  140.         }
  141.     }
  142.  
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement