using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class PlayerCombat : MonoBehaviour { public Animator animator; // Animator for attack animation public Transform attackPoint; // Point from which attack range is measured public LayerMask enemyLayers; // Layers that represent enemies public float attackRange = 0.5f; // Attack range public int attackDamage = 20; // Damage dealt per attack public float attackRate = 2f; // Attacks per second private float nextAttackTime = 0f; // Time for the next attack private PlayerStamina playerStamina; // Reference to player stamina public int attackStaminaCost = 10; // Stamina cost per attack AudioManager audioManager; // Reference to the audio manager void Awake() { // Initialize audio manager audioManager = GameObject.FindGameObjectWithTag("Audio").GetComponent(); } void Start() { // Initialize player stamina playerStamina = GetComponent(); } void Update() { // Check if it's time for the next attack if (Time.time >= nextAttackTime) { // Perform attack if mouse button is released and stamina is sufficient if (Input.GetMouseButtonUp(0) && playerStamina.UseStamina(attackStaminaCost)) { Attack(); nextAttackTime = Time.time + 1f / attackRate; // Set time for next attack } } } void Attack() { // Trigger attack animation animator.SetTrigger("isAttacking"); // Play attack sound effect audioManager.PlaySFX(audioManager.swordAttackSFX); // Detect and damage enemies in attack range Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); foreach (Collider2D enemy in hitEnemies) { // Damage boss if hit BossHealth bossHealth = enemy.GetComponent(); if (bossHealth != null) { bossHealth.TakeDamage(attackDamage); } // Damage enemy if hit EnemyHealth enemyHealth = enemy.GetComponent(); if (enemyHealth != null) { enemyHealth.TakeDamage(attackDamage); } } } void OnDrawGizmosSelected() { // Draw attack range gizmo in editor if (attackPoint != null) { Gizmos.DrawWireSphere(attackPoint.position, attackRange); } } }