Exemplo n.º 1
0
    private void Update()
    {
        Ray     ray                = camera.ScreenPointToRay(Input.mousePosition); //makes a ray to move along
        Vector3 dir                = ray.direction;                                //gets the direction the ray is going to so that the perpendicular normal ray can bbe taken to get the straffe ray.
        Vector3 left               = Vector3.Cross(dir, Vector3.up).normalized;
        Vector3 right              = -left;                                        //going right vector that is added just opposite of left
        Vector3 previousPos        = player.transform.position;                    //get reference for previous position so that if the new position does not fit inside the bounds, the old position cen be used
        Vector3 bottomCornerBounds = new Vector3(550, 940, 740);                   //min bound
        Vector3 topCornerBounds    = new Vector3(1240, 1000, 1365);                //max bound

        if (!chatfieldINF.isFocused)
        {
            if (Input.GetKey(KeyCode.W))//W - Forward 1 Unit
            {
                dist = 1;
                player.transform.position = ray.GetPoint(dist);
            }
            else if (Input.GetKey(KeyCode.S))
            {//S - Backward 1 Unit
                dist = -1;
                player.transform.position = ray.GetPoint(dist);
            }
            if (Input.GetKey(KeyCode.D))//D - Right 1 unit
            {
                player.transform.position = player.transform.position + right;
            }
            else if (Input.GetKey(KeyCode.A))//A- Left on unit
            {
                player.transform.position = player.transform.position + left;
            }
        }
        if (!PositionIsInBox(player.transform.localPosition, bottomCornerBounds, topCornerBounds))// check if new position is not in box, if so set position back to previous
        {
            player.transform.position = previousPos;
        }
        //Send cam position
        if (Time.realtimeSinceStartup - last_update > 1)
        {
            List <float> camPos = new List <float> {
                player.transform.position.x, player.transform.position.y, player.transform.position.z
            };
            networkMan.Send_message("Camera", networkMan.Encode_XML(camPos, typeof(List <float>)), null, null);
            last_update = Time.realtimeSinceStartup;
        }
    }
Exemplo n.º 2
0
    public void Update()
    {
        //Get Ray to start raycast
        Ray        ray = Camera.main.ScreenPointToRay(draggingPos);
        RaycastHit hit;

        draggingPos = Input.mousePosition;                     // position of mouse dragging and in conjunction with is Selecting created a dragging state
        if (Input.GetMouseButtonDown(0) && !collideMan.onMenu) //if user clicks down on game scene is selecting becomes true and the initial position of mouse is set
        {
            isSelecting = true;
            clickedPos  = Input.mousePosition;
        }
        else if (Input.GetMouseButtonDown(0) && collideMan.SpawnHovering != "")//Spawn a unit if user clicks down on panel to create it
        {
            Debug.Log("Spawn " + collideMan.SpawnHovering);
            Debug.Log(spawnatBuild);
            List <string> messageToSend = new List <string> {
                collideMan.SpawnHovering, spawnatBuild
            };
            networkMan.Send_message("Unit Adjustment", "Buy", networkMan.Encode_XML(messageToSend, typeof(List <string>)), "");
        }
        else if (Input.GetKeyDown(KeyCode.X) && displayInfo.Displaying != "" && int.Parse(displayInfo.Displaying.Substring(0, 1)) == playerIndex)//Sell Unit if X is clicked down
        {
            Debug.Log("Sell " + displayInfo.Displaying);
            networkMan.Send_message("Unit Adjustment", "Sell", networkMan.Encode_XML(new List <string> {
                displayInfo.Displaying
            }, typeof(List <string>)), "");
        }
        else if (Input.GetMouseButtonDown(0) && collideMan.MouseHovering != "")//Remove the unit from panel if he clicks on a unit that has already been selected in the panel
        {
            Debug.Log(1);
            selectedUnits.Remove(collideMan.MouseHovering);
            collideMan.MouseHovering = "";
        }

        else if (Input.GetMouseButtonUp(0))
        {
            if (draggingPos == clickedPos)                                                                        // if the user did not move mouse and thus did not drag
            {
                if (Physics.Raycast(ray, out hit))                                                                //check if ray hit something
                {
                    Transform AdultObj = hit.collider.transform.parent;                                           //As meshes were used the adult of the mesh component (ie. arm) should be used

                    if (AdultObj.parent == UnitFolder && int.Parse(AdultObj.name.Substring(0, 1)) == playerIndex) //if the parent is in unit folder and the object is yours
                    {
                        string   unitName     = buildMan.UnitOrder[int.Parse(AdultObj.name.Substring(2, 3))];     //get the unit name from characters from the Unique Identifier that allows the Unit Order dictionary to distinguish which unit it is
                        UnitInfo extraDetails = buildMan.UnitDetails[unitName];
                        if (extraDetails.unit_type == 0)                                                          // if item is unit
                        {
                            if (!selectedUnits.Contains(hit.collider.transform.parent.name))                      //if the user clicked on a unit, if they did not select it already, it becomes selected, otherwise it becomes unselected
                            {
                                selectedUnits.Add(hit.collider.transform.parent.name);
                            }

                            else
                            {
                                Debug.Log("single selection");
                                selectedUnits.Remove(hit.collider.transform.parent.name);
                            }
                        }
                        else if (!collideMan.onMenu)//if user clicks on building. selected units gets cleared and options pop up for what user can do with building.(i.e spawn units)
                        {
                            Debug.Log(2);
                            selectedUnits = new List <string> {
                            };
                            selectedBuild = unitName;
                            spawnatBuild  = AdultObj.name;
                            displayInfo.makeBuildDetails(spawnatBuild);
                        }
                    }
                    else if (selectedUnits.Count != 0 && !collideMan.onMenu)// if user clicks down on game scene and has some selected units, the units selected move toward the position that was clicked.
                    {
                        List <float> positionToMove = new List <float> {
                            hit.point.x, hit.point.y, hit.point.z
                        };
                        networkMan.Send_message("Unit Adjustment", "Move", networkMan.Encode_XML(selectedUnits, typeof(List <string>)), networkMan.Encode_XML(positionToMove, typeof(List <float>)));
                    }
                }
            }
            else
            {
                Debug.Log("box selection");
                //Box Selection - if user dragged mouse box selection is done
                selectedUnits = new List <string>();//selected units is cleared and all units that user owns within the square he dragged is selected instead
                foreach (Transform child in UnitFolder)
                {
                    string   unitName     = buildMan.UnitOrder[int.Parse(child.name.Substring(2, 3))];
                    UnitInfo extraDetails = buildMan.UnitDetails[unitName];
                    if (IsWithinSelectionBounds(child.gameObject) && int.Parse(child.name.Substring(0, 1)) == playerIndex && extraDetails.unit_type == 0)
                    {
                        if (!selectedUnits.Contains(child.name))
                        {
                            selectedUnits.Add(child.name);
                        }
                    }
                }
            }
            foreach (string item in selectedUnits)//make panels in selection panel ui for all units that were selected
            {
                if (selectionPan.transform.Find(item) == null)
                {
                    GameObject itemBox  = Instantiate(originalPan, selectionPan.transform);
                    string     unitName = buildMan.UnitOrder[int.Parse(item.Substring(2, 3))];
                    itemBox.GetComponent <Image>().sprite = spriteHolder.transform.Find(unitName).GetComponent <Image>().sprite;
                    itemBox.name = item;
                    itemBox.SetActive(true);
                }
            }
            isSelecting = false;//dragging becomes false after user stops clicking down
        }

        else if (collideMan.MouseHovering != "")//if user hover on unit in panel, display info of that unit
        {
            if (displayInfo.Displaying != collideMan.MouseHovering)
            {
                displayInfo.displayUnitPanel(collideMan.MouseHovering);
            }
        }
        else if (Physics.Raycast(ray, out hit))//if user hovers on unit in game scene display info
        {
            if (displayInfo.Displaying != hit.collider.transform.name)
            {
                if (hit.collider.transform.parent.parent == UnitFolder)
                {
                    displayInfo.displayUnitPanel(hit.collider.transform.parent.name);
                }
                else
                {
                    displayInfo.hideUnitPanel();
                }
            }
        }
        else if (collideMan.MouseHovering == "")//if user not hovering, hide info panel
        {
            displayInfo.hideUnitPanel();
        }
        foreach (Transform child in selectionPan.transform)//for all items in selection panel that were removed from the selected units, the selection panel must remove those as well
        {
            if (!selectedUnits.Contains(child.name))
            {
                GameObject.Destroy(child.gameObject);
            }
        }
    }
Exemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && collideMan.BuildHovering != "" && buildObject == null)//when user clicks on a building to build attach it to the mouse so user can place building
        {
            Building = collideMan.BuildHovering;
            GameObject build = ModelUnits.Find(collideMan.BuildHovering).gameObject;
            buildObject = Instantiate(build);
            buildObject.transform.name = collideMan.BuildHovering;
        }
        else if (Input.GetMouseButtonDown(0) && collideMan.BuildHovering != "" && buildObject != null && collideMan.BuildHovering != Building)//user chooses new building
        {
            GameObject.Destroy(buildObject);
            Building = collideMan.BuildHovering;
            GameObject build = ModelUnits.Find(collideMan.BuildHovering).gameObject;
            buildObject = Instantiate(build);
        }
        else if (Input.GetMouseButtonDown(0) && buildObject != null) //user tries building a building
        {
            if (isValid)                                             //build the building if the placement is valid
            {
                //Send message to make building with certain position
                List <float> positionofBuild = new List <float> {
                    buildObject.transform.position.x, buildObject.transform.position.y, buildObject.transform.position.z
                };
                networkMan.Send_message("Unit Adjustment", "Build", networkMan.Encode_XML(new List <string> {
                    Building
                }, typeof(List <string>)), networkMan.Encode_XML(positionofBuild, typeof(List <float>)));
                GameObject.Destroy(buildObject);
                buildObject = null;
                Building    = "";
                isValid     = false;
            }
        }
        else if (Input.GetMouseButtonDown(1) && buildObject != null)//if user right clicks destroy the object and deselect the building
        {
            isValid  = false;
            Building = "";
            GameObject.Destroy(buildObject);
            buildObject = null;
        }
        else if (collideMan.BuildHovering != "")//display the info of a building if user is hovering over it in build panel
        {
            if (displayInfo.Displaying != collideMan.BuildHovering)
            {
                displayInfo.displayBuildPanel(collideMan.BuildHovering);
            }
        }
        else//user if hovering with building
        {
            displayInfo.hideBuildPanel();//hide display info
            if (Building != "")// if building
            {
                RaycastHit hit;
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);//get point user is trying to build
                if (Physics.Raycast(ray, out hit) && buildObject != null)
                {
                    buildObject.transform.position = hit.point;                                                       //move position to the point user is trying to build
                    isValid = false                                                                                   //set valid to false and then make it true if position is valid

                              if (buildObject.transform.GetChild(0).GetComponent <Renderer>().material != invalidMat) //make building have have material that is invalid
                    {
                        foreach (Transform chil in buildObject.transform)
                        {
                            chil.GetComponent <Renderer>().material = invalidMat;
                        }
                    }

                    foreach (Transform child in EnergyCrys)
                    {