using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { // Start is called before the first frame update public float moveSpeed; public Rigidbody2D rb2d; public bool movingRight; public bool movingLeft; public bool movingDown; public bool movingUp; public int playerDirection; public GameObject faceR; public GameObject faceL; public GameObject faceD; public GameObject faceU; public GameObject faceHurt; public Animator playerAnimations; public bool attacking; public float attackCoolDownTime; public bool justAttacked; public bool hurting; public float hurtingCoolDownTime; public bool justHurt; public GameObject swordDown; public GameObject swordRight; public GameObject swordLeft; public GameObject swordUp; public GameObject bowUp; public GameObject bowLeft; public GameObject bowRight; public GameObject bowDown; public int weaponInUse; public GameObject arrowPrefab; public int healthCount; public GameObject gameOverScreen; public GameObject heart1; public GameObject heart2; public GameObject heart3; private readonly float knockbackDuration = 0.3f; private readonly Vector2 knockbackForce = new(3f, 2f); private bool isKnockedBack; private Vector2 knockbackDirection; private Vector2 moveInput; private void Start() { healthCount = 3; } // Update is called once per frame private void Update() { checkHealth(); if (!isKnockedBack && !attacking && !hurting && !justHurt) { if (Input.GetKey(KeyCode.Alpha1)) weaponInUse = 0; if (Input.GetKey(KeyCode.Alpha2)) weaponInUse = 1; refreshWeapons(); if (justHurt && hurtingCoolDownTime > 0) hurtingCoolDownTime -= Time.deltaTime; if (attacking == false && hurting == false && justHurt == false) { moveInput.x = Input.GetAxisRaw("Horizontal"); moveInput.y = Input.GetAxisRaw("Vertical"); moveInput.Normalize(); // Prevents diagonal movement if (Mathf.Abs(moveInput.x) > Mathf.Abs(moveInput.y)) { moveInput.y = 0; rb2d.velocity = moveInput * moveSpeed; } else { moveInput.x = 0; rb2d.velocity = moveInput * moveSpeed; } if (moveInput.x > 0) setDirection("right"); else if (moveInput.x < 0) setDirection("left"); else if (moveInput.y < 0) setDirection("down"); else if (moveInput.y > 0) setDirection("up"); // idle else if (moveInput.y == 0 && moveInput.x == 0) playerAnimations.Play("playerIdle"); // Used to determine time since attack to prevent attacking a lot. if (justAttacked && attackCoolDownTime > 0) attackCoolDownTime -= Time.deltaTime; } if (Input.GetKey(KeyCode.Space) && attackCoolDownTime <= 0 && hurtingCoolDownTime <= 0 && hurting == false) { if (movingUp) playerAnimations.Play("attackU"); else if (movingDown) playerAnimations.Play("attackD"); else if (movingRight) playerAnimations.Play("attackR"); else if (movingLeft) playerAnimations.Play("attackL"); if (weaponInUse == 1) { if (movingUp) Instantiate(arrowPrefab, transform.position, Quaternion.Euler(new Vector3(0, 0, 90))); else if (movingDown) Instantiate(arrowPrefab, transform.position, Quaternion.Euler(new Vector3(0, 0, 270))); else if (movingRight) Instantiate(arrowPrefab, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))); else if (movingLeft) Instantiate(arrowPrefab, transform.position, Quaternion.Euler(new Vector3(0, 0, 180))); moveInput.y = 0; moveInput.x = 0; attackCoolDownTime = 0.25f; justAttacked = true; } } rb2d.velocity = moveInput * moveSpeed; } if (isKnockedBack && !hurting && !justHurt) StartCoroutine(KnockbackCoroutine()); /*if (hurting && hurtingCoolDownTime <= 0) { faceHurt.SetActive(false); faceD.SetActive(true); hurting = false; /*if (movingRight) playerAnimations.Play("walkR");#1# playerAnimations.Play("playerIdle"); }*/ } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy") && !isKnockedBack) { healthCount--; // Calculate the knockback direction knockbackDirection = (transform.position - collision.transform.position).normalized; // Set the knockback flag isKnockedBack = true; } } private IEnumerator KnockbackCoroutine() { hurting = true; justHurt = true; faceHurt.SetActive(true); faceD.SetActive(false); faceU.SetActive(false); faceR.SetActive(false); faceL.SetActive(false); playerAnimations.Play("playerHurt"); // Apply the knockback force rb2d.AddForce(knockbackDirection * knockbackForce, ForceMode2D.Impulse); yield return new WaitForSeconds(knockbackDuration); isKnockedBack = false; faceHurt.SetActive(false); faceD.SetActive(true); hurting = false; justHurt = false; playerAnimations.Play("playerIdle"); } public void restartGame() { SceneManager.LoadScene(0); } private void checkHealth() { if (healthCount > 0) { if (healthCount == 3) { heart1.SetActive(true); heart2.SetActive(true); heart3.SetActive(true); } else if (healthCount == 2) { heart1.SetActive(true); heart2.SetActive(true); heart3.SetActive(false); } else if (healthCount == 1) { heart1.SetActive(true); heart2.SetActive(false); heart3.SetActive(false); } } else { gameOverScreen.SetActive(true); heart1.SetActive(false); heart2.SetActive(false); heart3.SetActive(false); } } private void refreshWeapons() { swordDown.SetActive(false); swordLeft.SetActive(false); swordRight.SetActive(false); swordUp.SetActive(false); bowDown.SetActive(false); bowLeft.SetActive(false); bowRight.SetActive(false); bowUp.SetActive(false); if (weaponInUse == 0) { swordDown.SetActive(true); swordLeft.SetActive(true); swordRight.SetActive(true); swordUp.SetActive(true); } else if (weaponInUse == 1) { bowDown.SetActive(true); bowLeft.SetActive(true); bowRight.SetActive(true); bowUp.SetActive(true); } } private void setDirection(string activeDir) { faceR.SetActive(false); faceL.SetActive(false); faceD.SetActive(false); faceU.SetActive(false); movingDown = false; movingLeft = false; movingRight = false; movingUp = false; if (activeDir == "right") { movingRight = true; faceR.SetActive(true); playerAnimations.Play("walkR"); } if (activeDir == "down") { movingDown = true; faceD.SetActive(true); playerAnimations.Play("walkD"); } if (activeDir == "up") { movingUp = true; faceU.SetActive(true); playerAnimations.Play("walkU"); } if (activeDir == "left") { movingLeft = true; faceL.SetActive(true); playerAnimations.Play("walkL"); } } }