Advertisement
Guest User

Untitled

a guest
Feb 21st, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5.  
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8.  
  9.     PlayerControls playerControls;
  10.     PlayerMain player;
  11.  
  12.     [SerializeField]
  13.     Rigidbody2D rb;
  14.  
  15.     [SerializeField]
  16.     float moveSpeed = 5;
  17.  
  18.     bool isSprinting;
  19.     //Reminder, currently Sprinting without moving drains stamina
  20.     bool isMoving;
  21.  
  22.     private void Awake()
  23.     {
  24.         playerControls = new PlayerControls();
  25.         player = this.gameObject.GetComponent<PlayerMain>();
  26.  
  27.         playerControls.Controls.Move.performed += _ => Move(playerControls.Controls.Move.ReadValue<Vector2>());
  28.         playerControls.Controls.Move.canceled += _ => Move(playerControls.Controls.Move.ReadValue<Vector2>());
  29.         playerControls.Controls.Sprint.performed += _ => Sprint();
  30.         playerControls.Controls.Sprint.canceled += _ => StopSprinting();
  31.     }
  32.  
  33.     void Move(Vector2 direction)
  34.     {
  35.         rb.velocity = direction * moveSpeed;
  36.     }
  37.  
  38.     void Sprint()
  39.     {
  40.         if (player.CurrentStamina > 0)
  41.         {
  42.             player.StamRegen = false;
  43.             isSprinting = true;
  44.             moveSpeed *= 2;
  45.  
  46.             StartCoroutine(UseStamina());
  47.         }
  48.     }
  49.  
  50.     void StopSprinting()
  51.     {
  52.         isSprinting = false;
  53.         moveSpeed = 5;
  54.  
  55.         CallStam();
  56.     }
  57.  
  58.     async void CallStam()
  59.     {
  60.         await Task.Delay(2500);
  61.  
  62.         player.SetStamBool();
  63.     }
  64.  
  65.     IEnumerator UseStamina()
  66.     {
  67.         while (isSprinting == true && player.CurrentStamina != 0)
  68.         {
  69.             yield return new WaitForSeconds(0.3f);
  70.  
  71.             player.StamRegen = false;
  72.             player.CurrentStamina -= 3;
  73.         }
  74.     }
  75.  
  76.     private void Update()
  77.     {
  78.         if (player.CurrentStamina <= 0)
  79.         {
  80.             player.CurrentStamina = 0;
  81.             StopSprinting();
  82.         }
  83.     }
  84.     private void OnEnable()
  85.     {
  86.         playerControls.Controls.Enable();
  87.     }
  88.  
  89.     private void OnDisable()
  90.     {
  91.         playerControls.Controls.Disable();
  92.     }
  93.  
  94. }
  95.  
  96. using System.Collections;
  97. using System.Collections.Generic;
  98. using System.Threading.Tasks;
  99. using UnityEngine;
  100.  
  101. public class MeleeAttack : MonoBehaviour
  102. {
  103.  
  104.     PlayerControls playerControls;
  105.     PlayerMain player;
  106.  
  107.     [SerializeField]
  108.     Transform attackDetection;
  109.  
  110.     [SerializeField]
  111.     LayerMask enemyLayers;
  112.  
  113.     Vector2 movementDirection;
  114.  
  115.     [SerializeField]
  116.     float attackRange = 0.5f;
  117.  
  118.     bool StamRegen;
  119.  
  120.     private void Awake()
  121.     {
  122.         playerControls = new PlayerControls();
  123.         player = this.gameObject.GetComponent<PlayerMain>();
  124.  
  125.         playerControls.Controls.Melee.performed += _ => Melee();
  126.         playerControls.Controls.Move.performed += _ => movementDirection = playerControls.Controls.Move.ReadValue<Vector2>();
  127.     }
  128.  
  129.     void Melee()
  130.     {
  131.         Vector2 attackDetectionPosition = attackDetection.position;
  132.         Vector2 attackDirection = attackDetectionPosition + movementDirection;
  133.  
  134.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackDirection, attackRange, enemyLayers);
  135.  
  136.         foreach (Collider2D enemy in hitEnemies)
  137.         {
  138.             Debug.Log("We hit " + enemy.name);
  139.         }
  140.  
  141.         if (player.CurrentStamina > 10)
  142.         {
  143.             player.CurrentStamina -= 10;
  144.  
  145.             //Deal Damage
  146.  
  147.  
  148.             player.StamRegen = false;
  149.             CallStam();
  150.         }
  151.  
  152.         else if (player.CurrentStamina > 0)
  153.         {
  154.             //Deal half damage
  155.  
  156.             player.StamRegen = false;
  157.             CallStam();
  158.         }
  159.     }
  160.  
  161.     async void CallStam()
  162.     {
  163.         await Task.Delay(2500);
  164.  
  165.         player.SetStamBool();
  166.     }
  167.  
  168.     private void OnEnable()
  169.     {
  170.         playerControls.Controls.Enable();
  171.     }
  172.  
  173.     private void OnDisable()
  174.     {
  175.         playerControls.Controls.Disable();
  176.     }
  177.  
  178.     private void OnDrawGizmosSelected()
  179.     {
  180.  
  181.         Vector2 attackDetectionPosition = attackDetection.position;
  182.         Vector2 attackDirection = attackDetectionPosition + movementDirection;
  183.         Gizmos.DrawWireSphere(attackDirection, attackRange);
  184.     }
  185.  
  186. }
  187.  
  188. using System.Collections;
  189. using System.Collections.Generic;
  190. using UnityEngine;
  191.  
  192. public class PlayerMain : MonoBehaviour
  193. {
  194.  
  195.     PietyBar pietyBar;
  196.  
  197.     [SerializeField]
  198.     GameObject pietyBarGameObject;
  199.  
  200.     public float piety;
  201.     int sanity;
  202.     int temptation;
  203.     int anxiety;
  204.  
  205.     int healthBase = 100;
  206.     int StaminaBase = 66;
  207.     public int MaxStamina;
  208.     public int MaxHealth;
  209.     public int CurrentHealth;
  210.     public int CurrentStamina;
  211.  
  212.     int meeleDamageBase = 20;
  213.     int rangedDamageBase = 10;
  214.  
  215.     public bool StamRegen;
  216.  
  217.     private void Awake()
  218.     {
  219.         pietyBar = pietyBarGameObject.GetComponent<PietyBar>();
  220.  
  221.         MaxStamina = StaminaBase;
  222.         MaxHealth = healthBase;
  223.         CurrentHealth = healthBase;
  224.         CurrentStamina = StaminaBase;
  225.     }
  226.  
  227.     private void Update()
  228.     {
  229.         if (piety != pietyBar.GetPiety())
  230.         {
  231.             piety = pietyBar.GetPiety();
  232.         }
  233.     }
  234.  
  235.     public void SetStamBool()
  236.     {
  237.         StamRegen = true;
  238.  
  239.         StartCoroutine(RegenerateStamina());
  240.     }
  241.  
  242.     IEnumerator RegenerateStamina()
  243.     {
  244.         while (StamRegen == true && CurrentStamina < MaxStamina)
  245.         {
  246.             yield return new WaitForSeconds(1);
  247.  
  248.             CurrentStamina += 5;
  249.         }
  250.  
  251.         if (CurrentStamina > MaxStamina)
  252.         {
  253.             int subtractionAmount = CurrentStamina - MaxStamina;
  254.  
  255.             CurrentStamina -= subtractionAmount;
  256.         }
  257.     }
  258.  
  259. }
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement