45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Mining behavior for pickaxes and similar tools.
|
|
/// </summary>
|
|
[CreateAssetMenu(menuName = "Items/Behaviors/Mining Behavior")]
|
|
public class MiningBehavior : ItemBehavior
|
|
{
|
|
[Header("Mining Settings")]
|
|
[SerializeField] private int damage = 50;
|
|
|
|
private void OnEnable()
|
|
{
|
|
// Set default mining values
|
|
if (string.IsNullOrEmpty(behaviorName)) behaviorName = "Mine";
|
|
if (string.IsNullOrEmpty(animTrigger)) animTrigger = "Attack";
|
|
canRepeat = true;
|
|
}
|
|
|
|
public override bool CanUse(GameObject user, GameObject target)
|
|
{
|
|
// Can always swing, but only deals damage if hitting a mineable block
|
|
return true;
|
|
}
|
|
|
|
public override void Use(GameObject user, GameObject target)
|
|
{
|
|
if (target == null) return;
|
|
|
|
// Use IDamageable interface for all damageable objects
|
|
if (target.TryGetComponent<IDamageable>(out var damageable))
|
|
{
|
|
damageable.TakeDamage(new DamageInfo(damage, DamageType.Mining, user));
|
|
}
|
|
}
|
|
|
|
public override string GetBlockedReason(GameObject user, GameObject target)
|
|
{
|
|
if (target == null) return "No target";
|
|
if (!target.TryGetComponent<IDamageable>(out _))
|
|
return "Cannot mine this object";
|
|
return null;
|
|
}
|
|
}
|