Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
11 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Models_scr;

public class CharacterController_src : MonoBehaviour
{
    private CharacterController characterController;
    private DefaultInput defaultInput;
    [HideInInspector]
    public Vector2 input_Movment;
    [HideInInspector]
    public Vector2 input_View;
    private Vector3 newCameraRotation;
    private Vector3 newCharacterRotation;
    public Settings settings;

    [Header("References")]
    public Transform cameraHolder;
    public Transform FeetTransform;

    [Header("Settings")]
    public PlayerSettingsModel playerSettings;
    public float viewClampMin = -60;
    public float viewClampMax = 60;
    public LayerMask playerMask;

    [Header("Gravity")]
    public float gravityAmount;
    private float playerGravity;
    public float GravityMin;
    public float GravityMax;

    public Vector3 JumpingForce;
    private Vector3 JumpingForceVelocity;

    [Header("Stance")]
    public PlayerStance playerStance;   
    public float playerStanceSmoothing;
    public CharacterStance playerStandStance;
    public CharacterStance playerCrouchStance;
    public CharacterStance playerProneStance;
    private float stanceCheckErrorMargin = 0.05f;
    private float camHeight;
    private float camHeightVelocity;

    private Vector3 stanceCapsuleCenter;
    private Vector3 stanceCapsuleCenterVelocity;

    private float stanceCapsuleHeight;
    private float stanceCapsuleHeightVelocity;

    public bool IsShiftDown = false;
    public bool IsProneActive = false;

    
    public bool IsSprinting = false;
    private bool WSprintTime = false;
    private bool WSprintPressTime = false;

    [Header("Weapon")]
    public WeaponController currnetWeapon;







    private void Awake()
    {
        defaultInput = new DefaultInput();  
        defaultInput.Character.Movment.performed += e => input_Movment = e.ReadValue<Vector2>();
        defaultInput.Character.View.performed += e => input_View = e.ReadValue<Vector2>();
        defaultInput.Character.Sprint.performed += e => Sprint();
        defaultInput.Character.Jump.performed += e => Jump();
        defaultInput.Character.Crouch.performed += e => Crouch();
        defaultInput.Character.ReleaseCrouch.performed += e => ReleasedCrouch();
        defaultInput.Character.Prone.performed += e => Prone();
        defaultInput.Character.WSprint.performed += e => WSprint(); 
        defaultInput.Character.WSprint1.performed += e => WSprint1();
        defaultInput.Character.WSprint2.performed += e => WSprint2();
        

        defaultInput.Enable();
        newCameraRotation = cameraHolder.localRotation.eulerAngles;
        newCharacterRotation = transform.localRotation.eulerAngles;

        characterController = GetComponent<CharacterController>();
        camHeight = cameraHolder.localPosition.y;

        if (currnetWeapon)
        {
            currnetWeapon.Initialise(this);
        }

    }

    private void Update(){
        if(!settings.IsGamePaused)
        {
            CalculateMovment();
            CalculateView();
            CalculateJump();
            calculateStance();
            fixedStance();
        }
        
        
        

    }
    private void CalculateView(){
        newCharacterRotation.y += playerSettings.ViewXSensitivity * input_View.x * Time.deltaTime;
        transform.localRotation = Quaternion.Euler(newCharacterRotation);
        newCameraRotation.x +=  playerSettings.ViewYSensitivity * -input_View.y * Time.deltaTime;
        newCameraRotation.x = Mathf.Clamp(newCameraRotation.x , viewClampMin , viewClampMax);
        cameraHolder.localRotation = Quaternion.Euler(newCameraRotation);
    }
    private void CalculateMovment(){
        var verticalSpeed = playerSettings.WalkForwardSpeed;
        var horizontalSpeed = playerSettings.WalkStrafeSpeed; 
        if(input_Movment.y <= 0.2f){
            IsSprinting = false;
            
        }
        if(playerStance == PlayerStance.Prone){
            IsSprinting = false;
            verticalSpeed = playerSettings.ProneForwardSpeed;
            horizontalSpeed = playerSettings.ProneStrafeSpeed;
        }
        if(playerStance == PlayerStance.Crouch){
            IsSprinting = false;
            verticalSpeed = playerSettings.CrouchForwardSpeed;
            horizontalSpeed = playerSettings.CrouchStrafeSpeed;
        }
        

        if(IsSprinting){
            verticalSpeed = playerSettings.RunForwardSpeed;
            horizontalSpeed = playerSettings.RunStrafeSpeed;
        }
        if(playerStance == PlayerStance.Crouch){
            verticalSpeed = playerSettings.CrouchForwardSpeed;
            horizontalSpeed = playerSettings.CrouchStrafeSpeed;
        }

        var newMovmentSpeed = new Vector3(horizontalSpeed * input_Movment.x * Time.deltaTime, 0 , verticalSpeed  * input_Movment.y * Time.deltaTime);
        newMovmentSpeed = transform.TransformDirection(newMovmentSpeed);
        if (playerGravity > GravityMin ){
            playerGravity -= gravityAmount * Time.deltaTime;
        }
        if (playerGravity < -0.1f && characterController.isGrounded){
            playerGravity = -0.1f;
        }

        

        
        newMovmentSpeed.y += playerGravity;
        newMovmentSpeed += JumpingForce * Time.deltaTime;
        characterController.Move(newMovmentSpeed);
        if (horizontalSpeed <= 0.1f){
            IsSprinting = false;
        }

    }
    private void CalculateJump(){
        JumpingForce = Vector3.SmoothDamp(JumpingForce, Vector3.zero, ref JumpingForceVelocity, playerSettings.JumpingFallOff);
    }
    private void Jump()
    {
        if (!settings.IsGamePaused)
        {
            if (!characterController.isGrounded)
            {
                return;
            }
            if(playerStance == PlayerStance.Crouch)
            {
                if (StanceCheck(playerStandStance.stanceCollider.height))
                {
                    return;
                }
                IsProneActive = false;
                playerStance = PlayerStance.Stand;
                return;
            }
            if(playerStance == PlayerStance.Prone)
            {
                if (StanceCheck(playerCrouchStance.stanceCollider.height))
                {
                    return;
                }
                IsProneActive = false;
                playerStance = PlayerStance.Crouch;
                return;
            }
            //jump
            playerGravity = 0;
            JumpingForce = Vector3.up * playerSettings.JumpingHeight;
        }
    }
    private void calculateStance(){
        var currentStance = playerStandStance;
        if(playerStance == PlayerStance.Crouch)
        {
            currentStance = playerCrouchStance;
        }
        else if (playerStance == PlayerStance.Prone)
        {
            currentStance = playerProneStance;
        }
        camHeight = Mathf.SmoothDamp(cameraHolder.localPosition.y, currentStance.CameraHeight, ref camHeightVelocity, playerStanceSmoothing);
        cameraHolder.localPosition = new Vector3(cameraHolder.localPosition.x, camHeight, cameraHolder.localPosition.z);    
        characterController.height = Mathf.SmoothDamp(characterController.height, currentStance.stanceCollider.height, ref stanceCapsuleHeightVelocity, playerStanceSmoothing);
        characterController.center = Vector3.SmoothDamp(characterController.center, currentStance.stanceCollider.center, ref stanceCapsuleCenterVelocity, playerStanceSmoothing);

    }
    private void Crouch(){
        if (!settings.IsGamePaused)
        {
            if (StanceCheck(playerCrouchStance.stanceCollider.height))
            {
                return;
            }
            IsShiftDown = true;
            IsProneActive = false;
            playerStance = PlayerStance.Crouch;
        }
        
    }
    private void fixedStance(){
        if (!IsShiftDown && !IsProneActive && !StanceCheck(playerStandStance.stanceCollider.height)){
            playerStance = PlayerStance.Stand;
            
        }
    }

    private void ReleasedCrouch()
    {
        if (!settings.IsGamePaused)
        {
            IsShiftDown = false;
            if (StanceCheck(playerStandStance.stanceCollider.height))
            {
                return;
            }
            IsProneActive = false;
            playerStance = PlayerStance.Stand;
        }
    }
    private void Prone(){
        if (!settings.IsGamePaused)
        {
            if (playerStance == PlayerStance.Prone)
            {
                IsProneActive = false;
                if (StanceCheck(playerProneStance.stanceCollider.height))
                {
                    return;

                }
                playerStance = PlayerStance.Stand;
                IsShiftDown = false;
                return;
            }
            IsShiftDown = false;
            IsProneActive = true;

            playerStance = PlayerStance.Prone;
        }
    }

    public bool StanceCheck(float stanceCheckHeight)
    {
        
        if (!settings.IsGamePaused)
        {
            var start = new Vector3(FeetTransform.position.x, FeetTransform.position.y + characterController.radius + stanceCheckErrorMargin, FeetTransform.position.z);
            var end = new Vector3(FeetTransform.position.x, FeetTransform.position.y + characterController.radius + stanceCheckErrorMargin + stanceCheckHeight, FeetTransform.position.z);



            return Physics.CheckCapsule(start, end, characterController.radius, playerMask);
        }
        else
        {
            return true;
        }

    }
    private void Sprint(){
        if (!settings.IsGamePaused)
        {
            if (input_Movment.y <= 0.2f)
            {
                IsSprinting = false;
                return;
            }
            IsSprinting = true;
        }
    }
    private void WSprint()
    {
        if (!settings.IsGamePaused)
        {
            WSprintPressTime = true;
            Invoke("WSprintPressTimeOff", 0.5f);
        }
    }
    private void WSprint1()
    {
        if (!settings.IsGamePaused)
        {
            if (WSprintPressTime)
            {
                WSprintTime = true;
                Invoke("WSprintTimeOff", 0.5f);
            }
        }
    }
    private void WSprint2(){
        if (!settings.IsGamePaused)
        {
            if (WSprintTime)
            {
                IsSprinting = true;

            }
        }
    }
    private void WSprintTimeOff()
    {
        if (!settings.IsGamePaused)
        {
            WSprintTime = false;
        }
        
    }
    private void WSprintPressTimeOff()
    {
        if (!settings.IsGamePaused)
        {
            WSprintPressTime = false;
        }
            
        
    }
    
}