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();
}
}
}
+1 -11
View File
@@ -3,7 +3,6 @@ using UnityEngine;
public class PlayerController : Character
{
[SerializeField] private HammerThrower _hammerThrower;
private GameObject _hammer;
private bool _isHoldingHammer = true;
@@ -61,19 +60,10 @@ public class PlayerController : Character
private void OnFireButtonPressed()
{
if (_hammerThrower.CanThrow)
{
_animator.SetTrigger("Body_ThrowHammer");
}
_animator.SetTrigger("Body_ThrowHammer");
}
// Animation event
public void ThrowHammerObject()
{
_hammerThrower.TryThrowHammer();
}
protected override void SetWalkingAnimation(bool isWalking)
{
_animator.SetBool("Legs_Walk", isWalking);
-102
View File
@@ -1,102 +0,0 @@
using System;
using UnityEngine;
public class HammerThrower : MonoBehaviour
{
[SerializeField] private Transform _spawnPoint;
[SerializeField] private GameObject hammerInHand;
[SerializeField] private float _throwSpeed = 5f;
[SerializeField] private float _throwCooldown = 1.5f;
[SerializeField] private Collider2D _playerCollider;
private GameObject _currentHammer;
private bool _hasHammer = true;
private bool _facingRight = true;
private float _cooldownTimer = 0f;
public bool HasHammer => _hasHammer;
public bool CanThrow => _hasHammer && _cooldownTimer <= 0f;
public float CooldownRemaining => Mathf.Max(0f, _cooldownTimer);
private void Awake()
{
_currentHammer = hammerInHand;
}
public void SetFacingDirection(bool facingRight)
{
_facingRight = facingRight;
}
public bool TryThrowHammer()
{
if (!CanThrow)
return false;
ThrowHammer();
return true;
}
private void ThrowHammer()
{
if (hammerInHand == null || _spawnPoint == null)
return;
_currentHammer = hammerInHand;
_currentHammer.transform.SetParent(null);
_currentHammer.transform.position = _spawnPoint.position;
_currentHammer.transform.rotation = _spawnPoint.rotation;
_hasHammer = false;
_cooldownTimer = _throwCooldown;
var hammer = _currentHammer.GetComponent<Hammer>();
if (hammer != null)
{
hammer.OnReturnedToHand += ReturnHammerToHand;
hammer.Initialize(_facingRight, _throwSpeed);
}
if (_playerCollider != null)
{
var hammerCollider = _currentHammer.GetComponent<Collider2D>();
if (hammerCollider != null)
{
Physics2D.IgnoreCollision(_playerCollider, hammerCollider);
}
}
}
private void Update()
{
if (_cooldownTimer > 0f)
{
_cooldownTimer -= Time.deltaTime;
}
}
private void ReturnHammerToHand()
{
if (_currentHammer == null || _spawnPoint == null)
return;
_currentHammer.transform.SetParent(_spawnPoint);
_currentHammer.transform.localPosition = Vector3.zero;
_currentHammer.transform.localRotation = Quaternion.identity;
var rb = _currentHammer.GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.linearVelocity = Vector2.zero;
rb.angularVelocity = 0f;
}
var hammer = _currentHammer.GetComponent<Hammer>();
if (hammer != null)
{
hammer.OnReturnedToHand -= ReturnHammerToHand;
}
_hasHammer = true;
_cooldownTimer = 0f;
}
}
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 59cb4a98d6866124793e8758b2ec958a