Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            if (RecieveInput)
            {
                if (Input.GetButtonDown("Jump"))
                {
                    mBufferedJumpTime = Time.time;
                }

                CameraRotation += new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0f);

                CameraRotation.Set(Mathf.Clamp(CameraRotation.x, -89f, 89f), CameraRotation.y, CameraRotation.z);
                Vector3 upOffset = new Vector3(mLeanPos.x, 3.9f, mLeanPos.z);
                upOffset.Normalize();
                Cam.transform.rotation = Quaternion.FromToRotation(Vector3.up, upOffset) * Quaternion.Euler(CameraRotation);

                mInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
                Quaternion q = Quaternion.Euler(0, CameraRotation.y, 0);
                mInput = q * mInput;

                //update camera offset in acceleration leaning
                {
                    ////TODO:[Gafgar: Sat/01-02-2020] add collision tracing here
                    Cam.transform.position = Ctrl.transform.position + mCameraStartOffset + mLeanPos + q * mHeadBobOffsetSoft;
                }
            }


            {
                GameObject rayHit = null;

                int nbHits = Physics.RaycastNonAlloc(Cam.transform.position, Cam.transform.forward, _hitBuffer, PickupRange, -1, QueryTriggerInteraction.Ignore);
                if (nbHits > 0)
                {
                    for (int index = nbHits; index < _hitBuffer.Length; index++)
                    {
                        _hitBuffer[index].distance = PickupRange * 50;
                    }
                    System.Array.Sort(_hitBuffer, (a, b) => (a.distance > b.distance) ? 1 : ((a.distance < b.distance) ? -1 : 0));
                    var obj = _hitBuffer[0].collider;
                    if (obj.gameObject == gameObject)
                    {
                        if (nbHits > 1)
                        {
                            obj = _hitBuffer[1].collider;
                        }
                        else
                        {
                            obj = null;
                        }
                    }
                    if (obj)
                    {
                        rayHit = obj.gameObject;
                    }
                }
                if (mHeighlightingConnectionRemoveTimer > 0)
                {
                    if (mHeighlightingConnectionRemoveTimer < 0.18f)
                    {
                        mHeighlightingConnectionRemoveTimer += Time.deltaTime;
                        if (mHeighlightingConnectionRemoveTimer >= 0.18f)
                        {
                            mHeighlightingConnectionj = null;
                        }
                    }
                }
                if (rayHit != mLastHeighlightObj)
                {
                    if (mLastHeighlightObj)
                    {
                        ComponentBase comp = mLastHeighlightObj.GetComponent <ComponentBase>();
                        if (comp)
                        {
                            comp.SetHighlighted(false);
                        }
                        AbstractConnection con = mLastHeighlightObj.GetComponent <AbstractConnection>();
                        if (con)
                        {
                            con.SetHighlighted(false);
                        }
                    }
                    mLastHeighlightObj = rayHit;
                    if (mHeighlightingConnectionRemoveTimer < 0)
                    {
                        mHeighlightingConnectionRemoveTimer = 0.01f;
                    }
                    bool hasSetStatusGUI = false;
                    if (mLastHeighlightObj)
                    {
                        Debug.DrawLine(Cam.transform.position + Cam.transform.forward, mLastHeighlightObj.transform.position);
                        if (!mCarryingComponent)
                        {
                            ComponentBase comp = mLastHeighlightObj.GetComponent <ComponentBase>();
                            if (comp)
                            {
                                comp.SetHighlighted(true);

                                if (_statusGUI)
                                {
                                    hasSetStatusGUI = true;
                                    _statusGUI.ShowStatusFor(comp);
                                }
                            }
                        }
                        AbstractConnection con = mLastHeighlightObj.GetComponent <AbstractConnection>();

                        if (con && !con.IsOccupied())
                        {
                            if (mCarryingComponent)
                            {
                                if (con.CanConnect(mCarryingComponent))
                                {
                                    con.SetHighlighted(true);



                                    mHeighlightingConnectionj           = con;
                                    mHeighlightingConnectionRemoveTimer = -1;
                                }
                            }
                            else
                            {
                                con.SetHighlighted(true);
                            }
                        }
                    }

                    if (_statusGUI && !hasSetStatusGUI)
                    {
                        _statusGUI.ShowStatusFor(null);
                    }
                }
            }
            if (Input.GetButtonDown("Fire1"))
            {
                if (!mCarryingComponent)
                {
                    if (mLastHeighlightObj)
                    {
                        ComponentBase comp = mLastHeighlightObj.GetComponent <ComponentBase>();
                        if (comp)
                        {
                            if (comp.IsConnected())
                            {
                                if (comp.CanDisconnect())
                                {
                                    comp.Disconnect();
                                }
                            }

                            if (!comp.IsConnected())
                            {
                                Debug.Log("Picked up: " + comp.name);
                                mCarryingComponent = comp;

                                mCarryPosition = mCarryingComponent.transform.position;
                                mCarryRotation = mCarryingComponent.transform.rotation;
                                mCarryTime     = 0;

                                Collider[] colliders = mCarryingComponent.GetComponentsInChildren <Collider>();
                                foreach (Collider c in colliders)
                                {
                                    c.enabled = false;
                                }
                                mCarryingComponent._Body.isKinematic = true;

                                if (Audio.Music.EventInstance.hasHandle() && !mCarryingComponent._isWorking)
                                {
                                    Audio.Music.SetParameter("Glitch", 1.0f);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Collider[] colliders = mCarryingComponent.GetComponentsInChildren <Collider>();
                    foreach (Collider c in colliders)
                    {
                        c.enabled = true;
                    }

                    if (mLastHeighlightObj)
                    {
                        AbstractConnection con = mLastHeighlightObj.GetComponent <AbstractConnection>();
                        if (con)
                        {
                            if (con.CanConnect(mCarryingComponent))
                            {
                                mCarryingComponent.ConnectTo(con);
                            }
                        }
                    }

                    if (!mCarryingComponent.IsConnected())
                    {
                        Debug.Log("Dropped: " + mCarryingComponent.name);
                        mCarryingComponent._Body.isKinematic = false;
                        mCarryingComponent._Body.AddForce(Cam.transform.forward * 2000);

                        mVelocitySoft += (new Vector3(-10.0f * Cam.transform.forward.x, 0.0f, -10.0f * Cam.transform.forward.z));

                        if (Audio.Music.EventInstance.hasHandle())
                        {
                            Audio.Music.SetParameter("Glitch", 0.0f);
                        }
                    }
                    else
                    {
                        mVelocitySoft += (new Vector3(3.0f * Cam.transform.forward.x, -2.0f, 3.0f * Cam.transform.forward.z));
                        Debug.Log("Connected: " + mCarryingComponent.name + " to " + mCarryingComponent._Connection.name);
                    }
                    mCarryingComponent = null;
                }
            }
        }