Example #1
0
    } // End of Awake().


    void Update(){
	    forwardThrottle = 0;
	    if(WesInput.GetKey("Forward"))
		    forwardThrottle += 1;
	    if(WesInput.GetKey("Backward"))
		    forwardThrottle -= 1;

        horizontalThrottle = 0;
	    if(WesInput.GetKey("Right"))
		    horizontalThrottle += 1;
	    if(WesInput.GetKey("Left"))
		    horizontalThrottle -= 1;

        verticalThrottle = 0;
	    if(WesInput.GetKey("Up"))
		    verticalThrottle += 1;
	    if(WesInput.GetKey("Down"))
		    verticalThrottle -= 1;
		
	    rotationThrottle = 0;
	    if(WesInput.GetKey("Roll Right"))
		    rotationThrottle += 1;
	    if(WesInput.GetKey("Roll Left"))
		    rotationThrottle -= 1;
    } // End of Update().
Example #2
0
    }     // End of Start().

    void Update()
    {
        // Smoothly interpolate camera position/rotation.
        transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref smoothVelTranslate, translateSmoothTime);
        Quaternion tempRot = transform.rotation;

        tempRot            = Quaternion.Slerp(tempRot, targetRot, 5 * Time.deltaTime);
        transform.rotation = tempRot;

        // Toggle camera lock/free movement with Spacebar.
        if (WesInput.GetKey("Camera Lock") && !spacePressed)
        {
            spacePressed = true;
            cameraLock   = !cameraLock;
        }

        // Toggle freelook/camera lock.
        if (!WesInput.GetKey("Camera Lock"))
        {
            spacePressed = false;
        }

        // Camera defaults to unfocused, but if an element is selected this will become true.
        cameraFocused = false;

        // Determine what the user is looking at.
        Ray        mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit cameraLookHit;

        lookedAtNode = null;
        if (Physics.Raycast(mouseRay, out cameraLookHit))
        {
            lookedAtObject = cameraLookHit.transform.gameObject;
            lookedAtNode   = lookedAtObject.GetComponent <Node>();
        }
        else
        {
            lookedAtObject = null;
        }

        // When not in free-look mode...
        if (cameraLock)
        {
            Screen.lockCursor = false;

            if (selectedAssembly != null)
            {
                cameraFocused = true;
                focusedPos    = selectedAssembly.GetCenter();
            }

            if (selectedNode != null)
            {
                cameraFocused = true;
                focusedPos    = selectedNode.transform.position;
            }

            // Rotate orbit via directional input.
            if (Input.GetMouseButton(1))
            {
                targetRot *= Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * cameraRotateSpeed, -Vector3.right);
                targetRot *= Quaternion.AngleAxis(Input.GetAxis("Mouse X") * cameraRotateSpeed, Vector3.up);
            }

            // If we are focusing on some object...
            if (cameraFocused)
            {
                // Zoom orbit with mousewheel.
                orbitDist -= (orbitDist * 0.3f) * Input.GetAxis("Mouse ScrollWheel") * orbitZoomSpeed;
                orbitDist  = Mathf.Clamp(orbitDist, 3f, Mathf.Infinity);

                targetPos = focusedPos - (targetRot * (Vector3.forward * orbitDist));
            }
            // If camera is locked but not focused...
            else
            {
                // Move in/out with mousewheel.
                targetPos += Camera.main.transform.forward * Input.GetAxis("Mouse ScrollWheel") * orbitZoomSpeed;
            }


            if (lookedAtObject)
            {
                if (lookedAtNode && Input.GetMouseButtonDown(0))
                {
                    mouseClickedNode = lookedAtNode;
                }
                else if (lookedAtNode && Input.GetMouseButtonUp(0))
                {
                    mouseReleasedNode = lookedAtNode;
                }

                // 'Selecting' a single node.
                if (mouseReleasedNode && (mouseClickedNode == mouseReleasedNode))
                {
                    Node clickAndReleaseNode = mouseReleasedNode;
                    // Select the assembly attached to the node, if applicable...
                    if ((clickAndReleaseNode.assembly != null) && (selectedAssembly != clickAndReleaseNode.assembly) && (!selectedNode || (selectedNode == clickAndReleaseNode) || (clickAndReleaseNode.assembly != selectedNode.assembly)))
                    {
                        selectedNode = null;
                        FocusOnAssembly(clickAndReleaseNode.assembly);
                    }
                    // Otherwise just select the node.
                    else
                    {
                        selectedNode     = clickAndReleaseNode;
                        selectedAssembly = null;
                        targetRot        = Quaternion.LookRotation(selectedNode.transform.position - transform.position, Camera.main.transform.up);
                    }
                }
            }


            // Create a new bond.
            if (mouseClickedNode && mouseReleasedNode && !mouseClickedNode.BondedTo(mouseReleasedNode) && (mouseClickedNode != mouseReleasedNode) && (mouseClickedNode.BondCount() < 3) && (mouseReleasedNode.BondCount() < 3))
            {
                new Bond(mouseClickedNode, mouseReleasedNode);
                ConsoleScript.NewLine("Manually created a bond.");
            }
            // Destroy an existing bond.
            else if (mouseClickedNode && mouseReleasedNode && mouseClickedNode.BondedTo(mouseReleasedNode))
            {
                mouseClickedNode.GetBondTo(mouseReleasedNode).Destroy();
                ConsoleScript.NewLine("Manually removed a bond.");
            }
        }
        // If we're in free-look mode...
        else
        {
            Screen.lockCursor = true;
            lookedAtObject    = null;
            selectedNode      = null;
            selectedAssembly  = null;

            // Pitch/yaw camera via mouse movement.
            targetRot *= Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * cameraRotateSpeed, -Vector3.right);
            targetRot *= Quaternion.AngleAxis(Input.GetAxis("Mouse X") * cameraRotateSpeed, Vector3.up);
        }

        // Roll camera using Q and E
        targetRot *= Quaternion.AngleAxis(WesInput.rotationThrottle * -cameraRotateSpeed, Vector3.forward);

        if (!cameraFocused)
        {
            // Navigation
            // Translate position with keyboard input.
            targetPos += WesInput.forwardThrottle * transform.forward * cameraMoveSpeed * Time.deltaTime;
            targetPos += WesInput.horizontalThrottle * transform.right * cameraMoveSpeed * Time.deltaTime;
            targetPos += WesInput.verticalThrottle * transform.up * cameraMoveSpeed * Time.deltaTime;
        }


        // Auto-orbit
        targetRot = Quaternion.RotateTowards(targetRot, targetRot * autoOrbit, Time.deltaTime * 2.5f);


        if (Input.GetMouseButtonUp(0))
        {
            mouseClickedNode  = null;
            mouseReleasedNode = null;
        }

        dragLineRenderer.enabled = false;
        if (mouseClickedNode && lookedAtNode)
        {
            dragLineRenderer.SetPosition(0, mouseClickedNode.transform.position);
            dragLineRenderer.SetPosition(1, lookedAtNode.transform.position);

            if (mouseClickedNode.BondedTo(lookedAtNode))
            {
                dragLineRenderer.SetColors(Color.yellow, Color.red);
                dragLineRenderer.enabled = true;
            }
            else if ((mouseClickedNode.BondCount() < 3) && (lookedAtNode.BondCount() < 3))
            {
                dragLineRenderer.SetColors(Color.green, Color.white);
                dragLineRenderer.enabled = true;
            }
        }
    } // End of Update().
