Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class moveSphere : MonoBehaviour
  6. {
  7.     public Rigidbody rb; // para trabalhar com aspetos físicos
  8.     private int speed = 500; //NEWWW
  9.     private Vector3 screenPoint, offset, initialPos, oldPos, ballSpeed, zeros;
  10.     private List<Vector3> positions = new List<Vector3>(); //NEWWWW
  11.     TrailRenderer currentTrail;
  12.     Collider myCollider;
  13.     public AudioSource soundEffect;
  14.  
  15.     public GameObject prefab, explosion, Cubo1, Cubo2, Cubo3;
  16.  
  17.     public Renderer objCubo1, objCubo2, objCubo3;
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         currentTrail = GetComponent<TrailRenderer>();
  23.         currentTrail.emitting = false;
  24.        // currentTrail.widthMultiplier = 0.0f;
  25.         rb = GetComponent<Rigidbody>();
  26.         zeros.Set(0, 0, 0);
  27.         initialPos = GetComponent<Rigidbody>().position;
  28.         myCollider = GetComponent<Collider>();
  29.         soundEffect = GetComponent<AudioSource>();
  30.        
  31.     }
  32.  
  33.     void OnCollisionEnter(Collision other) {
  34.  
  35.         // TODO: Inserir som de colisão
  36.         if (other.gameObject.name == "esfera" && rb.name == "esfera")
  37.         {
  38.             soundEffect.Play();
  39.         }
  40.  
  41.         if (other.gameObject.name == "trash_can" && rb.name == "esfera") {
  42.             Debug.Log("trash");
  43.             lockedScript ls = rb.gameObject.GetComponent<lockedScript>();
  44.             ls.locked = true;
  45.  
  46.             rb.transform.GetComponent<Renderer>().material.color = new Color(1,1,1,0.43f);
  47.         }
  48.  
  49.         if ((other.gameObject.name == "baliza1" || other.gameObject.name == "baliza2" || other.gameObject.name == "baliza3" || other.gameObject.name == "baliza3") && (rb.name == "esfera")) {
  50.             lockedScript[] spheres = (lockedScript[]) GameObject.FindObjectsOfType(typeof(lockedScript)) as lockedScript[];
  51.             GameObject obj = null;
  52.  
  53.             if (other.gameObject.name == "baliza1") {
  54.                 Cubo1 = GameObject.Find("bal1_1");
  55.                 objCubo1 = Cubo1.GetComponent<Renderer>();
  56.                 baliza sc = Cubo1.GetComponent<baliza>();
  57.                 var ass = sc.assigned;
  58.                 if (ass == true) {
  59.                     foreach (lockedScript sphere in spheres) {
  60.                         // Debug.Log(sphere.locked);
  61.                         if (sphere.locked == true) {
  62.                             sphere.locked = false;
  63.                             obj = sphere.gameObject;
  64.                             break;
  65.                         }
  66.                     }
  67.  
  68.                     // caso nao haja mais bolas livres
  69.                     // FIXME: escolher uma sem ser a primaria etc
  70.                     if (obj == null) {
  71.                         objCubo1.material.color = Color.white;
  72.                         return;
  73.                     }
  74.                     Cubo2 = GameObject.Find("bal1_2");
  75.                     objCubo2 = Cubo2.GetComponent<Renderer>();
  76.                     objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
  77.                     baliza sc1 = Cubo2.GetComponent<baliza>();
  78.                     sc1.assigned = true;
  79.  
  80.                     Color color1 = objCubo1.material.color;
  81.                     Color color2 = objCubo2.material.color;
  82.                     float newRed = (color1.r + color2.r)/2;
  83.                     float newGreen = (color1.g + color2.g)/2;
  84.                     float newBlue = (color1.b + color2.b)/2;
  85.                     Color newColor = new Color(newRed,newGreen,newBlue);
  86.                     other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
  87.                     // EXPLOSÃO AQUI
  88.                     foreach (ContactPoint contact in other.contacts)
  89.                     {
  90.                         explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
  91.                         explosion.GetComponent<Renderer>().material.color = newColor;
  92.                     }
  93.  
  94.                     obj.GetComponent<Renderer>().material.color = newColor;
  95.  
  96.                     // TODO: ESPERAR
  97.  
  98.                    
  99.  
  100.                     other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
  101.                     objCubo2.material.color = Color.white;
  102.                     objCubo1.material.color = Color.white;
  103.                     sc.assigned = false;
  104.                     sc1.assigned = false;
  105.                    
  106.                 } else if (ass == false) {
  107.                     objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
  108.                     sc.assigned = true;
  109.                 }
  110.             }
  111.             if (other.gameObject.name == "baliza2") {
  112.                 Cubo1 = GameObject.Find("bal2_1");
  113.                 objCubo1 = Cubo1.GetComponent<Renderer>();
  114.                 baliza sc = Cubo1.GetComponent<baliza>();
  115.                 var ass = sc.assigned;
  116.                 if (ass == true) {
  117.                     foreach (lockedScript sphere in spheres) {
  118.                         // Debug.Log(sphere.locked);
  119.                         if (sphere.locked == true) {
  120.                             sphere.locked = false;
  121.                             obj = sphere.gameObject;
  122.                             break;
  123.                         }
  124.                     }
  125.  
  126.                     // caso nao haja mais bolas livres
  127.                     // FIXME: escolher uma sem ser a primaria etc
  128.                     if (obj == null) {
  129.                         objCubo1.material.color = Color.white;
  130.                         return;
  131.                     }
  132.                     Cubo2 = GameObject.Find("bal2_2");
  133.                     objCubo2 = Cubo2.GetComponent<Renderer>();
  134.                     objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
  135.                     baliza sc1 = Cubo2.GetComponent<baliza>();
  136.                     sc1.assigned = true;
  137.                
  138.                     Color color1 = objCubo1.material.color;
  139.                     Color color2 = objCubo2.material.color;
  140.                     float newRed = (color1.r + color2.r)/2;
  141.                     float newGreen = (color1.g + color2.g)/2;
  142.                     float newBlue = (color1.b + color2.b)/2;
  143.                     Color newColor = new Color(newRed,newGreen,newBlue);
  144.                     other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
  145.                    
  146.                     // EXPLOSÃO
  147.                    foreach (ContactPoint contact in other.contacts)
  148.                     {
  149.                         explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
  150.                         explosion.GetComponent<Renderer>().material.color = newColor;
  151.                     }
  152.  
  153.                     obj.GetComponent<Renderer>().material.color = newColor;
  154.  
  155.                     // TODO: ESPERAR
  156.  
  157.                     other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
  158.                     objCubo2.material.color = Color.white;
  159.                     objCubo1.material.color = Color.white;
  160.                     sc.assigned = false;
  161.                     sc1.assigned = false;
  162.                    
  163.                 } else if (ass == false) {
  164.                     objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
  165.                     sc.assigned = true;
  166.                 }
  167.             }
  168.             if (other.gameObject.name == "baliza3") {
  169.                 Cubo1 = GameObject.Find("bal3_1");
  170.                 objCubo1 = Cubo1.GetComponent<Renderer>();
  171.                 baliza sc = Cubo1.GetComponent<baliza>();
  172.                 var ass = sc.assigned;
  173.                
  174.                 if (ass == true) {
  175.                     Cubo2 = GameObject.Find("bal3_2");
  176.                     objCubo2 = Cubo2.GetComponent<Renderer>();
  177.                     baliza sc2 = Cubo2.GetComponent<baliza>();
  178.                     var ass2 = sc2.assigned;
  179.                     if (ass2 == true) {
  180.                         foreach (lockedScript sphere in spheres) {
  181.                             // Debug.Log(sphere.locked);
  182.                             if (sphere.locked == true) {
  183.                                 sphere.locked = false;
  184.                                 obj = sphere.gameObject;
  185.                                 break;
  186.                             }
  187.                         }
  188.  
  189.                         // caso nao haja mais bolas livres
  190.                         if (obj == null) {
  191.                             objCubo1.material.color = Color.white;
  192.                             return;
  193.                         }
  194.                         Cubo3 = GameObject.Find("bal3_3");
  195.                         objCubo3 = Cubo3.GetComponent<Renderer>();
  196.                         objCubo3.material.color = rb.transform.GetComponent<Renderer>().material.color;
  197.                         baliza sc3 = Cubo3.GetComponent<baliza>();
  198.                         sc3.assigned = true;
  199.  
  200.                         Color color1 = objCubo1.material.color;
  201.                         Color color2 = objCubo2.material.color;
  202.                         Color color3 = objCubo3.material.color;
  203.  
  204.                         float newRed = (color1.r + color2.r + color3.r)/3;
  205.                         float newGreen = (color1.g + color2.g + color3.g)/3;
  206.                         float newBlue = (color1.b + color2.b + color3.b)/3;
  207.  
  208.                         Color newColor = new Color(newRed,newGreen,newBlue);
  209.                         other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
  210.                         // EXPLOSÃO AQUI
  211.                         foreach (ContactPoint contact in other.contacts)
  212.                         {
  213.                             explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
  214.                             explosion.GetComponent<Renderer>().material.color = newColor;
  215.                         }
  216.  
  217.                         obj.GetComponent<Renderer>().material.color = newColor;
  218.  
  219.                         // TODO: ESPERAR
  220.  
  221.                         other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
  222.                         objCubo2.material.color = Color.white;
  223.                         objCubo1.material.color = Color.white;
  224.                         objCubo3.material.color = Color.white;
  225.                         sc.assigned = false;
  226.                         sc2.assigned = false;
  227.                         sc3.assigned = false;
  228.                     }else {
  229.                         objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
  230.                         sc2.assigned = true;
  231.                     }
  232.                 }else {
  233.                     objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
  234.                     sc.assigned = true;
  235.                 }
  236.             }
  237.             if (other.gameObject.name == "baliza4") {
  238.                 Cubo1 = GameObject.Find("bal4_1");
  239.                 objCubo1 = Cubo1.GetComponent<Renderer>();
  240.                 baliza sc = Cubo1.GetComponent<baliza>();
  241.                 var ass = sc.assigned;
  242.                
  243.                 if (ass == true) {
  244.                     Cubo2 = GameObject.Find("bal4_2");
  245.                     objCubo2 = Cubo2.GetComponent<Renderer>();
  246.                     baliza sc2 = Cubo2.GetComponent<baliza>();
  247.                     var ass2 = sc2.assigned;
  248.                     if (ass2 == true) {
  249.                         foreach (lockedScript sphere in spheres) {
  250.                             // Debug.Log(sphere.locked);
  251.                             if (sphere.locked == true) {
  252.                                 sphere.locked = false;
  253.                                 obj = sphere.gameObject;
  254.                                 break;
  255.                             }
  256.                         }
  257.  
  258.                         // caso nao haja mais bolas livres
  259.                         if (obj == null) {
  260.                             objCubo1.material.color = Color.white;
  261.                             return;
  262.                         }
  263.                         Cubo3 = GameObject.Find("bal4_3");
  264.                         objCubo3 = Cubo3.GetComponent<Renderer>();
  265.                         objCubo3.material.color = rb.transform.GetComponent<Renderer>().material.color;
  266.                         baliza sc3 = Cubo3.GetComponent<baliza>();
  267.                         sc3.assigned = true;
  268.  
  269.                         Color color1 = objCubo1.material.color;
  270.                         Color color2 = objCubo2.material.color;
  271.                         Color color3 = objCubo3.material.color;
  272.  
  273.                         float newRed = (color1.r + color2.r + color3.r)/3;
  274.                         float newGreen = (color1.g + color2.g + color3.g)/3;
  275.                         float newBlue = (color1.b + color2.b + color3.b)/3;
  276.  
  277.                         Color newColor = new Color(newRed,newGreen,newBlue);
  278.                         other.gameObject.transform.GetComponent<Renderer>().material.color = newColor;
  279.                         // EXPLOSÃO AQUI
  280.                         foreach (ContactPoint contact in other.contacts)
  281.                         {
  282.                             explosion = Instantiate(prefab, contact.point, Quaternion.Euler(new Vector3(-90, 0, 0)));
  283.                             explosion.GetComponent<Renderer>().material.color = newColor;
  284.                         }
  285.  
  286.                         obj.GetComponent<Renderer>().material.color = newColor;
  287.  
  288.                         // TODO: ESPERAR
  289.  
  290.                         other.gameObject.transform.GetComponent<Renderer>().material.color = new Color(1.0f,1.0f,1.0f);
  291.                         objCubo2.material.color = Color.white;
  292.                         objCubo1.material.color = Color.white;
  293.                         objCubo3.material.color = Color.white;
  294.                         sc.assigned = false;
  295.                         sc2.assigned = false;
  296.                         sc3.assigned = false;
  297.                     }else {
  298.                         objCubo2.material.color = rb.transform.GetComponent<Renderer>().material.color;
  299.                         sc2.assigned = true;
  300.                     }
  301.                 }else {
  302.                     objCubo1.material.color = rb.transform.GetComponent<Renderer>().material.color;
  303.                     sc.assigned = true;
  304.                 }
  305.             }
  306.         }
  307.        
  308.     }
  309.  
  310.  
  311.     void OnMouseDown()
  312.     {
  313.         lockedScript sc = GetComponent<lockedScript>();
  314.         if (sc.locked != true) {
  315.             oldPos = initialPos;        
  316.             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  317.             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  318.             Color forTrail = rb.transform.GetComponent<Renderer>().material.color;
  319.             TrailRenderer myTrail = GetComponent<TrailRenderer>();
  320.             myTrail.material.color = forTrail;
  321.         }
  322.        
  323.     }
  324.  
  325.     void OnMouseDrag()
  326.     {
  327.         lockedScript sc = GetComponent<lockedScript>();
  328.         if (sc.locked != true) {
  329.             var curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  330.             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - offset;
  331.             transform.position = curPosition;
  332.             currentTrail.emitting = false;
  333.  
  334.             if ((curPosition.z > -550 && curPosition.z < 550 && (curPosition.x > 270 || curPosition.x < -270)) || (curPosition.x < 270 && curPosition.x > -270 && (curPosition.z < -550 || curPosition.z > 550)) ) {
  335.                 myCollider.enabled = false;
  336.  
  337.             }
  338.             // if (curPosition.x > 270 || curPosition.x < -270 || curPosition.z > 550 && curPosition.z < -550)
  339.             // {
  340.             //     myCollider.enabled = false;
  341.             // }
  342.             else
  343.             {
  344.                 myCollider.enabled = true;
  345.             }
  346.         }
  347.     }
  348.  
  349.     void OnMouseUp()
  350.     {
  351.        
  352.         lockedScript sc = GetComponent<lockedScript>();
  353.         if (sc.locked != true) {
  354.             var curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  355.             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - offset;
  356.  
  357.             //se a bola for largada dentro dos limites da mesa
  358.             if (curPosition.x < 270 && curPosition.x > -270 && curPosition.z < 550 && curPosition.z > -550)
  359.             {
  360.                 ballSpeed = (GetComponent<Rigidbody>().position - positions[0]);    //NEWWWWW
  361.                 currentTrail.emitting = true;
  362.             }
  363.             else
  364.             {
  365.                 currentTrail.emitting = false;
  366.                 rb.position = initialPos;
  367.                 rb.velocity = Vector3.zero;
  368.                 rb.angularVelocity = Vector3.zero;
  369.                 myCollider.enabled = true;
  370.             }
  371.            
  372.         }
  373.     }
  374.  
  375.     private void FixedUpdate()
  376.     {
  377.         oldPos = GetComponent<Rigidbody>().position;
  378.         if(positions.Count < 10)                    //NEWWWWW
  379.         {
  380.             positions.Add(oldPos);
  381.         } else
  382.         {
  383.             positions.RemoveAt(0);
  384.             positions.Add(oldPos);
  385.         }                                           //ATÉ AQUI
  386.         if(ballSpeed != zeros)
  387.         {
  388.             rb.AddForce(ballSpeed * speed);
  389.             ballSpeed.Set(0, 0, 0);
  390.         }
  391.  
  392.     }
  393.  
  394.     void OnTriggerEnter(Collider other)
  395.     {
  396.         if (other.gameObject.CompareTag("Pick Up"))
  397.         {
  398.             other.gameObject.SetActive(false);
  399.         }
  400.     }
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement