Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- PlayerControls playerControls;
- PlayerMain player;
- [SerializeField]
- Rigidbody2D rb;
- [SerializeField]
- float moveSpeed = 5;
- bool isSprinting;
- //Reminder, currently Sprinting without moving drains stamina
- bool isMoving;
- private void Awake()
- {
- playerControls = new PlayerControls();
- player = this.gameObject.GetComponent<PlayerMain>();
- playerControls.Controls.Move.performed += _ => Move(playerControls.Controls.Move.ReadValue<Vector2>());
- playerControls.Controls.Move.canceled += _ => Move(playerControls.Controls.Move.ReadValue<Vector2>());
- playerControls.Controls.Sprint.performed += _ => Sprint();
- playerControls.Controls.Sprint.canceled += _ => StopSprinting();
- }
- void Move(Vector2 direction)
- {
- rb.velocity = direction * moveSpeed;
- }
- void Sprint()
- {
- if (player.CurrentStamina > 0)
- {
- player.StamRegen = false;
- isSprinting = true;
- moveSpeed *= 2;
- StartCoroutine(UseStamina());
- }
- }
- void StopSprinting()
- {
- isSprinting = false;
- moveSpeed = 5;
- CallStam();
- }
- async void CallStam()
- {
- await Task.Delay(2500);
- player.SetStamBool();
- }
- IEnumerator UseStamina()
- {
- while (isSprinting == true && player.CurrentStamina != 0)
- {
- yield return new WaitForSeconds(0.3f);
- player.StamRegen = false;
- player.CurrentStamina -= 3;
- }
- }
- private void Update()
- {
- if (player.CurrentStamina <= 0)
- {
- player.CurrentStamina = 0;
- StopSprinting();
- }
- }
- private void OnEnable()
- {
- playerControls.Controls.Enable();
- }
- private void OnDisable()
- {
- playerControls.Controls.Disable();
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using UnityEngine;
- public class MeleeAttack : MonoBehaviour
- {
- PlayerControls playerControls;
- PlayerMain player;
- [SerializeField]
- Transform attackDetection;
- [SerializeField]
- LayerMask enemyLayers;
- Vector2 movementDirection;
- [SerializeField]
- float attackRange = 0.5f;
- bool StamRegen;
- private void Awake()
- {
- playerControls = new PlayerControls();
- player = this.gameObject.GetComponent<PlayerMain>();
- playerControls.Controls.Melee.performed += _ => Melee();
- playerControls.Controls.Move.performed += _ => movementDirection = playerControls.Controls.Move.ReadValue<Vector2>();
- }
- void Melee()
- {
- Vector2 attackDetectionPosition = attackDetection.position;
- Vector2 attackDirection = attackDetectionPosition + movementDirection;
- Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackDirection, attackRange, enemyLayers);
- foreach (Collider2D enemy in hitEnemies)
- {
- Debug.Log("We hit " + enemy.name);
- }
- if (player.CurrentStamina > 10)
- {
- player.CurrentStamina -= 10;
- //Deal Damage
- player.StamRegen = false;
- CallStam();
- }
- else if (player.CurrentStamina > 0)
- {
- //Deal half damage
- player.StamRegen = false;
- CallStam();
- }
- }
- async void CallStam()
- {
- await Task.Delay(2500);
- player.SetStamBool();
- }
- private void OnEnable()
- {
- playerControls.Controls.Enable();
- }
- private void OnDisable()
- {
- playerControls.Controls.Disable();
- }
- private void OnDrawGizmosSelected()
- {
- Vector2 attackDetectionPosition = attackDetection.position;
- Vector2 attackDirection = attackDetectionPosition + movementDirection;
- Gizmos.DrawWireSphere(attackDirection, attackRange);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMain : MonoBehaviour
- {
- PietyBar pietyBar;
- [SerializeField]
- GameObject pietyBarGameObject;
- public float piety;
- int sanity;
- int temptation;
- int anxiety;
- int healthBase = 100;
- int StaminaBase = 66;
- public int MaxStamina;
- public int MaxHealth;
- public int CurrentHealth;
- public int CurrentStamina;
- int meeleDamageBase = 20;
- int rangedDamageBase = 10;
- public bool StamRegen;
- private void Awake()
- {
- pietyBar = pietyBarGameObject.GetComponent<PietyBar>();
- MaxStamina = StaminaBase;
- MaxHealth = healthBase;
- CurrentHealth = healthBase;
- CurrentStamina = StaminaBase;
- }
- private void Update()
- {
- if (piety != pietyBar.GetPiety())
- {
- piety = pietyBar.GetPiety();
- }
- }
- public void SetStamBool()
- {
- StamRegen = true;
- StartCoroutine(RegenerateStamina());
- }
- IEnumerator RegenerateStamina()
- {
- while (StamRegen == true && CurrentStamina < MaxStamina)
- {
- yield return new WaitForSeconds(1);
- CurrentStamina += 5;
- }
- if (CurrentStamina > MaxStamina)
- {
- int subtractionAmount = CurrentStamina - MaxStamina;
- CurrentStamina -= subtractionAmount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement