멀티플레이어 지원
이동, 건설, 인터랙션, 공격 등
This commit is contained in:
54
Assets/Scripts/MineableBlock.cs
Normal file
54
Assets/Scripts/MineableBlock.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class MineableBlock : NetworkBehaviour
|
||||
{
|
||||
[Header("Block Stats")]
|
||||
[SerializeField] private int maxHp = 100;
|
||||
// [동기화] 모든 플레이어가 동일한 블록 체력을 보게 함
|
||||
private NetworkVariable<int> _currentHp = new NetworkVariable<int>();
|
||||
|
||||
[Header("Visuals")]
|
||||
[SerializeField] private GameObject breakEffectPrefab; // 파괴 시 파티클
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsServer)
|
||||
{
|
||||
_currentHp.Value = maxHp;
|
||||
}
|
||||
}
|
||||
|
||||
// 서버에서만 대미지를 처리하도록 제한
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
public void TakeDamageRpc(int damageAmount)
|
||||
{
|
||||
if (_currentHp.Value <= 0) return;
|
||||
|
||||
_currentHp.Value -= damageAmount;
|
||||
Debug.Log($"블록 대미지! 남은 체력: {_currentHp.Value}");
|
||||
|
||||
if (_currentHp.Value <= 0)
|
||||
{
|
||||
DestroyBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void DestroyBlock()
|
||||
{
|
||||
// 1. 모든 클라이언트에게 파괴 이펙트 재생 요청
|
||||
PlayBreakEffectRpc();
|
||||
|
||||
// 2. 서버에서 네트워크 오브젝트 제거 (모든 클라이언트에서 사라짐)
|
||||
GetComponent<NetworkObject>().Despawn();
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Everyone)]
|
||||
private void PlayBreakEffectRpc()
|
||||
{
|
||||
if (breakEffectPrefab != null)
|
||||
{
|
||||
Instantiate(breakEffectPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user