Example #3
0
    // Use this for initialization
    void Update()
    {
        string command = "";

        string[] commandArgs = new string[0];

        if (!active)
        {
            consoleKeyCleared = false;
            if (WesInput.GetKeyDown("Open Console"))
            {
                active          = true;
                WesInput.active = false;
            }

            // Keyboard shortcuts
            if (WesInput.GetKeyDown("Disband Assembly"))
            {
                command = "disband";
            }

            if (WesInput.GetKeyDown("Add Node"))
            {
                command = "addnode";
            }

            if (WesInput.GetKeyDown("Remove Node"))
            {
                command = "remnode";
            }

            if (WesInput.GetKeyDown("Auto Orbit"))
            {
                command = "orbit";
            }

            if (WesInput.GetKeyDown("Quit"))
            {
                command = "quit";
            }

            if (WesInput.GetKeyDown("Reload Application"))
            {
                command = "reload";
            }

            if (WesInput.GetKeyDown("Replicate Assembly"))
            {
                command = "replicate";
            }
        }
        else if (active)
        {
            /*
             *
             * if(Input.GetKeyDown(KeyCode.Backspace) && (inputText.Length > 0))
             *  inputText = inputText.Substring(0, inputText.Length - 1);
             *
             * foreach(char aChar in Input.inputString){
             *      if(aChar != '\b'){
             *      inputText += aChar;
             *  }
             * }
             *
             * if(!consoleKeyCleared){
             *  inputText = "";
             *  consoleKeyCleared = true;
             * }
             */

            // Enter a line.
            Input.eatKeyPressOnTextFieldFocus = false;
            if (Input.GetKeyDown(KeyCode.Return))
            {
                if (inputText != "")
                {
                    commandArgs = inputText.Split(' ');
                    for (int i = 0; i < commandArgs.Length; i++)
                    {
                        commandArgs[i] = commandArgs[i].Trim();
                    }
                    command = commandArgs[0];
                }
                else
                {
                    // (If no command was entered, display no response.)
                    inputText       = "";
                    active          = false;
                    WesInput.active = true;
                }
            }
        }

        if (command != "")
        {
            // Console commands --------------------------------------------------------

            Node     selectedNode     = CameraControl.selectedNode;
            Assembly selectedAssembly = CameraControl.selectedAssembly;

            if (command == "load")
            {
                // Load an assembly (by index or name).
                string loadAssemblyName = "";
                int    loadAssemblyNum  = 0;

                DirectoryInfo dir       = new DirectoryInfo(IOHelper.defaultSaveDir);
                FileInfo[]    info      = dir.GetFiles("*.*");
                bool          foundFile = false;

                // Load assembly by index...
                if (int.TryParse(commandArgs[1], out loadAssemblyNum))
                {
                    NewLine("Attempting to load index " + loadAssemblyNum + "...");

                    for (int i = 0; i < info.Length; i++)
                    {
                        FileInfo currentFile     = info[i];
                        string   currentFileName = currentFile.Name;
                        int      currentFileNum  = int.Parse(currentFileName.Substring(0, 3));
                        if (currentFileNum == loadAssemblyNum)
                        {
                            new Assembly(IOHelper.defaultSaveDir + currentFileName);
                            NewLine("Done.");
                            foundFile = true;
                            break;
                        }
                    }
                    if (!foundFile)
                    {
                        NewLine("Assembly not found in " + IOHelper.defaultSaveDir + " at index " + loadAssemblyNum + ".");
                    }
                }
                // Load assembly by name...
                else if (commandArgs.Length > 0)
                {
                    loadAssemblyName = commandArgs[1];
                    NewLine("Attempting to load '" + loadAssemblyName + "'...");

                    for (int i = 0; i < info.Length; i++)
                    {
                        FileInfo currentFile     = info[i];
                        string   currentFileName = currentFile.Name;
                        if ((currentFileName.Length > (loadAssemblyName.Length + 4)) && currentFileName.Substring((currentFileName.Length - loadAssemblyName.Length) - 4, loadAssemblyName.Length) == loadAssemblyName)
                        {
                            new Assembly(IOHelper.defaultSaveDir + currentFileName);
                            NewLine("Done.");
                            foundFile = true;
                            break;
                        }
                    }
                    if (!foundFile)
                    {
                        NewLine("Assembly '" + loadAssemblyName + "' not found in " + IOHelper.defaultSaveDir + ".");
                    }
                }
                else
                {
                    NewLine("Enter a save index or Assembly name.");
                }
                Clear();
            }

            else if (command == "loaddir")
            {
                if (commandArgs.Length > 0)
                {
                    IOHelper.LoadDirectory(commandArgs[1]);
                }
                else
                {
                    NewLine("No directory path found");
                }
                Clear();
            }

            else if (command == "clear")
            {
                // Clear entire simulation
                GameManager.ClearAll();
                NewLine("Cleared the world.");
                Clear();
            }

            else if (command == "orbit")
            {
                // Enable/disable auto-orbiting.
                if (CameraControl.autoOrbit == Quaternion.identity)
                {
                    CameraControl.OrbitOn();
                    NewLine("Auto-orbit enabled.");
                }
                else
                {
                    CameraControl.OrbitOff();
                    NewLine("Auto-orbit disabled.");
                }
                Clear();
            }

            else if (command == "save")
            {
                // Save selected assembly...
                if (selectedAssembly != null)
                {
                    NewLine("Assembly saved to " + selectedAssembly.Save());
                }
                else
                {
                    NewLine("No assembly selected!");
                }
                Clear();
            }

            else if (command == "addnode")
            {
                // Other command...
                GameObject newNode    = Object.Instantiate(GameManager.prefabs.node, Camera.main.transform.position + (Camera.main.transform.forward * 3.0f), Quaternion.identity) as GameObject;
                Node       nodeScript = newNode.GetComponent <Node>();
                lines.Add("Created a new node.");
                if (selectedNode)
                {
                    if (selectedNode.bonds.Count < 3)
                    {
                        new Bond(CameraControl.selectedNode, nodeScript);
                    }
                }

                CameraControl.selectedNode = nodeScript;
                Clear();
            }
            else if (command == "mutate")
            {
                // Mutate the selected assembly...
                if (selectedAssembly != null)
                {
                    selectedAssembly.Mutate();
                    NewLine("Assembly " + selectedAssembly.name + " mutated!");
                }
                else
                {
                    NewLine("No assembly selected!");
                }
                Clear();
            }

            else if (command == "quit")
            {
                // Quit the game...
                Application.Quit();
                NewLine("Quitting...");
                Clear();
            }

            else if (command == "reload")
            {
                // Reload the game...
                Application.LoadLevel(0);
                NewLine("Reloading application...");
                Clear();
            }

            else if (command == "remnode")
            {
                if (selectedNode)
                {
                    selectedNode.Destroy();
                    lines.Add("Removed a node.");
                }
                else
                {
                    lines.Add("Select a node first!");
                }

                Clear();
            }

            else if (command == "disband")
            {
                if (selectedAssembly != null)
                {
                    lines.Add("Disbanded " + selectedAssembly.name + ".");
                    selectedAssembly.Disband();
                }
                else
                {
                    lines.Add("Select an assembly first!");
                }

                Clear();
            }

            else if (command == "rename")
            {
                if (selectedAssembly == null)
                {
                    lines.Add("Select an assembly first!");
                }
                else if ((commandArgs.Length < 2) || (commandArgs[1] == ""))
                {
                    lines.Add("Please enter a name for the assembly.");
                }
                else if (selectedAssembly != null)
                {
                    lines.Add("Renamed " + selectedAssembly.name + " to " + commandArgs[1] + ".");
                    selectedAssembly.name = commandArgs[1];
                }
                Clear();
            }

            else if (command == "controls")
            {
                foreach (KeyValuePair <string, KeyCode> item in WesInput.keys)
                {
                    lines.Add(item.Key + " [" + item.Value + "]");
                }
                Clear();
            }

            else if (command == "replicate")
            {
                if (selectedAssembly != null)
                {
                    lines.Add("Replicated " + selectedAssembly.name + ".");
                    selectedAssembly.Replicate();
                }
                else
                {
                    lines.Add("Select an assembly first!");
                }

                Clear();
            }

            else if (command == "other")
            {
                // Other command...
                // Other command code...
                Clear();
            }


            // More commands go here!

            else if (inputText != "")
            {
                // If command is not recognized, say so and do nothing.
                NewLine("Unknown command '" + command + "'");
                Clear();
            }
        }
    }