1인 개발/나홀로 성 지키기

나홀로 성 지키기 개인 프로젝트 12일차

잼잼재미 2024. 4. 2. 19:59

구현 기능


1. 사운드 슬라이더 바 구현


 

  • Loby 씬, Battle 씬에서 볼륨 조절 구현
  • ScriptableObject에 볼륨 값을 저장

 

public void BGMControll()
{
    float sound = BGMSlider.value;
    GameManager.I.DataManager.GameDataSO.BGMVolume = sound;

    if (sound == -40f)	// -40일 때, 음악을 꺼줌
    {
        _audioMixer.SetFloat("BGM", -80f);
    }
    else
    {
        _audioMixer.SetFloat("BGM", sound);
    }
}

 

 

https://kkln2486.tistory.com/155

 

사운드 슬라이더 바 만들기

1. 슬라이더 추가 볼륨은 -80~20까지 존재하지만, -40~0까지만 사용함 (-40 이하는 거의 들리지 않고, 0 이상은 음악이 깨짐) 2. AudioSource 준비 3. AudioMixer 설정 Mixers 이름 설정 Master - BGM, SFX 그룹으로 구

kkln2486.tistory.com

 

 

2. 아이템 구현


 

 

 

IEnumerator COInactive(float time)
{
    yield return new WaitForSeconds(time);
    gameObject.SetActive(false);
}

IEnumerator COPlayerStatReset()
{
    yield return new WaitForSeconds(_itemTime);
    _playerController.Atk = _playerSO.Atk;
    _playerController.Speed = _playerSO.Speed;
    _attackButton.SkillCoolTime = _playerSO.SkillTime;
}

private IEnumerator COStopFade()
{
    yield return new WaitForSeconds(_itemTime);
    StopAllCoroutines();
    _playerSpriteRenderer.color = Color.white;
}

private IEnumerator COFadeInOut(float coolTime, int a, int b, int c, int d)
{
    while (true)
    {
        // 알파 값이 1에서 0으로 text가 보이지 않는다.
        yield return StartCoroutine(Fade(Color.white, new Color(a / 255f, b / 255f, c / 255f, d / 255f)));

        // 알파 값이 0에서 1로 text가 보인다.
        yield return StartCoroutine(Fade(new Color(a / 255f, b / 255f, c / 255f, d / 255f), Color.white));
    }
}

private IEnumerator Fade(Color start, Color end)
{
    float current = 0;
    float percent = 0;

    while (percent < 1)
    {
        current += Time.deltaTime;
        percent = current / _fadeTime;

        Color color = _playerSpriteRenderer.color;
        color = Color.Lerp(start, end, percent);
        _playerSpriteRenderer.color = color;

        yield return null;
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if(Type == PotionType.Hp)
    {
        if (collision.CompareTag("Player"))
        {
            GameManager.I.SoundManager.StartSFX("Potion");
            _castleController.CastleHpRecovery(_hp);
            StartCoroutine(COInactive(0));
        }
    }
    else if(Type == PotionType.Power)
    {
        if (collision.CompareTag("Player"))
        {
            GameManager.I.SoundManager.StartSFX("Potion");
            StartCoroutine(COFadeInOut(_itemTime, 127, 0, 255, 255));
            StartCoroutine(COPlayerStatReset());
            StartCoroutine(COInactive(7));
            StartCoroutine(COStopFade());
            _playerController.Atk *= _powerRatio;
            _spriteRenderer.color = new Color(255 / 255f, 255 / 255f, 255 / 255f, 0 / 255f);
            _collider.enabled = false;
        }
    }
    else if (Type == PotionType.Speed)
    {
        if (collision.CompareTag("Player"))
        {
            GameManager.I.SoundManager.StartSFX("Potion");
            StartCoroutine(COFadeInOut(_itemTime, 66, 179, 0, 255));
            StartCoroutine(COPlayerStatReset());
            StartCoroutine(COInactive(7));
            StartCoroutine(COStopFade());
            _playerController.Speed *= _speedRatio;
            _spriteRenderer.color = new Color(255 / 255f, 255 / 255f, 255 / 255f, 0 / 255f);
            _collider.enabled = false;
        }
    }
    else if (Type == PotionType.CoolTime)
    {
        if (collision.CompareTag("Player"))
        {
            GameManager.I.SoundManager.StartSFX("Potion");
            StartCoroutine(COFadeInOut(_itemTime, 50, 50, 50, 255));
            StartCoroutine(COPlayerStatReset());
            StartCoroutine(COInactive(7));
            StartCoroutine(COStopFade());
            _attackButton.SkillCoolTime *= _coolTimeRatio;
            _spriteRenderer.color = new Color(255 / 255f, 255 / 255f, 255 / 255f, 0 / 255f);
            _collider.enabled = false;
        }
    }

}

 

  • 코루틴 함수를 사용해서 각각 아이템마다 일정 시간 깜빡임 구현
  • GameObject가 비활성화 되면, 코루틴 함수가 실행되지 않기 때문에, Color.a값을 0으로 바꿔서 안보이게 함
  • 충분한 시간 뒤, 비활성화
  • 이때, Player와 또 다시 충돌하지 않게 하기 위해 enabled = false;로 Collider를 비활성화

 

회고


게임의 거의 마무리 단계까지 개발하였다. 직접 플레이를 해보니, 약간 심심한 느낌이 있어서 아이템을 구현했다. 예전 2D 로그라이크 프로젝트에서 구현했던 방법과 비슷하게 아이템을 구현했다. 그리고 사운드 슬라이드바도 이전 프로젝트에서 구현했던 적이 있어서 쉽게 구현할 수 있었다. 내일은 이제 전체적인 게임 밸런스 조절과 최종 버그 확인을 진행할 예정이다.