using System.Collections; using UnityEngine; public class Sword : MonoBehaviour, IWeapon { [SerializeField] private GameObject slashAnimPrefab; // Prefab for the slash animation [SerializeField] private Transform slashAnimSpawnPoint; // Spawn point for the slash animation [SerializeField] private float swordAttackCD = .5f; // Cooldown time between sword attacks [SerializeField] private WeaponInfo weaponInfo; // Information about the weapon private Transform weaponCollider; // Collider for the weapon private Animator myAnimator; // Animator component for controlling animations private GameObject slashAnim; // Instance of the slash animation private void Awake() { myAnimator = GetComponent(); // Get the Animator component attached to this GameObject } private void Start() { weaponCollider = PlayerController.Instance.GetWeaponCollider(); // Get the weapon collider from the PlayerController slashAnimSpawnPoint = GameObject.Find("SlashSpawnPoint").transform; // Find the spawn point for the slash animation } private void Update() { MouseFollowWithOffset(); // Update weapon rotation to follow the mouse with offset } public WeaponInfo GetWeaponInfo() { return weaponInfo; // Return information about the weapon } public void Attack() { myAnimator.SetTrigger("Attack"); // Trigger the attack animation in the Animator weaponCollider.gameObject.SetActive(true); // Activate the weapon collider for attacking slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity); // Instantiate the slash animation slashAnim.transform.parent = transform.parent; // Set the parent of the slash animation } // Animation Events // public void DoneAttackingAnimEvent() { weaponCollider.gameObject.SetActive(false); // Deactivate the weapon collider after attacking animation is done } public void SwingUpFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180, 0, 0); // Rotate the slash animation for upward swing if (PlayerController.Instance.FacingLeft) { // Flip the slash animation if player is facing left slashAnim.GetComponent().flipX = true; } } public void SwingDownFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0); // Rotate the slash animation for downward swing if (PlayerController.Instance.FacingLeft) { // Flip the slash animation if player is facing left slashAnim.GetComponent().flipX = true; } } // Helper Methods // private void MouseFollowWithOffset() { Vector3 mousePos = Input.mousePosition; // Get the position of the mouse cursor Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(PlayerController.Instance.transform.position); // Get player's position on screen float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg; // Calculate the angle between player and mouse cursor if (mousePos.x < playerScreenPoint.x) { // Adjust weapon rotation based on mouse position relative to player transform.rotation = Quaternion.Euler(0, -180, angle); // Rotate weapon when facing left weaponCollider.transform.rotation = Quaternion.Euler(0, -180, 0); // Adjust collider rotation for left-facing attack } else { transform.rotation = Quaternion.Euler(0, 0, angle); // Rotate weapon when facing right weaponCollider.transform.rotation = Quaternion.Euler(0, 0, 0); // Adjust collider rotation for right-facing attack } } }