139 lines
3.9 KiB
C#
139 lines
3.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class Hammer : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _impactNoiseRadius = 10f;
|
|
[SerializeField] private bool _emitNoiseOnImpact = true;
|
|
|
|
private bool _hasCollided = false;
|
|
private Rigidbody2D _rigidbody;
|
|
private Collider2D _collider;
|
|
private bool _canHit = false;
|
|
private Coroutine _disableHitCoroutine;
|
|
|
|
private void Awake()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody2D>();
|
|
_collider = GetComponent<Collider2D>();
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
Debug.Log($"Hammer collided with {collision.gameObject.name} via OnCollisionEnter2D");
|
|
|
|
if (_hasCollided)
|
|
return;
|
|
|
|
_hasCollided = true;
|
|
ProcessHit(collision.collider, collision.relativeVelocity, collision.GetContact(0).point);
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D collider)
|
|
{
|
|
Debug.Log($"Hammer collided with {collider.gameObject.name} via OnTriggerEnter2D");
|
|
|
|
if (_hasCollided)
|
|
return;
|
|
|
|
_hasCollided = true;
|
|
ProcessHit(collider, Vector2.zero, collider.transform.position);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (_hasCollided || _rigidbody == null || _collider == null)
|
|
return;
|
|
|
|
var hits = Physics2D.OverlapBoxAll(_collider.bounds.center, _collider.bounds.size, 0f);
|
|
foreach (var hit in hits)
|
|
{
|
|
if (hit == _collider)
|
|
continue;
|
|
|
|
var wall = hit.GetComponent<BreakableWall>();
|
|
if (wall != null)
|
|
{
|
|
Debug.Log($"Hammer overlapped with {hit.gameObject.name} in FixedUpdate");
|
|
_hasCollided = true;
|
|
ProcessHit(hit, Vector2.zero, hit.bounds.center);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ProcessHit(Collider2D collider, Vector2 impactVelocity, Vector2 contactPoint)
|
|
{
|
|
// Only process hits when the hammer is allowed to hit (activated by input)
|
|
if (!_canHit)
|
|
return;
|
|
|
|
StartCoroutine(ResetCollisionCoroutine());
|
|
// Check for enemy collision (stun)
|
|
var enemy = collider.gameObject.GetComponent<Character>();
|
|
if (enemy != null)
|
|
{
|
|
HandleEnemyCollision(enemy, impactVelocity);
|
|
EmitImpactNoise(contactPoint);
|
|
return;
|
|
}
|
|
|
|
// Check for breakable wall collision
|
|
var mapElement = collider.GetComponent<MapElement>();
|
|
if (mapElement != null && mapElement is BreakableWall)
|
|
{
|
|
mapElement.Hit();
|
|
EmitImpactNoise(contactPoint);
|
|
return;
|
|
}
|
|
|
|
// Fallback: hit something else
|
|
EmitImpactNoise(contactPoint);
|
|
}
|
|
|
|
public void ActivateHit(float duration)
|
|
{
|
|
_canHit = true;
|
|
if (_disableHitCoroutine != null)
|
|
StopCoroutine(_disableHitCoroutine);
|
|
_disableHitCoroutine = StartCoroutine(DisableHitAfter(duration));
|
|
}
|
|
|
|
private IEnumerator DisableHitAfter(float duration)
|
|
{
|
|
yield return new WaitForSeconds(duration);
|
|
_canHit = false;
|
|
_disableHitCoroutine = null;
|
|
}
|
|
|
|
private void HandleEnemyCollision(Character enemy, Vector2 impactVelocity)
|
|
{
|
|
// Apply stun to enemy
|
|
var enemyAI = enemy as EnemyAI;
|
|
if (enemyAI != null)
|
|
{
|
|
// enemyAI.OnHitByHammer(_stunDuration);
|
|
}
|
|
}
|
|
|
|
private void EmitImpactNoise(Vector2 position)
|
|
{
|
|
if (!_emitNoiseOnImpact)
|
|
return;
|
|
|
|
// Check if NoiseSystem exists and emit noise
|
|
var noiseSystem = NoiseSystem.Instance;
|
|
if (noiseSystem != null)
|
|
{
|
|
noiseSystem.Emit(position, _impactNoiseRadius);
|
|
}
|
|
}
|
|
|
|
private IEnumerator ResetCollisionCoroutine()
|
|
{
|
|
yield return new WaitForSeconds(0.25f);
|
|
_hasCollided = false;
|
|
}
|
|
|
|
}
|