more changes to make it better

This commit is contained in:
2026-06-22 15:49:58 +03:00
parent b1223d626a
commit 21682f6af2
14 changed files with 32224 additions and 5183 deletions
+19 -5
View File
@@ -7,11 +7,14 @@ public class Hammer : MonoBehaviour
[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 void Awake()
{
@@ -23,10 +26,12 @@ public class Hammer : MonoBehaviour
{
_facingRight = facingRight;
_velocity = new Vector2(facingRight ? speed : -speed, 0);
_isThrown = true;
if (_rigidbody != null)
{
_rigidbody.linearVelocity = _velocity;
_rigidbody.angularVelocity = 0f;
}
}
@@ -53,7 +58,7 @@ public class Hammer : MonoBehaviour
{
HandleEnemyCollision(enemy, collision.relativeVelocity);
EmitImpactNoise(collision.GetContact(0).point);
Destroy(gameObject);
ReturnToHand();
return;
}
@@ -63,13 +68,13 @@ public class Hammer : MonoBehaviour
{
mapElement.Hit();
EmitImpactNoise(collision.GetContact(0).point);
Destroy(gameObject);
ReturnToHand();
return;
}
// Fallback: destroy on any collision
// Fallback: return hammer on any collision
EmitImpactNoise(collision.GetContact(0).point);
Destroy(gameObject);
ReturnToHand();
}
private void HandleEnemyCollision(Character enemy, Vector2 impactVelocity)
@@ -94,4 +99,13 @@ public class Hammer : MonoBehaviour
noiseSystem.Emit(position, _impactNoiseRadius);
}
}
private void ReturnToHand()
{
_isThrown = false;
if (OnReturnedToHand != null)
{
OnReturnedToHand.Invoke();
}
}
}
+3 -12
View File
@@ -3,7 +3,7 @@ using UnityEngine;
public class PlayerController : Character
{
[SerializeField] GameObject _modelPrefab;
[SerializeField] private HammerThrower _hammerThrower;
private GameObject _hammer;
private bool _isHoldingHammer = true;
@@ -12,14 +12,13 @@ public class PlayerController : Character
private InputManager _inputManager;
private PlayerState _playerState;
private HammerThrower _hammerThrower;
private Vector2 _currentMovement;
private void Awake()
{
_inputManager = GetComponent<InputManager>();
_playerState = GetComponent<PlayerState>();
_hammerThrower = GetComponent<HammerThrower>();
_inputManager.OnFire += OnFireButtonPressed;
_inputManager.OnMovementChanged += OnMovementChanged;
@@ -53,7 +52,6 @@ public class PlayerController : Character
}
MoveTo(_currentMovement.x, isAllowVertical ? _currentMovement.y : 0);
_hammerThrower.SetFacingDirection(_facingRight);
}
private void OnMovementChanged(Vector2 movement)
@@ -73,12 +71,9 @@ public class PlayerController : Character
public void ThrowHammerObject()
{
_hammerThrower.TryThrowHammer();
UpdatePlayerSprite();
}
private void UpdatePlayerSprite()
{
}
protected override void SetWalkingAnimation(bool isWalking)
{
_animator.SetBool("Legs_Walk", isWalking);
@@ -87,10 +82,6 @@ public class PlayerController : Character
protected override void SetClimbingAnimation(bool isClimbing)
{
if (isClimbing)
{
}
_animator.SetBool("Climb", isClimbing);
}
+40 -22
View File
@@ -4,15 +4,15 @@ using UnityEngine;
public class HammerThrower : MonoBehaviour
{
[SerializeField] private Transform _spawnPoint;
[SerializeField] private GameObject _hammerPrefab;
[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;
private Collider2D _playerCollider;
public bool HasHammer => _hasHammer;
public bool CanThrow => _hasHammer && _cooldownTimer <= 0f;
@@ -20,7 +20,7 @@ public class HammerThrower : MonoBehaviour
private void Awake()
{
_playerCollider = GetComponent<Collider2D>() ?? GetComponentInChildren<Collider2D>();
_currentHammer = hammerInHand;
}
public void SetFacingDirection(bool facingRight)
@@ -39,10 +39,22 @@ public class HammerThrower : MonoBehaviour
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;
_currentHammer = Instantiate(_hammerPrefab, _spawnPoint.position, _spawnPoint.rotation);
var hammer = _currentHammer.GetComponent<Hammer>();
if (hammer != null)
{
hammer.OnReturnedToHand += ReturnHammerToHand;
hammer.Initialize(_facingRight, _throwSpeed);
}
if (_playerCollider != null)
{
@@ -52,33 +64,39 @@ public class HammerThrower : MonoBehaviour
Physics2D.IgnoreCollision(_playerCollider, hammerCollider);
}
}
// Initialize hammer with direction and speed
var hammerComponent = _currentHammer.GetComponent<Hammer>();
if (hammerComponent != null)
{
hammerComponent.Initialize(_facingRight, _throwSpeed);
}
// Flip hammer visual based on direction
_currentHammer.transform.localScale = new Vector2(
_currentHammer.transform.localScale.x * (_facingRight ? 1 : -1),
_currentHammer.transform.localScale.y
);
}
private void Update()
{
// Update cooldown timer
if (_cooldownTimer > 0f)
{
_cooldownTimer -= Time.deltaTime;
}
}
// Hammer destroyed → hammer returned
if (!_hasHammer && _currentHammer == null)
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)
{
_hasHammer = true;
rb.linearVelocity = Vector2.zero;
rb.angularVelocity = 0f;
}
var hammer = _currentHammer.GetComponent<Hammer>();
if (hammer != null)
{
hammer.OnReturnedToHand -= ReturnHammerToHand;
}
_hasHammer = true;
_cooldownTimer = 0f;
}
}