오늘 한 일
Player 상태창 추가 ( Health, Stamina, Hunger, Tuirst)
PlayerAttack 추가
DamageIndicator 추가
3인칭 카메라 추가
Player InputSystem에 Attack 추가
Player 상태창
using System;
using UnityEngine;
public class PlayerCondition : MonoBehaviour, IDamagable
{
public UICondition uiCondition;
Condition health { get { return uiCondition.health; } }
Condition hunger { get { return uiCondition.hunger; } }
Condition thirst { get { return uiCondition.thirst; } }
Condition stamina { get { return uiCondition.stamina; } }
이런 식으로 UICondition에 있는 health/hunger/stamina 등을 가져옴.
public float noHungerHealthDecay;
public event Action onTakeDamage;
private void Update()
{
hunger.Subtract(hunger.passiveValue * Time.deltaTime);
thirst.Subtract(thirst.passiveValue * Time.deltaTime);
stamina.Add(stamina.passiveValue * Time.deltaTime);
//기본적으로 허기·갈증은 줄고, 스태미나는 찬다.
if (hunger.curValue <= 0f || thirst.curValue <= 0f)
{
health.Subtract(noHungerHealthDecay * Time.deltaTime);
}
if (health.curValue <= 0f)
{
Die();
}
}
public void Heal(float amount)
{
health.Add(amount);
}
public void Eat(float amount)
{
hunger.Add(amount);
}
public void Drink(float amount)
{
thirst.Add(amount);
}
public void Die()
{
Debug.Log("플레이어가 죽었다.");
}
public void TakePhysicalDamage(float damage)
{
health.Subtract(damage);
onTakeDamage?.Invoke(); // UI 깜빡임 등 이벤트
}
public bool UseStamina(float amount)
{
if (stamina.curValue - amount < 0f)
{
return false;
}
stamina.Subtract(amount);
return true;
}
}
using UnityEngine;
using UnityEngine.UI;
public class Condition : MonoBehaviour
{
public float curValue;
public float maxValue;
public float startValue;
public float passiveValue;
public Image uiBar;
private void Start()
{
if (uiBar == null)
{
// Condition 오브젝트 밑에 Image 자동 연결
uiBar = transform.Find("Image")?.GetComponent<Image>();
if (uiBar == null)
Debug.LogWarning("uiBar Image를 찾을 수 없습니다!", this);
}
curValue = startValue;
}
private void Update()
{
uiBar.fillAmount = GetPercentage();
}
public void Add(float amount)
{
curValue = Mathf.Min(curValue + amount, maxValue);
}
public void Subtract(float amount)
{
curValue = Mathf.Max(curValue - amount, 0.0f);
}
public float GetPercentage()
{
return curValue / maxValue;
}
}
Player에 Condition.cs추가.
Conditions에 UICondition추가하고 각각에 맞는 오브젝트 넣기.
각각 상태창 오브젝트에는 Condition을 추가해서 값을 지정해줍니다.
내일 목표
온도 기능 추가( 온도에 맞게 너무 극한의 환경일시에 체력이 감소하는 효과)
캐릭터 애니메이션 추가