Exemple #1
0
    public void PrepareShipForLevelLoad()
    {
        Dictionary <GameObject, IList <GameObject> > graph = shipGraph.Graph;

        foreach (GameObject key in graph.Keys)
        {
            foreach (GameObject value in graph[key])
            {
                JointScript.Attach(key, value);
            }

            key.collider.isTrigger = false;
            Destroy(key.GetComponent <ShipEditorScript>());

            key.AddComponent <DamageOnCollisionScript>();
            key.AddComponent <Enforce2DBehaviourScript>();


            DontDestroyOnLoad(key);
            if (key.tag == "Engine")
            {
                key.AddComponent <ThrusterScript>();
                ThrusterScript thrusterScript = key.GetComponent <ThrusterScript>();
                KeyScript      keyScript      = key.GetComponent <KeyScript>();

                thrusterScript.setKeyCode(keyScript.key);
                Destroy(keyScript);
                Destroy(key.transform.Find("EngineKeyText").gameObject);
            }
        }
    }
Exemple #2
0
    public void ConstructMutatedBodyFromDNA()
    {
        for (int i = 0; i < myDNA.nodePositions.Count; i++)
        {
            nodes.Add(gd.lb.GetNode(myDNA.nodePositions[i] + origin));
        }

        for (int i = 0; i < myDNA.designInstructions.Count; i++)
        {
            GameObject newNode = nodes.ElementAt(myDNA.designInstructions[i].targetNode);
            GameObject oldNode = nodes.ElementAt(myDNA.designInstructions[i].baseNode);

            Physics.SyncTransforms();

            myDNA.designInstructions[i] = MutateDNAInstruction(myDNA.designInstructions[i]);

            Vector3    vector            = (newNode.transform.position - oldNode.transform.position);
            GameObject newlyCreatedJoint = gd.lb.GetJoint(oldNode.transform.position + vector.normalized * .9f);
            joints.Add(newlyCreatedJoint);
            newlyCreatedJoint.transform.rotation  = Quaternion.FromToRotation(Vector3.right, vector);
            newlyCreatedJoint.transform.rotation *= Quaternion.Euler(myDNA.designInstructions[i].GetRotation(), 0, 0);

            JointScript js = newlyCreatedJoint.GetComponent <JointScript>();
            js.SetBoneSize(vector.magnitude - 2.5f);
            js.ConnectBaseToNode(oldNode);
            js.ConnectEdgeToNode(newNode);


            js.SetSineFactors(myDNA.designInstructions[i].GetSineFactors());
        }
    }
Exemple #3
0
 void SelectJoint(int jointNum)
 {
     selectJoint                = joints[jointNum];
     selectJointScript          = selectJoint.GetComponent <JointScript>();
     checkMark.transform.parent = selectJoint.transform;
     sliderScript.SetRotSlider(selectJointScript.rotin);
 }
        private JointScript CreateJointObject(Joint joint, GameObject parent)
        {
            Shape shape = PartLoader.GetJoint(joint);

            GameObject gameObject = shape.Instantiate(parent.transform);

            JointScript jointScript = gameObject.AddComponent <JointScript>();

            jointScript.shape = shape;

            jointScript.Id         = joint.Id;
            jointScript.Controller = joint.Controller; // todo: this is not gud

            jointScript.SetColor(joint.Color);

            if (!GameController.Instance.potatoMode)
            {
                shape.ApplyTextures(gameObject);
            }

            jointScript.SetBlueprintPosition(joint.PosA);
            jointScript.SetBlueprintRotation(joint.XaxisA, joint.ZaxisA);

            jointScript.DoRotationPositionOffset(joint.XaxisA, joint.ZaxisA); // required for joints

            return(jointScript);
        }
    // Start is called before the first frame update
    public void Start()
    {
        counter = 0;
        A       = Random.Range(0f, 5f);
        B       = Random.Range(0f, 3.2f);
        C       = Random.Range(-1.8f, 1.8f);

        js = GM.GetComponent <JointScript>();
    }
