Files
Gnome-s-Bounty/Assets/Scripts/HammerThrower.cs
T
vova 7579175d24 Refactor and reorganize scripts for improved structure and functionality
- Removed obsolete scripts: Chest, BreakableWall, Door, DoorInteract, and UiManager.
- Moved Chest and BreakableWall scripts to EnvironmentObjects folder.
- Introduced HammerThrower class to manage hammer throwing mechanics.
- Updated PlayerController to utilize new InputManager and HammerThrower.
- Refactored PlayerState to manage player state and item collection.
- Enhanced UI management in UiManager to reflect player item collection.
- Updated EnemyAI to remove unused movement logic.
- Adjusted KeyChest to interact with PlayerState instead of Player.
- Cleaned up code and improved event handling for item collection.
2026-06-05 14:33:20 +03:00

50 lines
1.3 KiB
C#

using System;
using UnityEngine;
public class HammerThrower : MonoBehaviour
{
[SerializeField] private Transform _spawnPoint;
[SerializeField] private GameObject _hammerPrefab;
[SerializeField] private float _throwSpeed = 10f;
private GameObject _currentHammer;
private bool _hasHammer = true;
private bool _facingRight = true;
public bool HasHammer => _hasHammer;
public void SetFacingDirection(bool facingRight)
{
_facingRight = facingRight;
}
public void ThrowHammer()
{
if (!_hasHammer)
return;
_hasHammer = false;
_currentHammer = Instantiate(_hammerPrefab, _spawnPoint.position, _spawnPoint.rotation);
float direction = _facingRight ? 1f : -1f;
var rb = _currentHammer.GetComponent<Rigidbody2D>();
rb.linearVelocity = new Vector2(direction * _throwSpeed, 0);
// Flip hammer visually
var scale = _currentHammer.transform.localScale;
scale.x = Mathf.Abs(scale.x) * direction;
_currentHammer.transform.localScale = scale;
}
private void Update()
{
// Hammer destroyed → hammer returned
if (!_hasHammer && _currentHammer == null)
{
_hasHammer = true;
}
}
}