Beispiel #1
0
    public void LoadRobot()
    {
        string data = PlayerPrefs.GetString("robotData" + RobotName);

        try
        {
            foreach (string line in data.Split('#'))
            {
                string[] objectParameters = line.Split('|');

                int        objectIndex       = int.Parse(objectParameters[0]);
                int        objectPrefabIndex = int.Parse(objectParameters[1]);
                Vector2    position          = Utils.SerializeVector2(objectParameters[2]);
                Quaternion rotation          = Utils.SerializeQuaternion(objectParameters[3]);

                GameObject go = Instantiate(objects[objectPrefabIndex], position, rotation);

                ExportObjectData dat = go.GetComponent <ExportObjectData>();
                dat.objectPrefabIndex = objectPrefabIndex;
                dat.objectIndex       = objectIndex;

                instantiatedObjects.Add(go);

                //string[] jointIndexes = objectParameters[4].Split(',');
            }
        }
        catch { }

        foreach (string line in data.Split('#'))
        {
            try
            {
                string[] objectParameters = line.Split('|');

                int        objectIndex       = int.Parse(objectParameters[0]);
                int        objectPrefabIndex = int.Parse(objectParameters[1]);
                Vector2    position          = Utils.SerializeVector2(objectParameters[2]);
                Quaternion rotation          = Utils.SerializeQuaternion(objectParameters[3]);
                string[]   jointIndexes      = objectParameters[4].Split(',');

                foreach (string jointData in jointIndexes)
                {
                    int  indexInt = int.Parse(jointData.Split('.')[0]);
                    bool isHinged = jointData.Split('.')[1] == "h";

                    if (isHinged)
                    {
                        NonPhysicalJoint joint = instantiatedObjects[objectIndex].AddComponent <NonPhysicalJoint>();
                        joint.IsHingedJoint = true;
                        joint.SetConnectedObject(instantiatedObjects[indexInt]);
                        if (isPausedNow)
                        {
                            joint.Pause();
                        }
                        else
                        {
                            joint.Unpause();
                        }

                        LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();
                        liner.materialToSet = linerHingedMaterial;
                        liner.attachedJoint = joint;
                        liner.object1       = instantiatedObjects[indexInt];
                        liner.object2       = instantiatedObjects[objectIndex];
                    }
                    else
                    {
                        NonPhysicalJoint joint = instantiatedObjects[objectIndex].AddComponent <NonPhysicalJoint>();
                        joint.IsHingedJoint = false;
                        joint.SetConnectedObject(instantiatedObjects[indexInt]);
                        if (isPausedNow)
                        {
                            joint.Pause();
                        }
                        else
                        {
                            joint.Unpause();
                        }

                        LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();
                        liner.materialToSet = linerFixedMaterial;
                        liner.attachedJoint = joint;
                        liner.object1       = instantiatedObjects[indexInt];
                        liner.object2       = instantiatedObjects[objectIndex];
                    }
                }
            }
            catch
            {
                continue;
            }
        }
        try
        {
            string brainDataStr  = PlayerPrefs.GetString("brainData" + RobotName);
            string motorDataStr  = brainDataStr.Split(' ')[0];
            string sensorDataStr = brainDataStr.Split(' ')[1];

            int i = 0;
            foreach (string objIndexStr in motorDataStr.Split(','))
            {
                int ind = int.Parse(objIndexStr);
                if (ind != -1)
                {
                    brainData.connectedMotors[i] = instantiatedObjects[ind];
                    instantiatedObjects[ind].GetComponent <ExportObjectData>().connectedPort = i;
                }
                i++;
            }

            i = 0;
            foreach (string objIndexStr in sensorDataStr.Split(','))
            {
                int ind = int.Parse(objIndexStr);
                if (ind != -1)
                {
                    brainData.connectedSensors[i] = instantiatedObjects[ind];
                    instantiatedObjects[ind].GetComponent <ExportObjectData>().connectedPort = i;
                }
                i++;
            }
        }
        catch {}
        string codeData = @"
		-- Code for the robot is written in Lua scripting language
		function start()
			-- Functions to execute on the launch of the robot
		end

		function loop()
			-- Functions to execute repeatedly
			SetMotor(0, GetLeftJoystickX());
			SetMotor(1, GetLeftJoystickX());
		end"        ;

        codeField.text = PlayerPrefs.GetString("codeData" + RobotName, codeData);
    }
