using System.Collections; using UnityEngine; public class Sword : MonoBehaviour, IWeapon { [SerializeField] private GameObject slashAnimPrefab; // Slash animation prefab [SerializeField] private Transform slashAnimSpawnPoint; // Spawn location for the slash animation [SerializeField] private float swordAttackCD = .5f; // Cooldown duration between sword attacks [SerializeField] private WeaponInfo weaponInfo; // Details about this weapon private Transform weaponCollider; // Collider associated with the weapon private Animator myAnimator; // Animator component for handling animations private GameObject slashAnim; // Instance of the slash animation private void Awake() { myAnimator = GetComponent(); // Reference the Animator component attached to this GameObject } private void Start() { weaponCollider = PlayerController.Instance.GetWeaponCollider(); // Obtain the weapon collider from the PlayerController slashAnimSpawnPoint = GameObject.Find("SlashSpawnPoint").transform; // Locate the spawn point for the slash animation } private void Update() { MouseFollowWithOffset(); // Update the weapon's rotation to follow the mouse with an offset } public WeaponInfo GetWeaponInfo() { return weaponInfo; // Provide the weapon's information } public void Attack() { myAnimator.SetTrigger("Attack"); // Trigger the sword's attack animation weaponCollider.gameObject.SetActive(true); // Enable the weapon collider to register hits slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity); // Spawn the slash animation slashAnim.transform.parent = transform.parent; // Set the slash animation's parent to maintain its position relative to the player } // Animation Events // public void DoneAttackingAnimEvent() { weaponCollider.gameObject.SetActive(false); // Disable the weapon collider after the attack animation is complete } public void SwingUpFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180, 0, 0); // Flip the slash animation for an upward swing if (PlayerController.Instance.FacingLeft) { // If the player is facing left, flip the slash animation horizontally slashAnim.GetComponent().flipX = true; } } public void SwingDownFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0); // Rotate the slash animation for a downward swing if (PlayerController.Instance.FacingLeft) { // If the player is facing left, flip the slash animation horizontally slashAnim.GetComponent().flipX = true; } } // Helper Methods // private void MouseFollowWithOffset() { Vector3 mousePos = Input.mousePosition; // Capture the mouse cursor's position Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(PlayerController.Instance.transform.position); // Get the player's position in screen coordinates float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg; // Calculate the angle between the player and the mouse cursor if (mousePos.x < playerScreenPoint.x) { // If the mouse is to the left of the player transform.rotation = Quaternion.Euler(0, -180, angle); // Rotate the weapon to the left weaponCollider.transform.rotation = Quaternion.Euler(0, -180, 0); // Adjust the collider's rotation for a left-facing attack } else { transform.rotation = Quaternion.Euler(0, 0, angle); // Rotate the weapon to the right weaponCollider.transform.rotation = Quaternion.Euler(0, 0, 0); // Adjust the collider's rotation for a right-facing attack } } }