📝 24.02.01
오늘부터 Unity 숙련 주차가 시작되었다. 예상대로 강의는 3D 게임을 만들어보는 것이다. 2D 게임을 위주로만 만들어봤다 보니 3D 게임을 제작하는 것은 익숙하지도 않고 매우 어려웠다.
오늘 주로 한 것은 플레이어를 만들고, 해당 플레이어의 상태를 표시하는 UI를 만드는 것이었는데 가장 인상적인 스크립트는 이것이다.
PlayerConditions.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public interface IDamagable
{
void TakePhysicalDamage(int damageAmount);
}
[System.Serializable]
public class Condition
{
[HideInInspector]
public float curValue;
public float maxValue;
public float startValue;
public float regenRate;
public float decayRate;
public Image uiBar;
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;
}
}
public class PlayerConditions : MonoBehaviour, IDamagable
{
public Condition health;
public Condition hunger;
public Condition stamina;
public float noHungerHealthDecay;
public UnityEvent onTakeDamage;
void Start()
{
health.curValue = health.startValue;
hunger.curValue = hunger.startValue;
stamina.curValue = stamina.startValue;
}
void Update()
{
hunger.Subtract(hunger.decayRate * Time.deltaTime);
stamina.Add(stamina.regenRate * Time.deltaTime);
if(hunger.curValue == 0.0f)
health.Subtract(noHungerHealthDecay * Time.deltaTime);
if (health.curValue == 0.0f)
Die();
health.uiBar.fillAmount = health.GetPercentage();
hunger.uiBar.fillAmount = hunger.GetPercentage();
stamina.uiBar.fillAmount = stamina.GetPercentage();
}
public void Heal(float amount)
{
health.Add(amount);
}
public void Eat(float amount)
{
hunger.Add(amount);
}
public bool UseStamina(float amount)
{
if (stamina.curValue - amount < 0) return false;
stamina.Subtract(amount);
return true;
}
public void Die()
{
Debug.Log("플레이어가 죽었다.");
}
public void TakePhysicalDamage(int damageAmount)
{
health.Subtract(damageAmount);
onTakeDamage?.Invoke();
}
}
이 스크립트를 통해 여러가지를 배울 수 있었다.
이 외에도 C#에서 인터페이스는 다중상속이 가능하다는 점도 알차게 사용한 공부가 되는 스크립트였다.
이외에 오늘 세션에서 C# 메모리에 대한 심도있는 강의를 들었는데, 아직 내 말로 정리할 수 있을 정도로 공부되지는 않은 느낌이다. 좀 더 공부해보고 나의 단어로 설명할 수 있도록 노력해야 할 것 같다.
오늘 풀이한 알고리즘 문제