Beispiel #2
0
    void Update()
    {
        if (Input.touchCount > 0)                                                                                                                       //if something is touched
        {
            Touch   touch         = Input.GetTouch(0);                                                                                                  //Get touch
            Vector2 touchPosition = touch.position;                                                                                                     //Get touch position
            Vector3 wp            = Camera.main.ScreenToWorldPoint(touchPosition);
            wp.z = 0;

            RaycastHit2D hit = Physics2D.BoxCast(wp, Vector2.one * 0.1f, 0f, Vector2.zero);                     //hit = the object that ray hit by being sent from touch position

            if (hit)
            {
                Debug.Log(hit.transform.name);
            }

            if (touch.phase == TouchPhase.Began)                                                                                                        //If user starts pressing (Touch down)
            {
                //If we just started pressing (1 frame when user clicked)
                if (hit && !IsPointerOverUIObject())                                                                            //If If ray did touch any Geme object(not UI)
                {
                    hitObjectOnStart        = hit.transform.gameObject;                                                         //get touched gameobject
                    hitAndObjectCenterDelta = hit.transform.position - Camera.main.ScreenToWorldPoint(touchPosition);           //get delta beween touch position and gameobject

                    if (focusedObject && focusedObject == hit.transform.gameObject)                                             //if the object that we hit is the focused object
                    {
                        isHitOnFocusedObject = true;                                                                            //focused object was touched
                    }
                    else
                    {
                        isHitOnFocusedObject = false;                                                                                                           //focused object was not touched
                    }
                }
                else                                                                                                                                                                    //no objects were touched

                {
                    hitObjectOnStart        = null;
                    isHitOnFocusedObject    = false;
                    hitAndObjectCenterDelta = Vector2.zero;
                }

                startTime = Time.time;                                                                                   //starting time of the click = current time
            }
            else if (touch.phase == TouchPhase.Ended)                                                                    //else if we just finished pressing (Touch up)
            {
                if (Time.time - startTime < 0.1f && hit && hitObjectOnStart == hit.transform.gameObject)                 //if we tapped an object
                {
                    if (IsNowAddingRigidLink)                                                                            //if something is ready to be linked
                    {
                        bool ok = true;
                        foreach (NonPhysicalJoint j in focusedObject.GetComponents <NonPhysicalJoint>())                        // consider all joints of focused object one by one
                        {
                            if (j.connectedObject == hitObjectOnStart)                                                          //A joint was already added to this object
                            {
                                ok = false;                                                                                     // it is no tpossible to add link
                                DisplayError("A joint was already added to this object");                                       //Dislay the error
                            }
                        }
                        foreach (NonPhysicalJoint j in hitObjectOnStart.GetComponents <NonPhysicalJoint>())                     // consider all joints of hitted object one by one
                        {
                            if (j.connectedObject == focusedObject)                                                             //A joint was already added to this object
                            {
                                ok = false;                                                                                     // it is not possible to add link
                                DisplayError("A joint was already added to this object");                                       //Display the error
                            }
                        }
                        if (focusedObject == hitObjectOnStart)                                                                   //attempt to attach an object to itself
                        {
                            ok = false;                                                                                          // it is not possible to add link
                            DisplayError("You cannot attach an object to itself");                                               //Display the error
                        }
                        if (ok)                                                                                                  //if it is possible to add link
                        {
                            NonPhysicalJoint joint = focusedObject.AddComponent <NonPhysicalJoint>();                            //create a joint
                            joint.IsHingedJoint = false;
                            joint.SetConnectedObject(hit.transform.gameObject);                                                  //join focused object to currently touched object
                            if (isPausedNow)
                            {
                                joint.Pause();                                                                                                                                  //move together
                            }
                            else
                            {
                                joint.Unpause();                                                                                                                                //move seperatly
                            }

                            LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();                            //create a line between focused and currently touched object
                            liner.materialToSet = linerFixedMaterial;
                            liner.object1       = hit.transform.gameObject;
                            liner.attachedJoint = joint;
                            liner.object2       = focusedObject;
                        }


                        hitObjectOnStart        = null;
                        IsNowAddingRigidLink    = false;
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                        addingJointImage.GetComponent <Animation>().Play("NowAddingPanelSlideOut");                             //play animation of link adding
                    }
                    else if (IsNowAddingHingedLink)                                                                             //if we are adding link as a hinge
                    {
                        bool ok = true;
                        foreach (NonPhysicalJoint j in focusedObject.GetComponents <NonPhysicalJoint>())                        // consider all joints of hitted object one by one
                        {
                            if (j.connectedObject == hitObjectOnStart)                                                          //if A joint was already added to this object
                            {
                                ok = false;                                                                                     // it is not possible to add link
                                DisplayError("A joint was already added to this object");                                       //Dislay the error
                            }
                        }
                        foreach (NonPhysicalJoint j in hitObjectOnStart.GetComponents <NonPhysicalJoint>())
                        {
                            if (j.connectedObject == focusedObject)                                                                             //if a joint was already added to this object
                            {
                                ok = false;                                                                                                     // it is not possible to add link
                                DisplayError("A joint was already added to this object");                                                       //Dislay the error
                            }
                        }
                        if (ok)
                        {
                            if (focusedObject == hitObjectOnStart)                                                                              //attempt to attach an object to itself
                            {
                                ok = false;                                                                                                     // it is not possible to add link
                                DisplayError("You cannot attach an object to itself");                                                          //Dislay the error
                            }

                            NonPhysicalJoint joint = focusedObject.AddComponent <NonPhysicalJoint>();                             //create a joint
                            joint.SetConnectedObject(hit.transform.gameObject);
                            joint.IsHingedJoint = true;                                                                           //it is hinge joint
                            if (isPausedNow)
                            {
                                joint.Pause();
                            }
                            else
                            {
                                joint.Unpause();
                            }

                            LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();
                            liner.materialToSet = linerHingedMaterial;
                            liner.object1       = hit.transform.gameObject;
                            liner.attachedJoint = joint;
                            liner.object2       = focusedObject;
                        }

                        hitObjectOnStart        = null;
                        IsNowAddingHingedLink   = false;
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                        addingJointImage.GetComponent <Animation>().Play("NowAddingPanelSlideOut");
                    }
                    else
                    {
                        hitObjectOnStart = null;
                        SetObjectAsFocused(hit.transform.gameObject);
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                    }
                }
                isHitOnFocusedObject = false;

                AllowCameraMovement = true;
            }
            else
            {
                //If we are moving our finger
                if (isHitOnFocusedObject)
                {
                    Vector3 worldPos = Camera.main.ScreenToWorldPoint(touchPosition);
                    worldPos.z = 0;

                    /*bool pivoted = false;
                     * foreach (GameObject pivot in pivotPoints)
                     * {
                     *      //Debug.Log((pivot.transform.position - worldPos).magnitude);
                     *      if (pivot != focusedObject && (pivot.transform.position - worldPos).magnitude < 0.5f && pivot.GetComponent<ExportObjectData>().hingedObject == null)
                     *      {
                     *              pivot.GetComponent<ExportObjectData>().hingedObject = focusedObject;
                     *              focusedObject.GetComponent<ExportObjectData>().hingedObject = focusedObject;
                     *              pivoted = true;
                     *              worldPos = pivot.transform.position + (Vector3)hitAndObjectCenterDelta;
                     *              break;
                     *      }
                     * }*/
                    worldPos += (Vector3)hitAndObjectCenterDelta;
                    RoundVector(ref worldPos);
                    worldPos.z = 0;
                    focusedObject.transform.position = worldPos;
                    AllowCameraMovement = false;
                }
            }
        }

        if (AllowCameraMovement && !IsPointerOverUIObject())
        {
            controller.UpdateCamera();
        }

        if (focusedObject == null)
        {
            linkHingeButton.interactable = false;
            linkFixedButton.interactable = false;
            deleteButton.interactable    = false;
            rotateCCWButton.interactable = false;
            rotateCWButton.interactable  = false;
        }
        else
        {
            linkHingeButton.interactable = true;
            linkFixedButton.interactable = true;
            deleteButton.interactable    = true;
            rotateCCWButton.interactable = true;
            rotateCWButton.interactable  = true;
        }

        if (focusedObjectData != null && focusedObjectData.isMotorPort)
        {
            for (int i = 0; i < 8; i++)
            {
                if (brainData.connectedMotors[i] == null)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = false;
                }
                else if (i == focusedObjectData.connectedPort)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = true;
                }
                else
                {
                    portCheckboxList[i].interactable = false;
                    portCheckboxList[i].isOn         = false;
                }
            }
        }
        else if (focusedObjectData != null && focusedObjectData.isSensorPort)
        {
            for (int i = 0; i < 8; i++)
            {
                if (brainData.connectedSensors[i] == null)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = false;
                }
                else if (i == focusedObjectData.connectedPort)

                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = true;
                }
                else
                {
                    portCheckboxList[i].interactable = false;
                    portCheckboxList[i].isOn         = false;
                }
            }
        }
    }
