코딩의 신 29

김동관·2025년 11월 12일

오늘 한 일

  1. Conditions에 Health, Hunger, Stamina 추가 (hunger가 0이 되었을 시에 Health가 감소되는 효과)
  2. Player가 피해를 입을시에 데미지를 받는 효과추가
  1. Canvas에 체력, 배고픔, 스테미나 추가

Condition, UICondition, DamagerIndicator, PlayerCondition,Player를 추가해서 연결을 시켜줍니다.

public class Condition : MonoBehaviour
{
    public float curValue;
    public float maxValue;
    public float startValue;
    public float passiveValue;
    public Image uiBar;

    private void Start()
    {
        curValue = startValue;
    }

    private void Update()
    {
        uiBar.fillAmount = GetPercentage();
    }

    public void Add(float amount)
    {
        curValue = Mathf.Min(curValue + amount, maxValue);
    }
 //Condition에는 체력 최대,최소를 정해주었습니다.
 
 public class UICondition : MonoBehaviour
{
    public Condition health;
    public Condition hunger;
    public Condition stamina;

    private void Start()
    {
        CharacterManager.Instance.Player.condition.uiCondition = this;
    }
}
//UICondition에는 말 그대로 UI를 관리하는 간단한 클래스입니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public PlayerController controller;
    public PlayerCondition condition;

    private void Awake()
    {
        CharacterManager.Instance.Player = this;
        controller = GetComponent<PlayerController>();
        condition = GetComponent<PlayerCondition>();
    }
}
//Player.cs에는 연결만 시켜주며 싱글톤구조, 많이 안 건들이고 불러오게만 하는 형식으로 해줍니다.

public class PlayerCondition : MonoBehaviour
{
    public UICondition uiCondition;

    Condition health { get { return uiCondition.health; } }
    Condition hunger { get { return uiCondition.hunger; } }
    Condition stamina { get { return uiCondition.stamina; } }

    public float noHungerHealthDecay;
    public event Action onTakeDamage;

    private void Update()
    {
        hunger.Subtract(hunger.passiveValue * Time.deltaTime);
        stamina.Add(stamina.passiveValue * Time.deltaTime);

        if (hunger.curValue <= 0f)
        {
            health.Subtract(noHungerHealthDecay * Time.deltaTime);
        }
 //PlayerCondition에는 직접적으로 Condition을 관리해주며 Hunger	시간이 지나면 감소	Subtract()
Stamina	시간이 지나면 회복	Add() 이렇게 관리를 해주었습니다.

이제 Player오브젝트에 Player, PlayerCondition, PlayController를 추가해주면 됩니다. 값들은 원하는대로 추가해주시면 됩니다. 이상입니다!

profile
아이디어 뱅크

0개의 댓글