private async Task EvaluateInputKeypress(KeyboardEventArgs args)
        {
            if (args.Key.Equals("Enter", StringComparison.OrdinalIgnoreCase))
            {
                if (string.IsNullOrWhiteSpace(_inputText))
                {
                    return;
                }

                await CircuitConnection.SendChat(_inputText, Session.DeviceId);

                Session.ChatHistory.Add(new ChatHistoryItem()
                {
                    Origin  = ChatHistoryItemOrigin.Self,
                    Message = _inputText
                });

                _inputText = string.Empty;

                JsInterop.ScrollToEnd(_chatMessagesWindow);
            }
        }
Beispiel #2
0
    // Update is called once per frame
    void Update()
    {
        /*foreach(GameObject obj in circuitConnections)
         * {
         *      if(obj != null)
         *      {
         *              CircuitConnection circ = obj.GetComponent("CircuitConnection") as CircuitConnection;
         *              Debug.Log (circ.getConnectedPieces().Count + " , timestamp: " + circ.timeStamp);
         *      }
         * }*/

        if (currentConnection != null)
        {
            Vector3 curMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Vector2 differenceVector = new Vector2(curMouse.x - initialClickLocation.x, curMouse.y - initialClickLocation.y);


            //atan2 y/x
            //differenceVector = new Vector2(differenceVector.x/differenceVector.magnitude, differenceVector.y / differenceVector.magnitude);

            float angle = Mathf.Atan2(differenceVector.y, differenceVector.x);

            angle = angle * Mathf.Rad2Deg + 90;


            Vector2 midpoint = new Vector2((curMouse.x + initialClickLocation.x) / 2, (curMouse.y + initialClickLocation.y) / 2);

            currentConnection.transform.position = new Vector3(midpoint.x, midpoint.y, 0);

            //Magnitude of distance vector / height of sprite = scale


            if (differenceVector.magnitude >= 0.01f)
            {
                currentConnection.transform.eulerAngles = new Vector3(0.0f, 0.0f, angle);
                currentConnection.transform.localScale  = new Vector3(currentConnection.transform.localScale.x,
                                                                      differenceVector.magnitude / 64, 1);
            }
            else
            {
                currentConnection.transform.eulerAngles = new Vector3(0.0f, 0.0f, 0);
                currentConnection.transform.localScale  = new Vector3(currentConnection.transform.localScale.x, 0.01f, 1);
            }



            if (Input.GetMouseButtonUp(0))
            {
                //CircuitConnection cirConnect = currentConnection.GetComponent("CircuitConnection") as CircuitConnection;

                Vector3 mouseUpLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                //Vector2 mouseUpVector2 = new Vector2(mouseUpLocation.x, mouseUpLocation.y);

                GameObject initialBlock = null;
                GameObject endBlock     = null;

                foreach (GameObject obj in componentArray)
                {
                    if (obj == null)
                    {
                        continue;
                    }
                    Bounds  objBoundingBox = obj.renderer.bounds;
                    Vector3 objExtents     = objBoundingBox.extents;

                    Rect objBoundingRect = new Rect(objBoundingBox.center.x - objExtents.x,
                                                    objBoundingBox.center.y - objExtents.y, objExtents.x * 2, objExtents.y * 2);

                    if (objBoundingRect.Contains(mouseUpLocation))
                    {
                        endBlock = obj;
                    }
                    if (objBoundingRect.Contains(initialClickLocation))
                    {
                        initialBlock = obj;
                    }
                }

                if (initialBlock == null || endBlock == null || (initialBlock == endBlock))
                {
                    GameObject.Destroy(currentConnection);
                }
                else
                {
                    //breadth first search of space to construct connections along grid
                    GameObject.Destroy(currentConnection);
                    breadthFirstPath(initialBlock, endBlock);
                }

                currentConnection = null;
            }
        }

        else if (currentConnection == null)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                newPos.z             = 0;
                currentConnection    = (GameObject)Instantiate(connectionPrefab, newPos, Quaternion.identity);
                initialClickLocation = new Vector2(newPos.x, newPos.y);
            }
        }



        //Deleting connections


        if (Input.GetMouseButtonDown(1))
        {
            Vector3           mouseDownLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            List <GameObject> toBeRemoved       = new List <GameObject>();

            GameObject importantPiece = null;

            foreach (GameObject obj in circuitConnections)
            {
                Bounds  objBoundingBox = obj.renderer.bounds;
                Vector3 objExtents     = objBoundingBox.extents;

                Rect objBoundingRect = new Rect(objBoundingBox.center.x - objExtents.x,
                                                objBoundingBox.center.y - objExtents.y, objExtents.x * 2, objExtents.y * 2);

                if (objBoundingRect.Contains(mouseDownLocation))
                {
                    CircuitConnection clickLocScript = obj.GetComponent("CircuitConnection") as CircuitConnection;
                    int tempTimestamp = clickLocScript.timeStamp;

                    foreach (GameObject anotherObj in circuitConnections)
                    {
                        CircuitConnection circCon = anotherObj.GetComponent("CircuitConnection") as CircuitConnection;
                        if (circCon.timeStamp == tempTimestamp)
                        {
                            foreach (GameObject connpiece in circCon.getConnectedPieces())
                            {
                                if (!connpiece.name.Contains("powersupply") && !connpiece.name.Contains("passthrough"))
                                {
                                    importantPiece = connpiece;
                                    break;
                                }
                            }

                            toBeRemoved.Add(anotherObj);
                        }
                    }
                    break;
                }
            }

            foreach (GameObject tempObj in toBeRemoved)
            {
                circuitConnections.Remove(tempObj);
                CircuitConnection circCon = tempObj.GetComponent("CircuitConnection") as CircuitConnection;
                //Debug.Log (importantPiece.name);



                circCon.depower(importantPiece);
            }

            foreach (GameObject tempObj in toBeRemoved)
            {
                Destroy(tempObj);
            }
        }
    }
