상세 컨텐츠

본문 제목

유니티 총 리로드(재장전)

카테고리 없음

by MJ_119 2024. 10. 16. 16:08

본문

유니티 총 재장전(reload)

 

 

public enum WeaponType
{
    Pistol,
    Revolver,
    AutoRifle,
    Shotgun,
    Rifle
}



[System.Serializable]
public class Weapon
{
    public WeaponType weaponType;

    public int bulletsInMagazine; // 무기 현재 탄알 수
    public int magazineCapacity; // 무기 탄창 (최대 N 발)
    public int totalReserveAmmo; // 몸에 가지고있는 총 탄알 수

    public bool CanShoot()
    {
        return HaveEnoughBullets();
    }

    bool HaveEnoughBullets()
    {
        if( bulletsInMagazine > 0 )
        {
            bulletsInMagazine--;
            return true;
        }

        return false;
    }

    public bool CanReload()
    {
        if (magazineCapacity == bulletsInMagazine)
        {
            return false;
        }

        if ( totalReserveAmmo > 0 )
        {
            return true;
        }

        return false;
    }

    public void RefillBullets()
    {
        totalReserveAmmo += bulletsInMagazine; // 현재 가지고있는 총알을 버리지 않고, 부족한 총알 개수만 꽉 채움.

        int bulletsToReload = magazineCapacity;

        if (bulletsToReload > totalReserveAmmo)
        {
            bulletsToReload = totalReserveAmmo;
        }

        totalReserveAmmo -= bulletsToReload;

        bulletsInMagazine = bulletsToReload;

        if(totalReserveAmmo < 0 ) 
        { 
            totalReserveAmmo = 0;
        }
    }

}