Exemple #6
0
    public void TakeDamage()
    {
        if (immunityRemaining == 0)
        {
            immunityRemaining = DAMAGE_IMMUNITY_TIME;
            --health;
        }

        if (health == 0)
        {
            var shipScript = Utility.GetShipScript();
            var graph      = shipScript.GetShipGraph();

            // cannot iterate over list when it is being modified; must copy the objects into separate list
            var attachedObjects = new List <GameObject>(graph[gameObject]);
            foreach (GameObject attachedObject in attachedObjects)
            {
                JointScript.Detach(gameObject, attachedObject);
                shipScript.RemoveConnection(gameObject, attachedObject);
            }
        }
    }
Exemple #7
0
    private bool GetValidConnectionToNode(int a, int b)
    {
        GameObject newNode = nodes[a];
        GameObject oldNode = nodes[b];

        Vector3 vector = (newNode.transform.position - oldNode.transform.position);

        if (vector.magnitude < 3.1f)
        {
            return(false);
        }

        Physics.SyncTransforms();
        Ray ray = new Ray(oldNode.transform.position + vector.normalized * 1.5f, vector.normalized);

        RaycastHit[] rch = Physics.SphereCastAll(ray, .5f, vector.magnitude - 3f);

        if (rch.Length > 0)
        {
            return(false);
        }

        GameObject newlyCreatedJoint = gd.lb.GetJoint(oldNode.transform.position + vector.normalized * .9f);

        joints.Add(newlyCreatedJoint);
        newlyCreatedJoint.transform.rotation = Quaternion.FromToRotation(Vector3.right, vector);

        JointScript js = newlyCreatedJoint.GetComponent <JointScript>();

        js.SetBoneSize(vector.magnitude - 2.5f);
        js.ConnectBaseToNode(oldNode);
        js.ConnectEdgeToNode(newNode);
        js.SetSineFactors(GetRandomSineFactors());

        myDNA.AddToInstructions(new Instruction(b, a, js.GetSineFactors(), GetRandomRotFactor()));
        return(true);
    }
Exemple #8
0
    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = _cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray, out hitInfo, 3f, _layer))
            {
                if (hitInfo.collider.GetComponent <JointScript>() != null)
                {
                    _cacheJointScript = hitInfo.collider.GetComponent <JointScript>();
                    hitTarget         = true;
                    holding           = true;
                }
            }

            if (hitTarget)
            {
                _cacheJointScript.Grabbed();
                _camScript.SetAnchorTransformTarget(_cacheJointScript.ReturnAnchorTransform());
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            hitTarget = false;
        }

        if (holding)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Debug.Log("Swiped UP");

                if (_raycastController.IsHit())
                {
                    _cacheJointScript.Release(_raycastController.ReturnHitVectorPoint());
                }
                else
                {
                    _cacheJointScript.Release(_camScript.ReturnVectorPoint());
                }

                holding           = false;
                _cacheJointScript = null;
            }
        }
#endif

        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);

            Ray        ray = _cam.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 3, _layer))
            {
                if (hit.collider.GetComponent <JointScript>() != null)
                {
                    _cacheJointScript = hit.collider.GetComponent <JointScript>();
                    hitTarget         = true;
                    holding           = true;
                }
            }

            if (hitTarget)
            {
                if (SwipeManager.IsSwipingDown() || SwipeManager.IsSwipingDownLeft() || SwipeManager.IsSwipingDownRight())
                {
                    _cacheJointScript.Grabbed();
                    _camScript.SetAnchorTransformTarget(_cacheJointScript.ReturnAnchorTransform());
                }
            }

            if (touch.phase == TouchPhase.Ended)
            {
                hitTarget = false;
            }

            if (holding)
            {
                if (SwipeManager.IsSwipingUp() || SwipeManager.IsSwipingUpLeft() || SwipeManager.IsSwipingUpRight())
                {
                    Debug.Log("Swiped UP");

                    if (_raycastController.IsHit())
                    {
                        _cacheJointScript.Release(_raycastController.ReturnHitVectorPoint(), true);
                    }
                    else
                    {
                        _cacheJointScript.Release(_camScript.ReturnVectorPoint());
                    }

                    holding           = false;
                    _cacheJointScript = null;
                }
            }
        }
    }
