public void CreateComponent(ComponentSO newComp) { Vector2 mousePos = cam.ScreenToWorldPoint(new Vector2(Screen.width / 2, Screen.height / 2)); //Convert screen center sto world pos GameObject newComponent = Instantiate(componentPrefab, transform.position, Quaternion.identity); newComponent.transform.position = (Vector2)mousePos; if (newComponent.GetComponent <LGComponent>()) { LGComponent comp = newComponent.GetComponent <LGComponent>(); comp.componentData = newComp; comp.canvasUI.worldCamera = cam; comp.compUIText.text = comp.componentData.componentName; } }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) //Left mouse button { //Check to see if object is at position Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1000); if (hit.collider != null) { if (hit.collider.GetComponentInParent <LGComponent>()) //Hit node { currNodeConnector = hit.collider.GetComponentInParent <LGComponent>(); if (currNodeConnector.inputNodes.Contains(hit.transform.gameObject)) { currNodeConnector = null; } } if (hit.collider.GetComponent <LGComponent>()) //Hit component { currentSelectedComponent = hit.collider.GetComponent <LGComponent>(); moveComponent = hit.transform; } } } if (Input.GetMouseButtonDown(1)) //Right Mouse Button - Interact with inputs { //Check to see if object is at position Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1000); if (hit.collider != null) { if (hit.collider.GetComponentInParent <LGComponent>()) //Hit node { LGComponent comp = hit.collider.GetComponentInParent <LGComponent>(); if (comp.componentData.isInput) { switch (comp.componentData.componentType) { case ComponentSO.ComponentType.SWITCH: comp.componentActive = !comp.componentActive; break; } } } } } if (Input.GetKeyDown(KeyCode.Delete)) { if (currentSelectedComponent != null) { currentSelectedComponent.DeleteComponent(); currentSelectedComponent = null; } } if (Input.GetMouseButtonUp(0)) { if (currNodeConnector != null) { //Check to see if object is at position Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1000); if (hit.collider != null) //Hit something { if (hit.collider.GetComponentInParent <LGComponent>()) //Hit node { LGComponent parentComponent = hit.collider.GetComponentInParent <LGComponent>(); if (parentComponent != currNodeConnector) { //DRAW LINE BETWEEN NODES parentComponent.connections.Add(new Connection(parentComponent, currNodeConnector)); Debug.Log("Connected nodes"); } } } } currNodeConnector = null; moveComponent = null; } if (moveComponent != null) { moveComponent.position = (Vector2)cam.ScreenToWorldPoint(Input.mousePosition); } }
public Connection(LGComponent input, LGComponent output) { this.input = input; this.output = output; }
void UpdateWires() { List <Connection> deadConnection = new List <Connection>(); foreach (Connection connection in connections) { LGComponent from = connection.output; //OUTPUT LGComponent to = connection.input; //INPUT //REMOVE DEAD CONNECTIONS for (int n = 0; n < from.outputNodes.Count; n++) { if (from.outputNodes[n] == null) { deadConnection.Add(connection); } } for (int n = 0; n < to.inputNodes.Count; n++) { if (to.inputNodes[n] == null) { deadConnection.Add(connection); } } GameObject node1GO = from.outputNodes[connection.outputNode]; GameObject node2GO = to.inputNodes[connection.inputNode]; if (node1GO == null || node2GO == null) { deadConnection.Add(connection); } else { if (!node1GO.activeInHierarchy || !node2GO.activeInHierarchy) { deadConnection.Add(connection); } } if (deadConnection.Count < 1) { if (connection.wire == null) { connection.inputNode = connections.Count - 1; connection.outputNode = from.outputNodes.Count - 1; Vector2 node1 = from.outputNodes[connection.outputNode].transform.position; Vector2 node2 = to.inputNodes[connection.inputNode].transform.position; GameObject newLR = Instantiate(wirePrefab, transform.position, Quaternion.identity); newLR.transform.position = Vector3.zero; LineRenderer newLRComp = newLR.GetComponent <LineRenderer>(); newLRComp.positionCount = 2; newLRComp.SetPositions(new Vector3[] { node1, node2 }); connection.wire = newLRComp; } else { Vector2 node1 = from.outputNodes[connection.outputNode].transform.position; Vector2 node2 = to.inputNodes[connection.inputNode].transform.position; connection.wire.positionCount = 2; connection.wire.SetPositions(new Vector3[] { node1, node2 }); } } } //Remove dead connection from list for (int n = 0; n < deadConnection.Count; n++) { Destroy(deadConnection[n].wire); connections.Remove(deadConnection[n]); } }