Beispiel #3
0
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch   touch         = Input.GetTouch(0);
            Vector2 touchPosition = touch.position;
            Vector3 wp            = Camera.main.ScreenToWorldPoint(touchPosition);
            wp.z = 0;

            RaycastHit2D hit = Physics2D.BoxCast(wp, Vector2.one * 0.1f, 0f, Vector2.zero);

            if (hit)
            {
                Debug.Log(hit.transform.name);
            }

            if (touch.phase == TouchPhase.Began)
            {
                //If we just started pressing (1 frame when user clicked)
                if (hit && !IsPointerOverUIObject())
                {
                    hitObjectOnStart        = hit.transform.gameObject;
                    hitAndObjectCenterDelta = hit.transform.position - Camera.main.ScreenToWorldPoint(touchPosition);

                    if (focusedObject && focusedObject == hit.transform.gameObject)
                    {
                        isHitOnFocusedObject = true;
                    }
                    else
                    {
                        isHitOnFocusedObject = false;
                    }
                }
                else
                {
                    hitObjectOnStart        = null;
                    isHitOnFocusedObject    = false;
                    hitAndObjectCenterDelta = Vector2.zero;
                }

                startTime = Time.time;
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                //If we just ended pressing (1 frame when user releasd his finger)
                if (Time.time - startTime < 0.1f && hit && hitObjectOnStart == hit.transform.gameObject)
                {
                    if (IsNowAddingRigidLink)
                    {
                        bool ok = true;
                        foreach (NonPhysicalJoint j in focusedObject.GetComponents <NonPhysicalJoint>())
                        {
                            if (j.connectedObject == hitObjectOnStart)
                            {
                                ok = false;
                                DisplayError("A joint was already added to this object");
                            }
                        }
                        foreach (NonPhysicalJoint j in hitObjectOnStart.GetComponents <NonPhysicalJoint>())
                        {
                            if (j.connectedObject == focusedObject)
                            {
                                ok = false;
                                DisplayError("A joint was already added to this object");
                            }
                        }
                        if (focusedObject == hitObjectOnStart)
                        {
                            ok = false;
                            DisplayError("You cannot attach an object to itself");
                        }
                        if (ok)
                        {
                            NonPhysicalJoint joint = focusedObject.AddComponent <NonPhysicalJoint>();
                            joint.IsHingedJoint = false;
                            joint.SetConnectedObject(hit.transform.gameObject);
                            if (isPausedNow)
                            {
                                joint.Pause();
                            }
                            else
                            {
                                joint.Unpause();
                            }

                            LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();
                            liner.materialToSet = linerFixedMaterial;
                            liner.object1       = hit.transform.gameObject;
                            liner.attachedJoint = joint;
                            liner.object2       = focusedObject;
                        }


                        hitObjectOnStart        = null;
                        IsNowAddingRigidLink    = false;
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                        addingJointImage.GetComponent <Animation>().Play("NowAddingPanelSlideOut");
                    }
                    else if (IsNowAddingHingedLink)
                    {
                        bool ok = true;
                        foreach (NonPhysicalJoint j in focusedObject.GetComponents <NonPhysicalJoint>())
                        {
                            if (j.connectedObject == hitObjectOnStart)
                            {
                                ok = false;
                                DisplayError("A joint was already added to this object");
                            }
                        }
                        foreach (NonPhysicalJoint j in hitObjectOnStart.GetComponents <NonPhysicalJoint>())
                        {
                            if (j.connectedObject == focusedObject)
                            {
                                ok = false;
                                DisplayError("A joint was already added to this object");
                            }
                        }
                        if (ok)
                        {
                            if (focusedObject == hitObjectOnStart)
                            {
                                ok = false;
                                DisplayError("You cannot attach an object to itself");
                            }

                            NonPhysicalJoint joint = focusedObject.AddComponent <NonPhysicalJoint>();
                            joint.SetConnectedObject(hit.transform.gameObject);
                            joint.IsHingedJoint = true;
                            if (isPausedNow)
                            {
                                joint.Pause();
                            }
                            else
                            {
                                joint.Unpause();
                            }

                            LineBetweenTwoObjects liner = Instantiate(linerPrefab, Vector3.zero, Quaternion.identity).GetComponent <LineBetweenTwoObjects>();
                            liner.materialToSet = linerHingedMaterial;
                            liner.object1       = hit.transform.gameObject;
                            liner.attachedJoint = joint;
                            liner.object2       = focusedObject;
                        }

                        hitObjectOnStart        = null;
                        IsNowAddingHingedLink   = false;
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                        addingJointImage.GetComponent <Animation>().Play("NowAddingPanelSlideOut");
                    }
                    else
                    {
                        hitObjectOnStart = null;
                        SetObjectAsFocused(hit.transform.gameObject);
                        hitAndObjectCenterDelta = Vector2.zero;
                        startTime = 0f;
                    }
                }
                isHitOnFocusedObject = false;

                AllowCameraMovement = true;
            }
            else
            {
                //If we are moving our finger
                if (isHitOnFocusedObject)
                {
                    Vector3 worldPos = Camera.main.ScreenToWorldPoint(touchPosition);
                    worldPos.z = 0;

                    /*bool pivoted = false;
                     * foreach (GameObject pivot in pivotPoints)
                     * {
                     *      //Debug.Log((pivot.transform.position - worldPos).magnitude);
                     *      if (pivot != focusedObject && (pivot.transform.position - worldPos).magnitude < 0.5f && pivot.GetComponent<ExportObjectData>().hingedObject == null)
                     *      {
                     *              pivot.GetComponent<ExportObjectData>().hingedObject = focusedObject;
                     *              focusedObject.GetComponent<ExportObjectData>().hingedObject = focusedObject;
                     *              pivoted = true;
                     *              worldPos = pivot.transform.position + (Vector3)hitAndObjectCenterDelta;
                     *              break;
                     *      }
                     * }*/
                    worldPos += (Vector3)hitAndObjectCenterDelta;
                    RoundVector(ref worldPos);
                    worldPos.z = 0;
                    focusedObject.transform.position = worldPos;
                    AllowCameraMovement = false;
                }
            }
        }

        if (AllowCameraMovement && !IsPointerOverUIObject())
        {
            controller.UpdateCamera();
        }

        if (focusedObject == null)
        {
            linkHingeButton.interactable = false;
            linkFixedButton.interactable = false;
            deleteButton.interactable    = false;
            rotateCCWButton.interactable = false;
            rotateCWButton.interactable  = false;
        }
        else
        {
            linkHingeButton.interactable = true;
            linkFixedButton.interactable = true;
            deleteButton.interactable    = true;
            rotateCCWButton.interactable = true;
            rotateCWButton.interactable  = true;
        }

        if (focusedObjectData != null && focusedObjectData.isMotorPort)
        {
            for (int i = 0; i < 8; i++)
            {
                if (brainData.connectedMotors[i] == null)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = false;
                }
                else if (i == focusedObjectData.connectedPort)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = true;
                }
                else
                {
                    portCheckboxList[i].interactable = false;
                    portCheckboxList[i].isOn         = false;
                }
            }
        }
        else if (focusedObjectData != null && focusedObjectData.isSensorPort)
        {
            for (int i = 0; i < 8; i++)
            {
                if (brainData.connectedSensors[i] == null)
                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = false;
                }
                else if (i == focusedObjectData.connectedPort)

                {
                    portCheckboxList[i].interactable = true;
                    portCheckboxList[i].isOn         = true;
                }
                else
                {
                    portCheckboxList[i].interactable = false;
                    portCheckboxList[i].isOn         = false;
                }
            }
        }
    }