Exemple #9
0
 void OnCollisionEnter(Collision coll)
 {
     Debug.Log("Colliding!");
     JointScript.Attach(this.gameObject, coll.gameObject);
     JointScript.Detach(this.gameObject, coll.gameObject);
 }
        /// <summary>
        /// create child - joint references so both know what they are connected to
        /// </summary>
        /// <param name="blueprintScript"></param>
        /// <param name="blueprintData"></param>
        private void ArrangeJointReferencesUsingData(BlueprintScript blueprintScript, BlueprintData blueprintData)
        {
            // child -> joint is a bit of a pain because scriptchild knows nothing of joints[]
            int childIndex = 0;
            IEnumerable <Child> flatDataChildList = blueprintData.Bodies.SelectMany(body => body.Childs).Select(child => child);
            List <ChildScript>  flatLiveChildList = blueprintScript.Bodies.SelectMany(body => body.Childs).Select(child => child).ToList();

            IEnumerable <Joint> flatDataJointList  = blueprintData.Joints;
            List <JointScript>  flatLiveJointsList = blueprintScript.Joints;

            if (blueprintData.Joints == null || blueprintData.Joints.Count == 0)
            {
                Debug.Log($"no joints found, skipping reference step");
                return;
            }

            foreach (Child childData in flatDataChildList)
            {
                if (childData.Joints?.Count > 0)
                {
                    ChildScript childScript = flatLiveChildList[childIndex];
                    childScript.connectedJoints = new List <JointScript>();
                    foreach (JointReference jointRef in childData.Joints)
                    {
                        JointScript jointScript = flatLiveJointsList.FirstOrDefault(joint => joint.Id == jointRef.Id);
                        if (jointScript == default)
                        {
                            Debug.LogWarning($"child.joints: reference to missing joint!");
                            continue;
                        }

                        childScript.connectedJoints.Add(jointScript);

                        // connect that joint to this child if it has the data to connect to this child:
                        Joint jointData = flatDataJointList.FirstOrDefault(joint => joint.Id == jointRef.Id);
                        if (jointData.ChildA == childScript.shapeIdx)
                        {
                            jointScript.childA = childScript;
                        }
                        if (jointData.ChildB == childScript.shapeIdx)
                        {
                            jointScript.childB = childScript;
                        }
                    }
                }
                childIndex++;
            }

            //Debug.Log($"Verifying joint-child connection references");
            // verify connections, perspective: joint
            int jointIndex = 0;

            foreach (Joint jointData in flatDataJointList)
            {
                JointScript jointScript = flatLiveJointsList[jointIndex];

                if (jointData.ChildA == -1 || jointData.ChildA >= flatLiveChildList.Count)
                {
                    if (jointScript.childA == null)
                    {
                        Debug.LogError($"Invalid Joints! no joint.childA information. Attempting fix");

                        // todo: attempt fix: find child(s) that is connected to this jointscript, 1 child: childA, 2 childs: check childB || error
                    }
                    else
                    {
                        Debug.LogWarning($"joint.childA had invalid information! resolved via child.joints!");
                    }
                }
                else
                {
                    ChildScript childScriptA = flatLiveChildList[jointData.ChildA];
                    if (childScriptA.connectedJoints == null)
                    {
                        childScriptA.connectedJoints = new List <JointScript>();
                    }

                    if (!childScriptA.connectedJoints.Contains(jointScript))
                    {
                        Debug.LogWarning($"child.joints had missing information. resolving with joint.childA info!");
                        childScriptA.connectedJoints.Add(jointScript);
                    }
                }

                if (jointData.ChildB == -1 || jointData.ChildB >= flatLiveChildList.Count)
                {
                    if (jointScript.childB == null)
                    {
                        // this is allowed. a joint doesn't have to have a childB
                        // todo: attempt fix: find child(s) that is connected to this jointscript, 1 child: childA, 2 childs: check childB || ok
                    }
                    else
                    {
                        Debug.LogWarning($"joint.childB had invalid information! resolved via child.joints!"); // 'recovered' in prev step
                    }
                }
                else
                {
                    ChildScript childScriptB = flatLiveChildList[jointData.ChildB];
                    if (childScriptB.connectedJoints == null)
                    {
                        childScriptB.connectedJoints = new List <JointScript>();
                    }

                    if (!childScriptB.connectedJoints.Contains(jointScript))
                    {
                        Debug.LogWarning($"child.joints had missing information. resolving with joint.childA info!");
                        childScriptB.connectedJoints.Add(jointScript);
                    }
                }
                jointIndex++;
            }

            // todo: to be complete there should be another verify run on all childs to check if connected joints actually reference them back.
        }