104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class MineableBlock : NetworkBehaviour
|
|
{
|
|
[Header("Block Stats")]
|
|
[SerializeField] private int maxHp = 100;
|
|
// [동기화] 모든 플레이어가 동일한 블록 체력을 보게 함
|
|
private NetworkVariable<int> _currentHp = new NetworkVariable<int>();
|
|
|
|
|
|
[Header("Visuals")]
|
|
private Outline _outline;
|
|
private Vector3 _originalPos;
|
|
|
|
[Header("Shake Settings")]
|
|
[SerializeField] private float shakeDuration = 0.15f; // 흔들리는 시간
|
|
[SerializeField] private float shakeMagnitude = 0.1f; // 흔들리는 강도
|
|
private Coroutine _shakeCoroutine;
|
|
|
|
void Awake()
|
|
{
|
|
// 해당 오브젝트 혹은 자식에게서 Outline 컴포넌트를 찾습니다.
|
|
_outline = GetComponentInChildren<Outline>();
|
|
_originalPos = transform.localPosition; // 로컬 위치 저장
|
|
|
|
if (_outline != null)
|
|
{
|
|
// 게임 시작 시 하이라이트는 꺼둡니다.
|
|
_outline.enabled = false;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"{gameObject.name}: QuickOutline 에셋의 Outline 컴포넌트를 찾을 수 없습니다.");
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
if (_currentHp.Value <= 0)
|
|
{
|
|
DestroyBlock();
|
|
}
|
|
}
|
|
|
|
private void DestroyBlock()
|
|
{
|
|
// 2. 서버에서 네트워크 오브젝트 제거 (모든 클라이언트에서 사라짐)
|
|
GetComponent<NetworkObject>().Despawn();
|
|
}
|
|
|
|
// 하이라이트 상태를 설정하는 공개 메서드
|
|
public void SetHighlight(bool isOn)
|
|
{
|
|
if (_outline == null) return;
|
|
|
|
// 외곽선 컴포넌트 활성화/비활성화
|
|
_outline.enabled = isOn;
|
|
}
|
|
|
|
// 서버에서 호출하여 모든 클라이언트에게 흔들림 지시
|
|
[ClientRpc]
|
|
public void PlayHitEffectClientRpc()
|
|
{
|
|
if (_shakeCoroutine != null) StopCoroutine(_shakeCoroutine);
|
|
_shakeCoroutine = StartCoroutine(ShakeRoutine());
|
|
}
|
|
|
|
private IEnumerator ShakeRoutine()
|
|
{
|
|
float elapsed = 0.0f;
|
|
Debug.Log("흔들림 코루틴 시작"); // 시작 확인
|
|
|
|
while (elapsed < shakeDuration)
|
|
{
|
|
Vector3 randomOffset = Random.insideUnitSphere * shakeMagnitude;
|
|
transform.localPosition = _originalPos + randomOffset;
|
|
|
|
// 좌표가 실제로 바뀌고 있는지 로그 출력
|
|
// Debug.Log($"현재 좌표: {transform.localPosition}");
|
|
|
|
elapsed += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.localPosition = _originalPos;
|
|
Debug.Log("흔들림 코루틴 종료 및 위치 복구");
|
|
}
|
|
} |