39e4e51866
- Implemented PlayerController.cs to manage player movement and actions. - Created PlayerState.cs to track player lives, coins, and key status. - Added CameraFollow.cs for smooth camera movement following the player. - Developed Character.cs as an abstract class for character behavior. - Introduced Enums.cs for defining TreasureType and MapElementType. - Added IDoor interface for door interactions. - Created InputActions.cs for handling player input actions. - Implemented MainMenu.cs for basic menu functionality including play and exit options.
98 lines
2.6 KiB
C#
98 lines
2.6 KiB
C#
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;
|
|
|
|
private float _lifeTimer;
|
|
private Vector2 _velocity;
|
|
private bool _facingRight;
|
|
private Rigidbody2D _rigidbody;
|
|
private bool _hasCollided = false;
|
|
|
|
private void Awake()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody2D>();
|
|
_lifeTimer = _lifespan;
|
|
}
|
|
|
|
public void Initialize(bool facingRight, float speed)
|
|
{
|
|
_facingRight = facingRight;
|
|
_velocity = new Vector2(facingRight ? speed : -speed, 0);
|
|
|
|
if (_rigidbody != null)
|
|
{
|
|
_rigidbody.linearVelocity = _velocity;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Self-destruct after lifespan expires
|
|
_lifeTimer -= Time.deltaTime;
|
|
if (_lifeTimer <= 0)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (_hasCollided)
|
|
return;
|
|
|
|
_hasCollided = true;
|
|
|
|
// Check for enemy collision (stun)
|
|
var enemy = collision.gameObject.GetComponent<Character>();
|
|
if (enemy != null)
|
|
{
|
|
HandleEnemyCollision(enemy, collision.relativeVelocity);
|
|
EmitImpactNoise(collision.GetContact(0).point);
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
// Check for breakable wall collision
|
|
var mapElement = collision.collider.GetComponent<MapElement>();
|
|
if (mapElement != null && mapElement is BreakableWall)
|
|
{
|
|
mapElement.Hit();
|
|
EmitImpactNoise(collision.GetContact(0).point);
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
// Fallback: destroy on any collision
|
|
EmitImpactNoise(collision.GetContact(0).point);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|