Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SA
- {
- public class StateManager : MonoBehaviour
- {
- [Header("Init")]
- public GameObject activeModel;
- [Header("Inputs")]
- public float vertical;
- public float horizontal;
- public float moveAmount;
- public Vector3 moveDir;
- public bool rt, rb, lt, lb;
- public bool rollInput;
- public bool itemInput;
- [Header("Stats")]
- public float moveSpeed = 2;
- public float runSpeed = 3.5f;
- public float rotateSpeed = 5;
- public float toGround = 0.5f;
- public float rollSpeed = 1;
- [Header("States")]
- public bool onGround;
- public bool run;
- public bool lockOn;
- public bool inAction;
- public bool canMove;
- public bool isTwoHanded;
- public bool usingItem;
- [Header("Other")]
- public EnemyTarget lockOnTarget;
- public Transform lockOnTransform;
- public AnimationCurve roll_curve;
- [HideInInspector]
- public Animator anim;
- [HideInInspector]
- public Rigidbody rigid;
- [HideInInspector]
- public AnimatorHook a_hook;
- public ActionManager actionManager;
- [HideInInspector]
- public InventoryManager inventoryManager;
- [HideInInspector]
- public float delta;
- public LayerMask ignoreLayers;
- float _actionDelay;
- public void Init()
- {
- SetupAnimator();
- rigid = GetComponent<Rigidbody>();
- rigid.angularDrag = 999;
- rigid.drag = 4;
- rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
- inventoryManager = GetComponent<InventoryManager>();
- inventoryManager.Init();
- actionManager = GetComponent<ActionManager>();
- actionManager.Init(this);
- a_hook = activeModel.AddComponent<AnimatorHook>();
- a_hook.Init(this);
- gameObject.layer = 8;
- ignoreLayers = ~(1<< 9);
- anim.SetBool("onGround", true);
- }
- public void SetupAnimator()
- {
- if(activeModel == null)
- {
- anim = GetComponentInChildren<Animator>();
- if(anim == null)
- {
- Debug.Log("No model found");
- }
- else
- {
- activeModel = anim.gameObject;
- }
- }
- if(anim == null)
- anim = activeModel.GetComponent<Animator>();
- anim.applyRootMotion = false;
- }
- public void FixedTick(float d)
- {
- delta = d;
- usingItem = anim.GetBool("interacting");
- DetectItemAction();
- DetectAction();
- inventoryManager.curWeapon.weaponModel.SetActive(!usingItem);
- if (inAction)
- {
- anim.applyRootMotion = true;
- _actionDelay += delta;
- if(_actionDelay > 0.3f)
- {
- inAction = false;
- _actionDelay = 0;
- }
- else
- {
- return;
- }
- }
- canMove = !anim.GetBool("canMove");
- if(!canMove)
- return;
- //a_hook.rm_multi = 1;
- a_hook.CloseRoll();
- HandleRolls();
- anim.applyRootMotion = false;
- rigid.drag = (moveAmount > 0 || onGround == false) ? 0 : 4;
- float targetSpeed = moveSpeed;
- if(run)
- targetSpeed = runSpeed;
- if(onGround)
- rigid.velocity = moveDir * (targetSpeed * moveAmount);
- if(run)
- lockOn = false;
- Vector3 targetDir = (lockOn == false)?
- moveDir
- :
- (lockOnTarget != null)?
- lockOnTarget.transform.position - transform.position
- :
- moveDir;
- targetDir.y = 0;
- if(targetDir == Vector3.zero)
- targetDir = transform.forward;
- Quaternion tr = Quaternion.LookRotation(targetDir);
- Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, delta * moveAmount * rotateSpeed);
- transform.rotation = targetRotation;
- anim.SetBool("lockon", lockOn);
- if (lockOn == false)
- HandleMovementAnimations();
- else
- HandleLockOnAnimations(moveDir);
- }
- public void DetectItemAction()
- {
- if (canMove == false || usingItem)
- return;
- if (itemInput == false)
- return;
- ItemAction slot = actionManager.consumableItem;
- string targetAnim = slot.targetAnim;
- if(string.IsNullOrEmpty(targetAnim))
- return;
- //inventoryManager.curWeapon.weaponModel.SetActive(false);
- usingItem = true;
- anim.Play(targetAnim);
- }
- public void DetectAction()
- {
- if (canMove == false || usingItem)
- return;
- if (rb == false && rt == false && lt == false && lb == false)
- return;
- string targetAnim = null;
- Action slot = actionManager.GetActionSlot(this);
- if (slot == null)
- return;
- targetAnim = slot.targetAnim;
- if(string.IsNullOrEmpty(targetAnim))
- return;
- canMove = false;
- inAction = true;
- anim.CrossFade(targetAnim, 0.2f);
- }
- public void Tick(float d)
- {
- delta = d;
- onGround = OnGround();
- anim.SetBool("onGround", onGround);
- }
- void HandleRolls()
- {
- if (!rollInput || usingItem)
- return;
- float v = vertical;
- float h = horizontal;
- v = (moveAmount > 0.3f)? 1 : 0;
- h = 0;
- if(v != 0)
- {
- if (moveDir == Vector3.zero)
- moveDir = transform.forward;
- Quaternion targetRot = Quaternion.LookRotation(moveDir);
- transform.rotation = targetRot;
- a_hook.InitForRoll();
- a_hook.rm_multi = rollSpeed;
- }
- else
- {
- a_hook.rm_multi = 1.3f;
- }
- anim.SetFloat("vertical", v);
- anim.SetFloat("horizontal", h);
- canMove = false;
- inAction = true;
- anim.CrossFade("Rolls", 0.2f);
- }
- void HandleMovementAnimations()
- {
- anim.SetBool("run", run);
- anim.SetFloat("vertical", moveAmount, 0.4f, delta);
- }
- void HandleLockOnAnimations(Vector3 moveDir)
- {
- Vector3 relativeDir = transform.InverseTransformDirection(moveDir);
- float h = relativeDir.x;
- float v = relativeDir.z;
- anim.SetFloat("vertical", v, 0.2f, delta);
- anim.SetFloat("horizontal", h, 0.2f, delta);
- }
- public bool OnGround()
- {
- bool r = false;
- Vector3 origin = transform.position + (Vector3.up * toGround);
- Vector3 dir = -Vector3.up;
- float dis = toGround + 0.3f;
- Debug.DrawRay(origin, dir * dis);
- RaycastHit hit;
- if(Physics.Raycast(origin, dir, out hit, dis))
- {
- r = true;
- Vector3 targetPosition = hit.point;
- transform.position = targetPosition;
- }
- return r;
- }
- public void HandleTwoHanded()
- {
- anim.SetBool("two_handed", isTwoHanded);
- if (isTwoHanded)
- actionManager.UpdateActionsTwoHanded();
- else
- actionManager.UpdateActionsOneHanded();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement