-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyAttackHitbox.cs
More file actions
73 lines (61 loc) · 2.95 KB
/
Copy pathEnemyAttackHitbox.cs
File metadata and controls
73 lines (61 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Assets/Scripts/EnemyAttackHitbox.cs
using UnityEngine;
using System.Collections; // Added to support IEnumerator
public class EnemyAttackHitbox : MonoBehaviour
{
private SkeletonAI skeletonAI;
private Collider2D hitboxCollider;
private DamageDealer damageDealer;
void Awake()
{
skeletonAI = GetComponentInParent<SkeletonAI>();
if (skeletonAI == null)
Debug.LogError("SkeletonAI component not found in parent.");
hitboxCollider = GetComponent<Collider2D>();
if (hitboxCollider == null)
Debug.LogError("Collider2D component missing on EnemyAttackHitbox.");
// Ensure hitbox is disabled initially
hitboxCollider.enabled = false;
}
void OnEnable()
{
// Enable the collider when the hitbox is activated
hitboxCollider.enabled = true;
Debug.Log("Attack Hitbox Enabled.");
}
void OnDisable()
{
// Disable the collider when the hitbox is deactivated
hitboxCollider.enabled = false;
Debug.Log("Attack Hitbox Disabled.");
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
// Get the DamageReceiver component from the player
DamageReceiver receiver = collision.GetComponent<DamageReceiver>();
if (receiver != null && skeletonAI != null && skeletonAI.damageDealer != null && skeletonAI.damageDealer.damageStats != null)
{
// Determine if the hit is a critical hit
bool isCritical = UnityEngine.Random.value <= skeletonAI.damageDealer.damageStats.criticalHitChance;
// Calculate final damage
int damageAmount = isCritical
? Mathf.RoundToInt(skeletonAI.damageDealer.damageStats.minDamage * skeletonAI.damageDealer.damageStats.criticalHitMultiplier)
: skeletonAI.damageDealer.damageStats.minDamage;
// Calculate knockback direction (from enemy to player)
Vector2 knockbackDirection = (collision.transform.position - skeletonAI.transform.position).normalized;
// Calculate knockback force within the defined range
float knockbackForce = Random.Range(skeletonAI.damageDealer.damageStats.minKnockbackForce, skeletonAI.damageDealer.damageStats.maxKnockbackForce);
// Create DamageInfo struct with all necessary information
DamageInfo damageInfo = new DamageInfo(damageAmount, isCritical, skeletonAI.damageDealer.damageStats.criticalHitMultiplier, knockbackForce, knockbackDirection);
// Apply damage to the player
receiver.TakeDamage(damageInfo);
}
else
{
Debug.LogWarning("Player does not have a DamageReceiver component or DamageDealer is not properly set.");
}
}
}
}