71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Mining behavior for pickaxes and similar tools.
|
|
/// Supports both legacy MineableBlock and new chunk-based MineableChunk.
|
|
/// </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;
|
|
|
|
// Try chunk-based mining first (new system)
|
|
if (target.TryGetComponent<MineableChunk>(out var chunk))
|
|
{
|
|
// Get the specific block index from PlayerNetworkController
|
|
var playerController = user.GetComponent<PlayerNetworkController>();
|
|
if (playerController != null)
|
|
{
|
|
var chunkTarget = playerController.GetCurrentChunkTarget();
|
|
if (chunkTarget.hasHit && chunkTarget.chunk == chunk)
|
|
{
|
|
// Damage the specific block within the chunk
|
|
chunk.DamageBlockServerRpc(chunkTarget.blockIndex, (byte)Mathf.Min(255, damage));
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Fallback to legacy IDamageable interface
|
|
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";
|
|
|
|
// Check for chunk
|
|
if (target.TryGetComponent<MineableChunk>(out _))
|
|
return null; // Chunks are always mineable
|
|
|
|
// Check for legacy damageable
|
|
if (!target.TryGetComponent<IDamageable>(out _))
|
|
return "Cannot mine this object";
|
|
|
|
return null;
|
|
}
|
|
}
|