Ejemplo n.º 1
0
    private void ProcessInput(RxInputs.MovementInputs input)
    {
        if (!AllowInput)
        {
            return;
        }

        //Movement
        Vector3 inputDir = Vector3.zero;

        if (b_CamRelativeMovement)
        {
            Vector3 camForward = Camera.main.transform.forward;
            camForward.y = 0;
            camForward.Normalize();
            inputDir = (Camera.main.transform.right * input.Direction2D.x) + (camForward * input.Direction2D.y);
        }
        else
        {
            inputDir = new Vector3(input.Direction2D.x, 0.0f, input.Direction2D.y).normalized;
        }

        if (inputDir != Vector3.zero)
        {
            inputDir.Normalize();
        }

        m_Vel -= c_Friction * m_Vel * Time.deltaTime;

        m_Vel += inputDir * c_Accel * Time.deltaTime;

        //m_RB.velocity = m_Vel;

        if (!b_Jumping)
        {
            if (m_Vel.magnitude > 1.0f)
            {
                b_Running        = true;
                m_FootstepTimer += Time.deltaTime;

                this.transform.rotation = Quaternion.LookRotation(Quaternion.Euler(Vector3.up * -90.0f) * m_Vel);
            }
            else
            {
                b_Running         = false;
                m_FootstepTimer   = 0.0f;
                m_MeshFilter.mesh = m_StandMesh;
                this.GetComponent <Renderer>().material = m_VertexMat;
            }

            if (m_CharacterController.isGrounded && input.b_Jump)
            {
                b_Running = false;
                b_Jumping = true;
                this.GetComponent <Renderer>().material = m_JumpMat;
                b_JumpTimerActive = true;
                m_JumpTimer       = 0.0f;
                Vector3 jumpVel = Vector3.up * 5.0f;
                m_PlayerAnimControl.PlayJumpAnim();

                m_CharacterController.Move(Vector3.up * m_JumpForceCurve.Evaluate(m_JumpTimer) * m_JumpForce * Time.deltaTime);
                //Debug.Log("Jump");
                m_MeshFilter.mesh = m_JumpFrame;

                SoundManager.Instance.PlaySoundNotSpatial(SoundManager.Sounds.JUMP_START, 0.8f);
            }
        }
        else
        {
            if (m_Vel != Vector3.zero)
            {
                this.transform.rotation = Quaternion.LookRotation(Quaternion.Euler(Vector3.up * -90.0f) * m_Vel);
            }

            if (m_CharacterController.isGrounded)
            {
                b_JumpTimerActive = false;
                b_Jumping         = false;
                b_Running         = true;
                m_PlayerAnimControl.PlayLandAnim();
                var go = (GameObject)GameObject.Instantiate(m_LandingParticlesPrefab, this.transform.position, Quaternion.identity);

                Destroy(go, 5.0f);
                //Debug.Log("Land");
                SoundManager.Instance.PlaySoundNotSpatial(SoundManager.Sounds.JUMP_LAND, 0.8f);
            }
            else
            {
                if (b_JumpTimerActive)
                {
                    m_JumpTimer += Time.deltaTime;

                    if (m_MeshFilter.mesh != m_FallFrame && m_JumpTimer >= m_JumpDuration * 0.5f)
                    {
                        m_MeshFilter.mesh = m_FallFrame;
                        this.GetComponent <Renderer>().material = m_FallMat;
                    }

                    if (m_JumpTimer >= m_JumpDuration)
                    {
                        b_JumpTimerActive = false;
                    }

                    m_CharacterController.Move(Vector3.up * m_JumpForceCurve.Evaluate(m_JumpTimer) * m_JumpForce * Time.deltaTime);
                }
            }
        }

        m_CharacterController.Move((m_Vel + Physics.gravity) * Time.fixedDeltaTime);

        //animation

        if (b_Running && !b_Jumping)
        {
            //anim
            m_AnimTimer += Time.deltaTime;

            if (m_AnimTimer >= m_FrameTime)
            {
                m_AnimTimer -= m_FrameTime;
                m_CurrentFrame++;
                m_CurrentFrame %= m_RunFrames.Count;

                m_MeshFilter.mesh = m_RunFrames[m_CurrentFrame];
                this.GetComponent <Renderer>().material = m_VertexMat;
            }
            //particle

            //get color at position
            Color?floorColor = null;
            if (m_FloorTexture != null)
            {
                RaycastHit hitInfo;
                if (Physics.Raycast(this.transform.position + Vector3.up, Vector3.down, out hitInfo, 5.0f, (1 << LayerMask.NameToLayer("GroundRaycast"))))
                {
                    floorColor = m_FloorTexture.GetPixelBilinear(hitInfo.textureCoord.x, hitInfo.textureCoord.y);
                }
            }

            m_ParticleTimer += Time.deltaTime;
            if (m_ParticleTimer >= m_ParticleSpawnInterval)
            {
                m_ParticlePool[m_CurrentParticleID].GetComponent <RunParticle>().Activate(m_Vel, floorColor);
                m_CurrentParticleID++;
                m_CurrentParticleID %= c_NumRunParticles;
                m_ParticleTimer     -= m_ParticleSpawnInterval;
            }
        }
    }