Change hammer mechanic from throw to hit

This commit is contained in:
2026-06-22 16:34:46 +03:00
parent 21682f6af2
commit 5fbe7391d3
15 changed files with 39263 additions and 1328 deletions
+51 -53
View File
@@ -2,79 +2,85 @@ using UnityEngine;
public class Hammer : MonoBehaviour
{
[SerializeField] private float _lifespan = 5f;
[SerializeField] private float _stunDuration = 1f;
[SerializeField] private float _impactNoiseRadius = 10f;
[SerializeField] private bool _emitNoiseOnImpact = true;
public event System.Action OnReturnedToHand;
private float _lifeTimer;
private Vector2 _velocity;
private bool _facingRight;
private Rigidbody2D _rigidbody;
private bool _hasCollided = false;
private bool _isThrown = false;
private Rigidbody2D _rigidbody;
private Collider2D _collider;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
_lifeTimer = _lifespan;
}
public void Initialize(bool facingRight, float speed)
{
_facingRight = facingRight;
_velocity = new Vector2(facingRight ? speed : -speed, 0);
_isThrown = true;
if (_rigidbody != null)
{
_rigidbody.linearVelocity = _velocity;
_rigidbody.angularVelocity = 0f;
}
}
private void Update()
{
// Self-destruct after lifespan expires
_lifeTimer -= Time.deltaTime;
if (_lifeTimer <= 0)
{
Destroy(gameObject);
}
_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)
{
// Check for enemy collision (stun)
var enemy = collision.gameObject.GetComponent<Character>();
var enemy = collider.gameObject.GetComponent<Character>();
if (enemy != null)
{
HandleEnemyCollision(enemy, collision.relativeVelocity);
EmitImpactNoise(collision.GetContact(0).point);
ReturnToHand();
HandleEnemyCollision(enemy, impactVelocity);
EmitImpactNoise(contactPoint);
return;
}
// Check for breakable wall collision
var mapElement = collision.collider.GetComponent<MapElement>();
var mapElement = collider.GetComponent<MapElement>();
if (mapElement != null && mapElement is BreakableWall)
{
mapElement.Hit();
EmitImpactNoise(collision.GetContact(0).point);
ReturnToHand();
EmitImpactNoise(contactPoint);
return;
}
// Fallback: return hammer on any collision
EmitImpactNoise(collision.GetContact(0).point);
ReturnToHand();
// Fallback: hit something else
EmitImpactNoise(contactPoint);
}
private void HandleEnemyCollision(Character enemy, Vector2 impactVelocity)
@@ -83,7 +89,7 @@ public class Hammer : MonoBehaviour
var enemyAI = enemy as EnemyAI;
if (enemyAI != null)
{
enemyAI.OnHitByHammer(_stunDuration);
// enemyAI.OnHitByHammer(_stunDuration);
}
}
@@ -99,13 +105,5 @@ public class Hammer : MonoBehaviour
noiseSystem.Emit(position, _impactNoiseRadius);
}
}
private void ReturnToHand()
{
_isThrown = false;
if (OnReturnedToHand != null)
{
OnReturnedToHand.Invoke();
}
}
}