S0m3guy

Untitled

Jun 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class BreadBoardInputPin : MonoBehaviour {
  7.  
  8.     public float inputValue = 5;
  9.     private SpriteRenderer spritRend;
  10.  
  11. //  private Line, newLineScript;
  12.     private LineRenderer lineRenderer = new LineRenderer ();
  13.     private LineRenderer newLineRend = new LineRenderer();
  14.     public GameObject newLineObj;
  15.     public Transform origin;
  16.  
  17.     public float[] outputs;
  18.  
  19.     [SerializeField]
  20.     Canvas UIcanvas;
  21.     [SerializeField]
  22.     GameObject ADPanel;
  23.     [SerializeField]
  24.     SpriteRenderer triangle;
  25.  
  26.     [SerializeField]
  27.     float variable;
  28.  
  29.     // Variables for short click detection
  30.     float levelTimer = 0.0f;
  31.     bool pressed = false;
  32.  
  33.     public string inputType = "digital";
  34.  
  35.     // For analog input
  36.     public float sineValue = 0;
  37.     float increment = 0.07f;
  38.     public Color lerpedColor;
  39.     float x;
  40.  
  41.     GameObject line;
  42.     Collider2D collisionObject;
  43.  
  44.     Vector3 clampVector;
  45.     BoxCollider2D upperBound, lowerBound;
  46.  
  47.     Text debugText1, debugText2;
  48.  
  49.     void Awake () {
  50.         spritRend = gameObject.GetComponent<SpriteRenderer> ();
  51.  
  52.         origin = GetComponent<Transform> ();
  53.  
  54.         outputs = new float[1];
  55.     }
  56.  
  57.     // Use this for initialization
  58.     void Start () {
  59.         upperBound = GameObject.Find("Upperbound").GetComponent<BoxCollider2D>();
  60.         lowerBound = GameObject.Find("Lowerbound").GetComponent<BoxCollider2D>();
  61.  
  62.         x  = Random.Range(0,10); // Generates randomization for all analog inputs
  63.  
  64.         debugText1 = GameObject.Find("DebugText1").GetComponent<Text>();
  65.         debugText2 = GameObject.Find("DebugText2").GetComponent<Text>();
  66.     }
  67.  
  68.     // Update is called once per frame
  69.     void Update () {
  70.  
  71.         if (pressed) {
  72.             levelTimer += Time.deltaTime;
  73.         }
  74.  
  75.         if (inputType == "analog") {
  76.             lerpedColor = Color.Lerp(Color.white, Color.green, inputValue / 5);
  77.             spritRend.color = lerpedColor;
  78.         } else if (inputType == "digital") {
  79.             if (inputValue == 5) {
  80.                 spritRend.color = Color.green;         
  81.             } else if (inputValue == 0) {
  82.                 spritRend.color = Color.white;         
  83.             }
  84.         }
  85.  
  86.         forwardInput (inputValue, outputs);
  87.     }
  88.  
  89.     public void startSine() {
  90.         InvokeRepeating("voltAmplitude", 0.07f, .07f);
  91.     }
  92.  
  93.     void voltAmplitude(){
  94.  
  95.         inputValue = Mathf.Abs(Mathf.Sin (x)*2.505f + 2.5f);
  96.         x += increment;
  97.     }
  98.  
  99.     private void SwitchDot () {
  100.  
  101.         if (inputValue == 0) {
  102.             inputValue = 5;
  103.         } else if (inputValue == 5) {
  104.             inputValue = 0;
  105.         }
  106.     }
  107.        
  108.     void OnMouseDown () {
  109.  
  110.         pressed = true;
  111.  
  112.         // instantiate Line after clicking pin
  113.         line = Instantiate (Resources.Load("LinePrefab")) as GameObject;
  114.         line.name = "Line_(" + line.GetHashCode() + ")";
  115.         debugText1.text = "Instantiated " + line.name;
  116.  
  117.         line.GetComponent<Bezier_Spline> ().originObject = this.gameObject;
  118.  
  119.         Manager.currentlyDrawnLine = newLineObj;
  120.     }
  121.  
  122.     void OnMouseDrag () {
  123.  
  124.         // Make the line visible after the "tap" time
  125.         if (levelTimer >= 0.25) {
  126.             line.GetComponent<LineRenderer>().enabled = true;
  127.         }
  128.  
  129.         Vector2 screenPos = new Vector2 ();
  130.         Camera.main.ScreenToWorldPoint (screenPos);
  131.  
  132.         // Restrict the position of the line
  133.         clampVector = new Vector3 ((Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).x,
  134.             Mathf.Clamp ((Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).y,
  135.                 lowerBound.bounds.max.y,
  136.                 upperBound.bounds.min.y),
  137.             (Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).z);
  138.  
  139.         line.GetComponent<LineRenderer>().SetPosition(0, transform.position);
  140.         line.GetComponent<LineRenderer>().SetPosition(1, clampVector);
  141.  
  142.         collisionObject = Physics2D.OverlapPoint (Camera.main.ScreenToWorldPoint (Input.mousePosition));
  143.  
  144.         line.GetComponent<Bezier_Spline>().tangent2.transform.position = new Vector3 (
  145.             ((Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).x - 1),
  146.             Mathf.Clamp ((Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).y,
  147.                 lowerBound.bounds.max.y,
  148.                 upperBound.bounds.min.y),
  149.             (Camera.main.ScreenToWorldPoint (Input.mousePosition) + Vector3.forward * 10).z);
  150.  
  151.         if ((collisionObject && collisionObject.CompareTag("inputPin")) ||
  152.             collisionObject && collisionObject.CompareTag("outputPin")) {
  153.             line.GetComponent<Bezier_Spline>().controlPoints[4] = collisionObject.gameObject;
  154.             line.GetComponent<Bezier_Spline>().controlPoints[5] = collisionObject.gameObject;
  155.         } else if (!collisionObject) {
  156.             line.GetComponent<Bezier_Spline>().controlPoints[4] = line.GetComponent<Bezier_Spline>().mouseFollower;
  157.             line.GetComponent<Bezier_Spline>().controlPoints[5] = line.GetComponent<Bezier_Spline>().mouseFollower;
  158.         }
  159.     }
  160.        
  161.     void OnMouseUp () {
  162.  
  163.         // Pin is tapped in a neutral state
  164.         if (levelTimer < 0.25 && inputType == "") {
  165.  
  166.             // Position UI
  167.             triangle.transform.position = new Vector3(transform.position.x + 0.59f, transform.position.y - 0.33f, transform.position.z);
  168.             ADPanel.transform.position = new Vector3(transform.position.x + 2.5f, transform.position.y - 0.9f, transform.position.z);
  169.  
  170.             // Pop UI
  171.             UIcanvas.enabled = true;
  172.             triangle.enabled = true;
  173.  
  174.             // Save current pin reference
  175.             Manager.currentInputPin = this.gameObject;
  176.  
  177.             debugText2.text = "About to destroy line " + line.name;
  178.             Destroy(line);
  179.             // ***here is where the already drawn and connected line is destroyed***
  180.  
  181.         } else if (levelTimer < 0.25 && inputType == "digital") {
  182.             SwitchDot();
  183.             Destroy(line);
  184.         } else if (levelTimer > 0.25 && inputType != "" && (collisionObject && collisionObject.gameObject == this.gameObject)) {
  185.             Handheld.Vibrate();
  186.             triangle.transform.position = new Vector3(transform.position.x + 0.59f, transform.position.y - 0.33f, transform.position.z);
  187.             ADPanel.transform.position = new Vector3(transform.position.x + 2.5f, transform.position.y - 0.9f, transform.position.z);
  188.  
  189.             UIcanvas.enabled = true;
  190.             triangle.enabled = true;
  191.             Manager.currentInputPin = this.gameObject;
  192.             Destroy(line);
  193.         }
  194.  
  195.         levelTimer = 0;
  196.         pressed = false;
  197.  
  198.         if (collisionObject && collisionObject.CompareTag("inputPin")) {
  199.             if (collisionObject.GetComponent<FuncBlockInputPin>().connectedLine) {
  200.                 // Line is already connected
  201.                 Destroy(collisionObject.GetComponent<FuncBlockInputPin>().connectedLine.gameObject);
  202.                 collisionObject.GetComponent<FuncBlockInputPin>().connectedLine = line;
  203.                 line.GetComponent<Bezier_Spline>().destinObject = collisionObject.gameObject;
  204.                 line.GetComponent<Bezier_Spline>().originObject = this.gameObject;
  205.             } else {
  206.                 // No line connected
  207.                 collisionObject.GetComponent<FuncBlockInputPin>().connectedLine = line;
  208.                 line.GetComponent<Bezier_Spline>().destinObject = collisionObject.gameObject;
  209.                 line.GetComponent<Bezier_Spline>().originObject = this.gameObject;
  210.                 line.GetComponent<Bezier_Spline>().isEndingPointSnapped = true;
  211.             }
  212.         } else if (collisionObject && collisionObject.CompareTag("outputPin")) {
  213.             if (collisionObject.GetComponent<BreadBoardOutputPin>().connectedLine) {
  214.                 Destroy(collisionObject.GetComponent<BreadBoardOutputPin>().connectedLine.gameObject);
  215.                 collisionObject.GetComponent<BreadBoardOutputPin>().connectedLine = line;
  216.                 line.GetComponent<Bezier_Spline>().destinObject = collisionObject.gameObject;
  217.                 line.GetComponent<Bezier_Spline>().originObject = this.gameObject;
  218.             } else {
  219.                 collisionObject.GetComponent<BreadBoardOutputPin>().connectedLine = line;
  220.                 line.GetComponent<Bezier_Spline>().destinObject = collisionObject.gameObject;
  221.                 line.GetComponent<Bezier_Spline>().originObject = this.gameObject;
  222.                 line.GetComponent<Bezier_Spline>().isEndingPointSnapped = true;
  223.             }
  224.         } else if (!collisionObject
  225.                   || !collisionObject.CompareTag("outputPin")
  226.                   || !collisionObject.CompareTag("inputPin")
  227.                   || !collisionObject.CompareTag("output")) {
  228.             Destroy(line);
  229.         }
  230.  
  231.         Manager.currentlyDrawnLine = null;
  232.     }
  233.  
  234.     public void forwardInput (float input, float[] outputs) {
  235.         for (int i = 0; i <= outputs.Length-1; i++) {
  236.             outputs[i] = inputValue;
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment