Raphafrei

WeaponFix

Nov 14th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.46 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5.  
  6. [RequireComponent(typeof(AudioSource))]
  7. public class Atirar : MonoBehaviour {
  8.  
  9.    
  10.     public  Animation weaponAnim;
  11.     public  AnimationClip fireClip;
  12.     public  AnimationClip reloadClip;
  13.  
  14.    public KeyCode botaoRecarregar = KeyCode.R;
  15.    public int armaInicial = 0;
  16.    public string TagInimigo = "inimigo";
  17.    public Text BalasPente, BalasExtra;
  18.    public Material MaterialLasers;
  19.    public Arma919[] armas;
  20.    //
  21.    int armaAtual;
  22.    AudioSource emissorSom;
  23.    bool recarregando, atirando;
  24.    LineRenderer linhaDoLaser;
  25.    GameObject luzColisao;
  26.  
  27.    void Start () {
  28.       //laser das armas
  29.       luzColisao = new GameObject ();
  30.       luzColisao.AddComponent<Light> ();
  31.       luzColisao.GetComponent<Light> ().intensity = 8;
  32.       luzColisao.GetComponent<Light> ().bounceIntensity = 8;
  33.       luzColisao.GetComponent<Light> ().range = 0.2f;
  34.       luzColisao.GetComponent<Light> ().color = Color.red;
  35.       LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer> ();
  36.       lineRenderer.material = MaterialLasers;
  37.       lineRenderer.SetColors (Color.white, Color.white);
  38.       lineRenderer.SetWidth (0.015f, 0.05f);
  39.       lineRenderer.SetVertexCount (2);
  40.       linhaDoLaser = GetComponent<LineRenderer> ();
  41.       //
  42.       for (int x = 0; x < armas.Length; x++) {
  43.          armas [x].objetoArma.SetActive (false);
  44.          armas [x].lugarParticula.SetActive (false);
  45.          armas [x].balasExtra = armas [x].numeroDeBalas - armas [x].balasPorPente;
  46.          armas [x].balasNoPente = armas [x].balasPorPente;
  47.          armas [x].Miras.corLaser.a = 1;
  48.       }
  49.       if (armaInicial > armas.Length-1) {
  50.          armaInicial = armas.Length-1;
  51.       }
  52.       armas [armaInicial].objetoArma.SetActive (true);
  53.       armas [armaInicial].lugarParticula.SetActive (true);
  54.       armaAtual = armaInicial;
  55.       emissorSom = GetComponent<AudioSource> ();
  56.       recarregando = atirando = false;
  57.    }
  58.  
  59.    void Update () {
  60.  
  61.        weaponAnim.clip = fireClip;
  62.        weaponAnim.Play();
  63.    
  64.       //UI
  65.       BalasExtra.text = "BalasExtra: " + armas[armaAtual].balasExtra;
  66.       BalasPente.text = "BalasNoPente: " + armas[armaAtual].balasNoPente;
  67.       //troca de armas
  68.       if (Mathf.Abs (Input.GetAxis ("Mouse ScrollWheel")) > 0 && recarregando == false && atirando == false) {
  69.          if(Input.GetAxis ("Mouse ScrollWheel") > 0){
  70.             armaAtual++;
  71.          }
  72.          if(Input.GetAxis ("Mouse ScrollWheel") < 0){
  73.             armaAtual--;
  74.          }
  75.          if (armaAtual < 0) {
  76.             armaAtual = armas.Length - 1;
  77.          }
  78.          if (armaAtual > armas.Length - 1) {
  79.             armaAtual = 0;
  80.          }
  81.          AtivarArmaAtual ();
  82.       }
  83.       //atirar
  84.       if (Input.GetMouseButtonDown (0) && armas[armaAtual].balasNoPente > 0 && recarregando == false && atirando == false) {
  85.          atirando = true;
  86.          StartCoroutine (TempoTiro (armas [armaAtual].tempoPorTiro));
  87.          emissorSom.clip = armas [armaAtual].somTiro;
  88.          emissorSom.PlayOneShot (emissorSom.clip);
  89.          armas [armaAtual].balasNoPente--;
  90.          GameObject balaTemp = Instantiate (armas [armaAtual].particulaFogo, armas [armaAtual].lugarParticula.transform.position, transform.rotation) as GameObject;
  91.          Destroy (balaTemp, 0.5f);
  92.          //
  93.          RaycastHit pontoDeColisao;
  94.          if (Physics.Raycast (transform.position, transform.forward, out pontoDeColisao)) {
  95.             if (pontoDeColisao.transform.gameObject.tag == TagInimigo) {
  96.                pontoDeColisao.transform.gameObject.GetComponent<Inimigo> ().vida -= armas[armaAtual].danoPorTiro;
  97.             }
  98.          }
  99.       }
  100.       //recarregar
  101.       if (Input.GetKeyDown (botaoRecarregar) && recarregando == false && atirando == false && (armas[armaAtual].balasNoPente < armas[armaAtual].balasPorPente) && (armas [armaAtual].balasExtra > 0)) {
  102.          emissorSom.clip = armas [armaAtual].somRecarga;
  103.          emissorSom.PlayOneShot (emissorSom.clip);
  104.          int todasAsBalas = armas [armaAtual].balasNoPente + armas [armaAtual].balasExtra;
  105.          if (todasAsBalas >= armas [armaAtual].balasPorPente) {
  106.             armas [armaAtual].balasNoPente = armas [armaAtual].balasPorPente;
  107.             armas [armaAtual].balasExtra = todasAsBalas - armas [armaAtual].balasPorPente;
  108.          } else {
  109.             armas [armaAtual].balasNoPente = todasAsBalas;
  110.             armas [armaAtual].balasExtra = 0;
  111.          }
  112.          recarregando = true;
  113.          StartCoroutine (TempoRecarga(armas[armaAtual].tempoDaRecarga));
  114.       }
  115.       //laser da arma
  116.       if (recarregando == false) {
  117.          if (armas [armaAtual].Miras.ativarLaser == true) {
  118.             linhaDoLaser.enabled = true;
  119.             linhaDoLaser.material.SetColor ("_TintColor", armas [armaAtual].Miras.corLaser);
  120.             luzColisao.SetActive (true);
  121.             Vector3 PontoFinalDoLaser = transform.position + (transform.forward * 500);
  122.             RaycastHit hitDoLaser;
  123.             if (Physics.Raycast (transform.position, transform.forward, out hitDoLaser, 500)) {
  124.                linhaDoLaser.SetPosition (0, armas [armaAtual].lugarParticula.transform.position);
  125.                linhaDoLaser.SetPosition (1, hitDoLaser.point);
  126.                float distancia = Vector3.Distance (transform.position, hitDoLaser.point) - 0.03f;
  127.                luzColisao.transform.position = transform.position + transform.forward * distancia;
  128.             } else {
  129.                linhaDoLaser.SetPosition (0, armas [armaAtual].lugarParticula.transform.position);
  130.                linhaDoLaser.SetPosition (1, PontoFinalDoLaser);
  131.                luzColisao.transform.position = PontoFinalDoLaser;
  132.             }
  133.          }
  134.       } else {
  135.          linhaDoLaser.enabled = false;
  136.          luzColisao.SetActive (false);
  137.       }
  138.       //checar limites da municao
  139.       if (armas [armaAtual].balasNoPente > armas [armaAtual].balasPorPente) {
  140.          armas [armaAtual].balasNoPente = armas [armaAtual].balasPorPente;
  141.       }else if (armas [armaAtual].balasNoPente < 0) {
  142.          armas [armaAtual].balasNoPente = 0;
  143.       }
  144.       int numBalasExtra = armas [armaAtual].numeroDeBalas - armas [armaAtual].balasPorPente;
  145.       if (armas [armaAtual].balasExtra > numBalasExtra) {
  146.          armas [armaAtual].balasExtra = numBalasExtra;
  147.       }else if (armas [armaAtual].balasExtra < 0) {
  148.          armas [armaAtual].balasExtra = 0;
  149.       }
  150.    }
  151.  
  152.    IEnumerator TempoTiro(float tempoDoTiro){
  153.       yield return new WaitForSeconds (tempoDoTiro);
  154.       atirando = false;
  155.    }
  156.  
  157.    IEnumerator TempoRecarga(float tempoAEsperar){
  158.       yield return new WaitForSeconds (tempoAEsperar);
  159.       recarregando = false;
  160.    }
  161.  
  162.    void AtivarArmaAtual(){
  163.       for (int x = 0; x < armas.Length; x++) {
  164.          armas [x].objetoArma.SetActive (false);
  165.          armas [x].lugarParticula.SetActive (false);
  166.       }
  167.       armas [armaAtual].objetoArma.SetActive (true);
  168.       armas [armaAtual].lugarParticula.SetActive (true);
  169.       if (armas [armaAtual].Miras.ativarLaser == true) {
  170.          linhaDoLaser.material.color = armas [armaAtual].Miras.corLaser;
  171.          linhaDoLaser.enabled = true;
  172.          luzColisao.SetActive (true);
  173.          luzColisao.GetComponent<Light> ().color = armas [armaAtual].Miras.corLaser;
  174.       } else {
  175.          linhaDoLaser.enabled = false;
  176.          luzColisao.SetActive (false);
  177.       }
  178.    }
  179.  
  180.    void OnGUI(){
  181.       if (armas [armaAtual].Miras.AtivarMiraComum == true) {
  182.          GUIStyle stylez = new GUIStyle();
  183.          stylez.alignment = TextAnchor.MiddleCenter;
  184.          GUI.skin.label.fontSize = 20;
  185.          GUI.Label (new Rect (Screen.width / 2-6, Screen.height / 2-12, 12, 22), "+");
  186.       }
  187.    }
  188.     [Serializable]
  189.     public class LaserOuMira{
  190.      public bool ativarLaser = false;
  191.      public Color corLaser = Color.red;
  192.      public bool AtivarMiraComum = true;
  193.     }
  194.  
  195.     [Serializable]
  196.     public class Arma919{
  197.      [HideInInspector]
  198.      public int balasExtra, balasNoPente;
  199.    //
  200.      public int danoPorTiro = 40;
  201.       [Range(65,500)]
  202.     public int numeroDeBalas = 240;
  203.     [Range(1,50)]
  204.     public int balasPorPente = 30;
  205.       [Range(0.01f,5.0f)]
  206.       public float tempoPorTiro = 0.3f;
  207.       [Range(0.01f,5.0f)]
  208.       public float tempoDaRecarga = 0.5f;
  209.      [Space(10)]
  210.      public LaserOuMira Miras;
  211.      [Space(10)]
  212.       public GameObject objetoArma;
  213.      public GameObject lugarParticula;
  214.     public GameObject particulaFogo;
  215.       public AudioClip somTiro, somRecarga;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment