Fire_Wizard

PlayerKnockback

Jun 30th, 2021
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using static MyClasses;
  5.  
  6. public class PlayerController : MonoBehaviour, IVelocityRotated, IKnockbackeable
  7. {
  8.     // ----------------------------------------------------------- TOP -----------------------------------------------------------
  9.  
  10.     private enum States { Idle, Walking, Attacking, InKnockback}
  11.  
  12.     // ----------------------------------------------------------- TO SERIALIZED -----------------------------------------------------------
  13.     //[SerializeField] private float MyMovementSpeed = 5f;
  14.     [SerializeField] private MobSO myMobInfo;
  15.     public GameObject myBullet;
  16.  
  17.     // ----------------------------------------------------------- PRIVATE FIELDS -----------------------------------------------------------
  18.  
  19.     private Vector2 inputVector = Vector2.zero;
  20.     private Vector2 velocity = Vector2.zero;
  21.     private States currentState = States.Idle;
  22.  
  23.     // ----------------------------------------------------------- STATES FIELDS -----------------------------------------------------------
  24.     private float isAttacking = -1f;
  25.     private float canAttack = 0;
  26.  
  27.     private Vector2 knockbackEndpoint;
  28.  
  29.     // ----------------------------------------------------------- PUBLIC FIELDS -----------------------------------------------------------
  30.     public Vector2 Velocity { get { return velocity; } }
  31.  
  32.     // ----------------------------------------------------------- Components -----------------------------------------------------------
  33.     private Rigidbody2D myRigidbody;
  34.     private CapsuleCollider2D myCollider;
  35.     private Transform myMuzzlePos;
  36.  
  37.     // ----------------------------------------------------------- UNITY FUNCTIONS -----------------------------------------------------------
  38.  
  39.     void Start()
  40.     {
  41.         Cursor.visible = false;
  42.         myRigidbody = GetComponent<Rigidbody2D>();
  43.         myCollider = GetComponent<CapsuleCollider2D>();
  44.         myMuzzlePos = transform.Find("Muzzle");
  45.     }
  46.  
  47.     // Update is called once per frame
  48.     void Update()
  49.     {
  50.         currentState = GetNewState();
  51.         DoStateLogic();
  52.         //MyUtils.Print(currentState, myRigidbody.velocity, myRigidbody.velocity == Vector2.zero);
  53.     }
  54.  
  55.     private void FixedUpdate()
  56.     {
  57.         if(currentState != States.Attacking && currentState != States.InKnockback)
  58.         {
  59.             velocity = Vector2.Lerp(velocity, inputVector * myMobInfo.MovementSpeed, myMobInfo.MovementLerpWeight);
  60.             myRigidbody.velocity = velocity;
  61.         }
  62.         else if(currentState != States.InKnockback)
  63.         {
  64.             velocity = Vector2.zero;
  65.             myRigidbody.velocity = velocity;
  66.  
  67.         }
  68.         if (Mathf.Abs(myRigidbody.velocity.x) < 0.1f && Mathf.Abs(myRigidbody.velocity.y) < 0.1f) myRigidbody.velocity = Vector2.zero;
  69.     }
  70.  
  71.     //private void OnCollisionEnter2D(Collision2D collision)
  72.     //{
  73.     //    if(currentState == States.InKnockback)
  74.     //    {
  75.     //        print("LKDSflkas");
  76.     //        knockbackEndpoint = transform.position;
  77.     //        currentState = States.Idle;
  78.     //    }
  79.     //}
  80.  
  81.     // ----------------------------------------------------------- MY FUNCTIONS -----------------------------------------------------------
  82.  
  83.     private Vector2 GetInputVector()
  84.     {
  85.         float x = 0;
  86.         float y = 0;
  87.         if (Input.GetKey(KeyCode.A)) x = -1f;
  88.         else if (Input.GetKey(KeyCode.D)) x = 1;
  89.         if (Input.GetKey(KeyCode.S)) y = -1f;
  90.         if (Input.GetKey(KeyCode.W)) y = 1f;
  91.  
  92.         Vector2 output = new Vector2(x, y);
  93.         if (x != 0 && y != 0) output.Normalize();
  94.         return output;
  95.  
  96.     }
  97.  
  98.     private States GetNewState()
  99.     {
  100.         switch (currentState)
  101.         {
  102.             default:
  103.             case States.Idle:
  104.                 if (velocity != Vector2.zero)
  105.                     return States.Walking;
  106.                 return States.Idle;
  107.             case States.Walking:
  108.                 if (velocity == Vector2.zero) return States.Idle;
  109.                 return States.Walking;
  110.             case States.Attacking:
  111.                 if (isAttacking < 0) return States.Idle;
  112.                 return States.Attacking;
  113.             case States.InKnockback:
  114.                 if(myRigidbody.velocity == Vector2.zero)
  115.                 //if (dist <= 0.1f * 0.1f)
  116.                 {
  117.                     if (isAttacking > 0) return States.Attacking;
  118.                     knockbackEndpoint = Vector2.zero;
  119.                     return States.Idle;
  120.                 }
  121.                 return States.InKnockback;
  122.         }
  123.     }
  124.    
  125.     private void DoStateLogic()
  126.     {
  127.         // Is Attacking Logic
  128.         if(isAttacking >= 0f)
  129.         {
  130.             isAttacking += Time.deltaTime;
  131.             if (isAttacking >= myMobInfo.AttackInteruptionDuration) isAttacking = -1f;
  132.         }
  133.         if(canAttack > 0f)
  134.         {
  135.             canAttack += Time.deltaTime;
  136.             if (canAttack >= myMobInfo.AttackCooldown) canAttack = 0f;
  137.         }
  138.  
  139.         // MOVEMENT
  140.         switch(currentState)
  141.         {
  142.             case States.Idle:
  143.             case States.Walking:
  144.                 inputVector = GetInputVector();
  145.                 if(Input.GetMouseButton(0) && canAttack == 0)
  146.                 {
  147.                     Shoot();
  148.                 }
  149.  
  150.                 break;
  151.            
  152.  
  153.         }
  154.     }
  155.  
  156.     private void Shoot()
  157.     {
  158.         currentState = States.Attacking;
  159.         isAttacking = 0f;
  160.         canAttack += Time.deltaTime;
  161.         GameObject go = Pooler.Instance.Get("PlayerBullet");
  162.         Vector2 dir = ((Vector2)(MyUtils.CameraUtils.MousePosition - transform.position)).normalized;
  163.         go.GetComponent<IProjectile>()?.Setup(myMuzzlePos.position, dir );
  164.         GetKnockback(-dir, 0.5f);
  165.     }
  166.  
  167.     // ----------------------------------------------------------- INTERFACE FUNCTIONS -----------------------------------------------------------
  168.  
  169.     public void GetKnockback(Vector2 dir, float scale,float duration=0.1f)
  170.     {
  171.         duration = Mathf.Clamp(duration, 0, 1);
  172.         scale = Mathf.Clamp(scale, 0, 5);
  173.         currentState = States.InKnockback;
  174.  
  175.         myRigidbody.AddForce(dir.normalized * scale * 10f, ForceMode2D.Impulse);
  176.         MyUtils.Time.SetTimeout(() => {
  177.             myRigidbody.velocity = Vector2.zero;
  178.         }, duration, this);
  179.  
  180.     }
  181.  
  182.  
  183.  
  184.  
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment