void Update()
    {
        debugFrameCount++;

        // don't process the game if the world is still being instantiated.
        if (worldGenerator.isBusy())
        {
            return;
        }

        if (playerRef != null)
        {
            // always set the camera on top of the player.
            cameraRef.transform.position = new Vector3(playerRef.position.x, playerRef.position.y, transform.position.z - 50);
        }

        // if you are adding a platform...
        // cancels when you click on nothing or when you right click.

        Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        mousePointInWorld.z = 0;
        // add platform system
        if (addingPlatforms && platformsToAdd.Count > 0)
        {
            PlatformBehavior platformToPreviewForAdd = platformsToAdd[0];  // show a preview of the platform.
            platformToPreviewForAdd.GetComponent <ContainerEntityBehavior>().refreshChildList();
            platformToPreviewForAdd.renderAsFadedPreview();
            platformToPreviewForAdd.setValueBlockText("" + platformToPreviewForAdd.getValue());
            platformToPreviewForAdd.transform.position = mousePointInWorld;
            platformToPreviewForAdd.gameObject.SetActive(true);
            platformToPreviewForAdd.GetComponent <BoxCollider2D>().isTrigger = true;

            if ((Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape)) && hoverLink == null) // deselecting with right click?
            {
                addingPlatforms = false;
                platformToPreviewForAdd.gameObject.SetActive(false);
                if (selectedLink != null)  // deselect on canceling placement.
                {
                    selectedLink.setPreviewConnection(null);
                    selectedLink.setState(LinkBehavior.State.NORMAL);
                    selectedLink = null;
                }
            }
            else if (selectedLink != null && Input.GetMouseButton(0)) // dragging from a link.
            {
                selectedLink.setPreviewConnection(platformToPreviewForAdd.GetComponent <ConnectableEntityBehavior>());
                selectedLink.UpdateRendering();
            }
            else if (selectedLink != null && Input.GetMouseButtonUp(0)) // release to finish adding platform,
            {
                platformsToAdd.Remove(platformToPreviewForAdd);
                platformToPreviewForAdd.transform.position = mousePointInWorld;
                platformToPreviewForAdd.gameObject.SetActive(true);
                platformToPreviewForAdd.GetComponent <BoxCollider2D>().isTrigger = false;
                platformToPreviewForAdd.isInLevel = true;                                                         // set as being added to the level.
                platformToPreviewForAdd.GetComponent <ContainerEntityBehavior>().refreshChildList();
                selectedLink.setConnectionTo(platformToPreviewForAdd.GetComponent <ConnectableEntityBehavior>()); // connect the selected link to the new platform
                platformToPreviewForAdd.updateRenderAndState();                                                   // force rendering update.
                addingPlatforms = false;
                codePanelBehavior.appendCodeText(selectedLink.getVariableName() + " = new Node();");              // append code to the generated code window.

                string actMsg = "Platform is added from link " + selectedLink.GetComponent <LoggableBehavior>().getLogID() + " at (" + mousePointInWorld.x + ", " + mousePointInWorld.y + ")";
                loggingManager.sendLogToServer(actMsg);

                selectedLink.setPreviewConnection(null);
                selectedLink.setState(LinkBehavior.State.NORMAL);
                selectedLink = null;                                     // deselect.

                hudBehavior.setPlatformsToAddText(platformsToAdd.Count); // update UI
                updateObjectiveHUDAndBlocks();
                updatePlatformEntities();
            }
        } // end addingPlatforms block


        if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(0)) // the state of clicking has not changed this frame.
        {
            hoverLink = null;                                             // to make sure it is properly updated this next frame.
        }
        // if it is a hover link and the mouse is released, then check to establish a connection.
        mousePointInWorld.z = 0;
        for (int i = 0; i < worldGenerator.levelLinks.Count; i++)
        {
            LinkBehavior lb = worldGenerator.levelLinks[i];
            if (lb.selectable)
            {
                if (lb.isPointInside(mousePointInWorld))
                {
                    if (lb.state == LinkBehavior.State.NORMAL)
                    {
                        hoverLink = lb;
                        lb.setState(LinkBehavior.State.HOVER);
                        if (selectedLink != null && hoverLink.connectableEntity != null)
                        {
                            selectedLink.setPreviewConnection(hoverLink.connectableEntity); // preview the connection if there is a select link.
                            selectedLink.UpdateRendering();
                        }
                    }

                    if (Input.GetMouseButtonDown(0)) // if the link is just being clicked.
                    {
                        if (lb.state == LinkBehavior.State.SELECTED)
                        {
                            // if the link being clicked is selected
                            if (getMsSinceLastClick() < doubleClickDelay)
                            {
                                lb.setConnectionTo(null); // remove connection on double click
                                lb.setPreviewConnection(null);
                                lb.UpdateRendering();
                                codePanelBehavior.appendCodeText(lb.getVariableName() + " = null;");
                                updatePlatformEntities(); // changing links - update platforms
                                updateObjectiveHUDAndBlocks();
                            }
                        }
                        else // clicking on a link that is NOT selected
                        {
                            selectedLink = lb;
                            lb.setState(LinkBehavior.State.SELECTED);
                            if (hoverLink == selectedLink)
                            {
                                hoverLink.setPreviewConnection(null);
                                hoverLink.UpdateRendering();
                                hoverLink = null; // hover link can't be the same as selected link.
                            }
                        }
                    }
                    else if (Input.GetMouseButtonUp(0))                                                         // if the mouse is being released over this link
                    {
                        if (lb.state == LinkBehavior.State.HOVER && selectedLink != null && selectedLink != lb) // if there IS a selected link. establish link.
                        {
                            selectedLink.setConnectionEqualTo(ref lb);
                            codePanelBehavior.appendCodeText(selectedLink.getVariableName() + " = " + lb.getVariableName() + ";");
                            selectedLink.setPreviewConnection(null);
                            selectedLink.setState(LinkBehavior.State.NORMAL);
                            lb.UpdateRendering();
                            updatePlatformEntities(); // changing links - update platforms
                            updateObjectiveHUDAndBlocks();
                        }
                    }
                }
                else
                { // mouse is NOT over link block
                    if (lb.state == LinkBehavior.State.HOVER)
                    {
                        lb.setState(LinkBehavior.State.NORMAL); // no longer a hover block.
                    }
                    else if (lb.state == LinkBehavior.State.SELECTED && !Input.GetMouseButton(0))
                    {
                        lb.setState(LinkBehavior.State.NORMAL); // no longer the selected block
                        lb.setPreviewConnection(null);          // no preview
                        lb.UpdateRendering();
                    }
                }
            }
            else
            {
                if (lb.state != LinkBehavior.State.NORMAL)
                {
                    lb.setState(LinkBehavior.State.NORMAL);
                }
            }
        } // end iterating through links.

        // validate the selected link
        if (selectedLink != null && selectedLink.state != LinkBehavior.State.SELECTED)
        {
            selectedLink = null;
        }
        // validate the hover link
        if (hoverLink != null && hoverLink.state != LinkBehavior.State.HOVER)
        {
            hoverLink = null;
        }


        // record the last time that the mouse clicked for double clicking
        if (Input.GetMouseButton(0))
        {
            lastTimeClickedMillis = System.DateTime.Now;
        }
    }