Beispiel #3
0
    private void breadthFirstPath(GameObject beginningObject, GameObject endingObject)
    {
        Queue <GameObject> Q = new Queue <GameObject>();
        List <GameObject>  V = new List <GameObject>();


        Q.Enqueue(beginningObject);
        V.Add(beginningObject);

        while (Q.Count > 0)
        {
            GameObject    expandedNode = Q.Dequeue();
            ShipComponent script       = expandedNode.GetComponent("ShipComponent") as ShipComponent;

            if (expandedNode == endingObject)
            {
                GameObject tempNode = endingObject;

                ShipComponent nodeScript = tempNode.GetComponent("ShipComponent") as ShipComponent;


                //IMPORTANT STUFF HERE
                bool canCreateConnection = false;
                if (beginningObject.name.Contains("powersupply") && !endingObject.name.Contains("passthrough"))
                {
                    PowerSupply powerScript = beginningObject.GetComponent("PowerSupply") as PowerSupply;
                    if (powerScript.addNode(endingObject))
                    {
                        nodeScript.powerSupply  = beginningObject;
                        nodeScript._powerLevel += 100;
                        Debug.Log("LINK CREATED");
                        canCreateConnection = true;
                    }
                    else
                    {
                        Debug.Log("THIS NODE IS OVERCAPACITY, CANNOT CREATE LINK");
                    }
                }
                if (canCreateConnection)
                {
                    //ending
                    while (tempNode != beginningObject)
                    {
                        //create connection
                        ShipComponent nodeScript2 = tempNode.GetComponent("ShipComponent") as ShipComponent;

                        Vector2 difference = new Vector2(tempNode.transform.position.x - nodeScript2.parent.transform.position.x,
                                                         tempNode.transform.position.y - nodeScript2.parent.transform.position.y);


                        Vector2 midpoint = new Vector2((tempNode.transform.position.x + nodeScript2.parent.transform.position.x) / 2,
                                                       (tempNode.transform.position.y + nodeScript2.parent.transform.position.y) / 2);


                        GameObject connection = (GameObject)Instantiate(graphicConnectionPrefab,
                                                                        new Vector3(midpoint.x, midpoint.y, 0),
                                                                        Quaternion.identity);

                        CircuitConnection circuitConnection = connection.GetComponent("CircuitConnection") as CircuitConnection;


                        float angle = Mathf.Atan2(difference.y, difference.x);


                        angle = angle * Mathf.Rad2Deg + 90;

                        connection.transform.position    = new Vector3(midpoint.x, midpoint.y, 0);
                        connection.transform.eulerAngles = new Vector3(0.0f, 0.0f, angle);
                        connection.transform.localScale  = new Vector3(2.0f, 1.0f, 1.0f);

                        circuitConnection.addConnectedPiece(tempNode);
                        circuitConnection.addConnectedPiece(nodeScript2.parent);
                        circuitConnection.timeStamp = timestamp;

                        nodeScript2._numConnections++;
                        ShipComponent anotherTemp = nodeScript2.parent.GetComponent("ShipComponent") as ShipComponent;
                        anotherTemp._numConnections++;

                        circuitConnections.Add(connection);
                        tempNode = nodeScript2.parent;
                    }
                    timestamp++;
                }
            }

            //get neighbors
            List <GameObject> neighbors = getNeighborsOfNode(script.index_Row, script.index_Column);
            //Debug.Log ("Count: " + neighbors.Count + " index_row: " + script.index_Row + ", index_col: " + script.index_Column);
            foreach (GameObject neighbor in neighbors)
            {
                //Debug.Log ("Nothing");
                if (!V.Contains(neighbor) && neighbor != null)
                {
                    ShipComponent neighborScript = neighbor.GetComponent("ShipComponent") as ShipComponent;
                    neighborScript.parent = expandedNode;
                    V.Add(neighbor);
                    Q.Enqueue(neighbor);
                }
            }
        }
    }