diff --git a/Assets/Editor/ReadOnlyDrawer.cs b/Assets/Editor/ReadOnlyDrawer.cs deleted file mode 100644 index f28e0596..00000000 --- a/Assets/Editor/ReadOnlyDrawer.cs +++ /dev/null @@ -1,24 +0,0 @@ -#if UNITY_EDITOR -using UnityEngine; -using UnityEditor; - -// PlayerStatsTest.ReadOnlyAttribute를 위한 에디터 속성 드로어 -[CustomPropertyDrawer(typeof(PlayerStatsTest.ReadOnlyAttribute))] -public class ReadOnlyDrawer : PropertyDrawer -{ - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) - { - // 이전 GUI 활성화 상태 저장 - bool wasEnabled = GUI.enabled; - - // 필드 비활성화 (읽기 전용) - GUI.enabled = false; - - // 속성 그리기 - EditorGUI.PropertyField(position, property, label, true); - - // GUI 활성화 상태 복원 - GUI.enabled = wasEnabled; - } -} -#endif \ No newline at end of file diff --git a/Assets/KSH/TestCode.meta b/Assets/KJM.meta similarity index 77% rename from Assets/KSH/TestCode.meta rename to Assets/KJM.meta index bc9a631b..b8671c84 100644 --- a/Assets/KSH/TestCode.meta +++ b/Assets/KJM.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 533241c82a79dcc46932391bf865ce21 +guid: dbfd37452c39a544895c4f842901be82 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/KJM/KJM.unity b/Assets/KJM/KJM.unity new file mode 100644 index 00000000..0d91b2f4 --- /dev/null +++ b/Assets/KJM/KJM.unity @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f13f719e71b8e944fc097d5a437f5e135e094a1cf536cc80881327f9a5dbe59 +size 48687 diff --git a/Assets/KSH/TestCode/Test.prefab.meta b/Assets/KJM/KJM.unity.meta similarity index 63% rename from Assets/KSH/TestCode/Test.prefab.meta rename to Assets/KJM/KJM.unity.meta index 0bbcf71a..a4683bb9 100644 --- a/Assets/KSH/TestCode/Test.prefab.meta +++ b/Assets/KJM/KJM.unity.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 90448f678f8c503408de14c38cd7c653 -PrefabImporter: +guid: fce4c0b08a429b64695c3c0aed06df0f +DefaultImporter: externalObjects: {} userData: assetBundleName: diff --git a/Assets/KJM/KJM_Test.meta b/Assets/KJM/KJM_Test.meta new file mode 100644 index 00000000..f8f4c871 --- /dev/null +++ b/Assets/KJM/KJM_Test.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cf313a0298660aa4db16ced680557810 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KJM/KJM_Test/Save.cs b/Assets/KJM/KJM_Test/Save.cs new file mode 100644 index 00000000..5687fb38 --- /dev/null +++ b/Assets/KJM/KJM_Test/Save.cs @@ -0,0 +1,43 @@ +using System; +using UnityEngine; + +// 던전 관련 저장 데이터 +[Serializable] +public class DungeonSave +{ + // 강화 수치 + public int attackLevel; + public int attackSpeedLevel; + public int heartLevel; + public int moveSpeedLevel; + public int evasionTimeLevel; + + // 현재 진행 중인 스테이지 + public int stageLevel; +} + +// 일상(자취방) 관련 저장 데이터 +[Serializable] +public class HomeSave +{ + // 일상 시간 + public float time; + public int day; + + // 체력 및 평판 수치 + public float health; + public float reputation; + + //이벤트 + public bool isEvent; + public int mealCount; + public int houseworkCount; +} + +// 게임 전체 저장 구조 +[Serializable] +public class Save +{ + public HomeSave homeSave; + public DungeonSave dungeonSave; +} \ No newline at end of file diff --git a/Assets/KSH/TestCode/PlayerStatsTest.cs.meta b/Assets/KJM/KJM_Test/Save.cs.meta similarity index 83% rename from Assets/KSH/TestCode/PlayerStatsTest.cs.meta rename to Assets/KJM/KJM_Test/Save.cs.meta index 6b74a38e..dc3b536b 100644 --- a/Assets/KSH/TestCode/PlayerStatsTest.cs.meta +++ b/Assets/KJM/KJM_Test/Save.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: ae7f2b39529d58a4fa75cf1d30dae9be +guid: 883ac325bc606014fa78662a7b7ec954 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/KJM/KJM_Test/SaveManager.cs b/Assets/KJM/KJM_Test/SaveManager.cs new file mode 100644 index 00000000..0b9c8df1 --- /dev/null +++ b/Assets/KJM/KJM_Test/SaveManager.cs @@ -0,0 +1,188 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using CI.QuickSave; +using Unity.VisualScripting; +using UnityEngine; +using UnityEngine.SceneManagement; + +public class SaveManager : Singleton +{ + private const string SaveFolder = "QuickSave"; + private string MainSaveFilePath => GetSavePath("Save_Main"); + private string BackupSaveFilePath => GetSavePath("Save_Backup"); + + + private Save mainSave; + private Save backupSave; + + + void Start() + { + Load(); //저장된 메인,백업 세이브를 로드 + } + + public void Save() + { + if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(StatManager.instance.ToSaveData())) //같은 상태는 저장되지 않음. 백업 덮어쓰기 방지. + return; + + EnsureSaveExists(); + + backupSave = LoadMain(); //메인 세이브를 백업 세이브에 로드 + SaveBackup(); //백업 세이브 저장 + UpdateSaveInfo(); //세이브를 현재 정보로 업데이트 + SaveMain(); //메인 세이브 저장 + + Debug.Log("세이브 되었습니다."); + } + + public void Load() + { + EnsureSaveExists(); + + mainSave = LoadMain(); + backupSave = LoadBackup(); + + StatManager.instance.loadSaveData2StataManager(mainSave); + + Debug.Log("메인 로드" + mainSave.homeSave.reputation); //임시 코드 + Debug.Log("백업 로드" + backupSave.homeSave.reputation); //임시 코드 + } + + private void UpdateSaveInfo() + { + mainSave = StatManager.instance.ToSaveData(); //스탯을 관리하는 클래스에 선언된 스탯 업데이트 함수를 호출 + } + + private void SaveMain() + { + QuickSaveWriter.Create("Save_Main") + .Write("Main", mainSave) + .Commit(); + } + + private void SaveBackup() + { + QuickSaveWriter.Create("Save_Backup") + .Write("Backup", backupSave) + .Commit(); + } + + private Save LoadMain() + { + try + { + return QuickSaveReader.Create("Save_Main").Read("Main"); + } + catch (System.Exception e) + { + Debug.LogWarning("Main 세이브 로드 실패: " + e.Message); + + // 백업 시도 + if (QuickSaveRaw.Exists(BackupSaveFilePath)) + { + Debug.LogWarning("백업 세이브로 복구 시도"); + return LoadBackup(); + } + + // 백업도 없을 경우 새 세이브 생성 + Debug.LogError("세이브 전체 손상 → 새 세이브 생성"); + return CreateNewSave(); + } + } + + private Save LoadBackup() + { + try + { + return QuickSaveReader.Create("Save_Backup").Read("Backup"); + } + catch (System.Exception e) + { + Debug.LogWarning("Backup 세이브 로드 실패: " + e.Message); + + // 백업 시도 + if (QuickSaveRaw.Exists(MainSaveFilePath)) + { + Debug.LogWarning("메인 세이브로 복구 시도"); + return LoadMain(); + } + + // 메인도 없을 경우 새 세이브 생성 + Debug.LogError("세이브 전체 손상 → 새 세이브 생성"); + return CreateNewSave(); + } + } + + //더미 세이브 파일 생성 + private Save CreateNewSave() + { + var fresh = StatManager.instance.ToSaveData(); + SaveMain(); + SaveBackup(); + return fresh; + } + + //세이브 파일의 존재 여부 확인 + private void EnsureSaveExists() + { + if (!QuickSaveRaw.Exists(MainSaveFilePath)) // Save_Main 파일이 없을때 + { + if (!QuickSaveRaw.Exists(BackupSaveFilePath)) //Save_Backup 파일도 존재하지 않을때 + { + UpdateSaveInfo(); + SaveMain(); //Save_Main 파일 생성 + backupSave = LoadMain(); + SaveBackup(); //Save_Backup 파일 생성 + + Debug.Log("세이브가 존재하지 않아 새로운 세이브를 생성했습니다."); + } + else + { + mainSave = LoadBackup(); //백업을 메인으로 로드. + SaveMain(); + Debug.Log("메인을 백업 세이브로 로드했습니다."); + } + } + else + { + if (!QuickSaveRaw.Exists(BackupSaveFilePath)) //Save_Backup 파일이 없을떄 + { + backupSave = LoadMain(); + SaveBackup(); + Debug.Log("백업을 메인 세이브로 로드했습니다."); + } + + } + } + + //세이브 파일 디렉토리 확인 + private string GetSavePath(string fileNameWithoutExt) + { + string directory = Path.Combine(Application.persistentDataPath, SaveFolder); + + // 폴더가 없다면 생성 + if (!Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + return Path.Combine(directory, fileNameWithoutExt + ".json"); + } + + // 씬이 바뀔 때 마다 자동저장 + protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + StartCoroutine(SaveAfterOneFrame()); + } + + //Start함수 이후에 호출되도록 1프레임 지연 + IEnumerator SaveAfterOneFrame() + { + yield return null; + Save(); + Debug.Log("자동저장 되었습니다."); + } + + +} diff --git a/Assets/Editor/ReadOnlyDrawer.cs.meta b/Assets/KJM/KJM_Test/SaveManager.cs.meta similarity index 83% rename from Assets/Editor/ReadOnlyDrawer.cs.meta rename to Assets/KJM/KJM_Test/SaveManager.cs.meta index 3f22dabf..a0dbead0 100644 --- a/Assets/Editor/ReadOnlyDrawer.cs.meta +++ b/Assets/KJM/KJM_Test/SaveManager.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: eb5079b7064e2324890f78e18dfe7a6e +guid: 9c85bf88c19d95947bc288da9dfd85bf MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/KJM/KJM_Test/StatManager.cs b/Assets/KJM/KJM_Test/StatManager.cs new file mode 100644 index 00000000..b0129abc --- /dev/null +++ b/Assets/KJM/KJM_Test/StatManager.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; +using Random = UnityEngine.Random; + +/// +/// 테스트용 임시 클래스 +/// +public class StatManager : MonoBehaviour +{ + public static StatManager instance; + + public int attackLevel; + public int attackSpeedLevel; + public int heartLevel; + public int moveSpeedLevel; + public int evasionTimeLevel; + public int stageLevel; + + public float time; + public int day; + public float health; + public float reputation; + + public bool isEvent; //Todo 이벤트 여부 및 관련 조건들 추가 + public int mealCount; + public int houseworkCount; + + + private void Awake() + { + instance = this; + } + + /// + /// 스탯값 변화 + /// + public void ChangeValue() + { + float floatValue = Random.Range(0f, 2f); + Debug.Log(floatValue); + int intValue = Random.Range(0, 10); + Debug.Log(intValue); + + attackLevel = intValue; + attackSpeedLevel = intValue; + heartLevel = intValue; + moveSpeedLevel = intValue; + evasionTimeLevel = intValue; + stageLevel = intValue; + + + time = floatValue; + day = intValue; + health = floatValue; + reputation = floatValue; + + isEvent = false; + mealCount = intValue; + houseworkCount = intValue; + + Debug.Log("ChangeValue"); + + } + + /// + /// 스탯값 반환 + /// + /// + public Save ToSaveData() + { + return new Save + { + dungeonSave = new DungeonSave + { + attackLevel = this.attackLevel, + attackSpeedLevel = this.attackSpeedLevel, + heartLevel = this.heartLevel, + moveSpeedLevel = this.moveSpeedLevel, + evasionTimeLevel = this.evasionTimeLevel, + stageLevel = this.stageLevel + }, + homeSave = new HomeSave + { + time = this.time, + day = this.day, + health = this.health, + reputation = this.reputation, + isEvent = false, + mealCount = this.mealCount, + houseworkCount = this.houseworkCount, + } + }; + } + + public void loadSaveData2StataManager(Save saveData) + { + attackLevel = saveData.dungeonSave.attackLevel; + attackSpeedLevel = saveData.dungeonSave.attackSpeedLevel; + heartLevel = saveData.dungeonSave.heartLevel; + moveSpeedLevel = saveData.dungeonSave.moveSpeedLevel; + evasionTimeLevel = saveData.dungeonSave.evasionTimeLevel; + stageLevel = saveData.dungeonSave.stageLevel; + + time = saveData.homeSave.time; + day = saveData.homeSave.day; + health = saveData.homeSave.health; + reputation = saveData.homeSave.reputation; + isEvent = saveData.homeSave.isEvent; + mealCount = saveData.homeSave.mealCount; + houseworkCount = saveData.homeSave.houseworkCount; + } + + public void SceneChange() + { + SceneManager.LoadScene("Main"); + } +} diff --git a/Assets/KJM/KJM_Test/StatManager.cs.meta b/Assets/KJM/KJM_Test/StatManager.cs.meta new file mode 100644 index 00000000..a6714c1f --- /dev/null +++ b/Assets/KJM/KJM_Test/StatManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: afa35303e7c5e6141b05a11a7b5233ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/PlayerStats.cs b/Assets/KSH/PlayerStats.cs index 59a2ff09..0766c619 100644 --- a/Assets/KSH/PlayerStats.cs +++ b/Assets/KSH/PlayerStats.cs @@ -6,6 +6,20 @@ using Random = UnityEngine.Random; public class PlayerStats : MonoBehaviour { + public class StatsChangeData // 변경된 스탯 데이터 + { + public float Time { get; } + public float Health { get; } + public float Reputation { get; } + + public StatsChangeData(float time, float health, float reputation) + { + Time = time; + Health = health; + Reputation = reputation; + } + } + private GameConstants _gameConstants; private ValueByAction _valueByAction; @@ -17,6 +31,8 @@ public class PlayerStats : MonoBehaviour public event Action Exhaustion; // 탈진 public event Action Overslept; // 결근(늦잠) public event Action ZeroReputation; // 평판 0 이벤트 + public event Action OnStatsChanged; // 스탯 변경 이벤트 + public event Action OnWorked; // 퇴근 이벤트 (출근 이후 집에 돌아올 시간에 발생) private float previousAddHealth = 0f; @@ -49,6 +65,15 @@ public class PlayerStats : MonoBehaviour ModifyTime(effect.timeChange, actionType); ModifyHealth(effect.healthChange); ModifyReputation(effect.reputationChange); + + // 스탯 변경 이벤트 (UI 업데이트용) + OnStatsChanged?.Invoke(new StatsChangeData(TimeStat, HealthStat, ReputationStat)); + + // 스탯 - 시간이 변경된 이후 퇴근 이벤트 발생 + if (actionType == ActionType.Work) + { + OnWorked?.Invoke(); + } } // 출근 가능 여부 확인 메서드 diff --git a/Assets/KSH/TestCode/PlayerStatsTest.cs b/Assets/KSH/TestCode/PlayerStatsTest.cs deleted file mode 100644 index 445d3f8b..00000000 --- a/Assets/KSH/TestCode/PlayerStatsTest.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class PlayerStatsTest : MonoBehaviour -{ - [Header("현재 스탯")] - [SerializeField, ReadOnly] private float currentTime; - [SerializeField, ReadOnly] private float currentHealth; - [SerializeField, ReadOnly] private float currentReputation; - [SerializeField, ReadOnly] private int currentDay; - - [Header("테스트 액션")] - [Tooltip("액션을 선택하고 체크박스를 체크하여 실행")] - [SerializeField] private ActionType actionToTest; - [SerializeField] private bool executeAction; - - // 컴포넌트 참조 - [Header("필수 참조")] - [SerializeField] private PlayerStats playerStats; - [SerializeField] private GameManager gameManager; - - // ReadOnly 속성 (인스펙터에서 수정 불가능하게 만듦) - public class ReadOnlyAttribute : PropertyAttribute { } - - private void Start() - { - // 참조 찾기 (없을 경우) - if (playerStats == null) - { - playerStats = FindObjectOfType(); - Debug.Log("PlayerStats를 찾아 참조했습니다."); - } - - if (gameManager == null) - { - gameManager = FindObjectOfType(); - Debug.Log("GameManager를 찾아 참조했습니다."); - } - - // 초기 스탯 표시 업데이트 - UpdateStatsDisplay(); - } - - private void Update() - { - if (Application.isPlaying) - { - // 매 프레임마다 스탯 업데이트 - UpdateStatsDisplay(); - - // 체크박스가 체크되면 선택된 액션 실행 - if (executeAction) - { - ExecuteSelectedAction(); - executeAction = false; // 체크박스 초기화 - } - } - } - - private void UpdateStatsDisplay() - { - // 참조 확인 후 스탯 업데이트 - if (playerStats != null) - { - currentTime = playerStats.TimeStat; - currentHealth = playerStats.HealthStat; - currentReputation = playerStats.ReputationStat; - - // GameManager에서 날짜 정보 가져오기 - if (gameManager != null) - { - currentDay = gameManager.CurrentDay; - } - else - { - Debug.LogWarning("GameManager 참조가 없습니다."); - } - } - else - { - Debug.LogWarning("PlayerStats 참조가 없습니다."); - } - } - - private void ExecuteSelectedAction() - { - if (playerStats != null) - { - // 선택한 액션 실행 - playerStats.PerformAction(actionToTest); - UpdateStatsDisplay(); - Debug.Log($"액션 실행: {actionToTest}"); - - // 콘솔에 현재 스탯 정보 출력 - Debug.Log($"현재 스탯 - 시간: {currentTime}, 체력: {currentHealth}, 평판: {currentReputation}, 날짜: {currentDay}"); - } - else - { - Debug.LogError("PlayerStats 참조가 없어 액션을 실행할 수 없습니다."); - } - } -} \ No newline at end of file diff --git a/Assets/KSH/TestCode/Test.prefab b/Assets/KSH/TestCode/Test.prefab deleted file mode 100644 index b1d3b885..00000000 --- a/Assets/KSH/TestCode/Test.prefab +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5a65e9dd60a7532ae1c4c43fda477ac0940c18c5c60f8a164b6fd52b9f1f4a42 -size 5196 diff --git a/Assets/Scripts/Common.meta b/Assets/Scripts/Common.meta new file mode 100644 index 00000000..84267ee8 --- /dev/null +++ b/Assets/Scripts/Common.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3bf376b79225dd241aa996c1967947a1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/GameConstants.cs b/Assets/Scripts/Common/GameConstants.cs similarity index 100% rename from Assets/KSH/GameConstants.cs rename to Assets/Scripts/Common/GameConstants.cs diff --git a/Assets/KSH/GameConstants.cs.meta b/Assets/Scripts/Common/GameConstants.cs.meta similarity index 100% rename from Assets/KSH/GameConstants.cs.meta rename to Assets/Scripts/Common/GameConstants.cs.meta diff --git a/Assets/KSH/GameManager.cs b/Assets/Scripts/Common/GameManager.cs similarity index 94% rename from Assets/KSH/GameManager.cs rename to Assets/Scripts/Common/GameManager.cs index 80b0725b..a7f88c4b 100644 --- a/Assets/KSH/GameManager.cs +++ b/Assets/Scripts/Common/GameManager.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; -public class GameManager : Singleton +public partial class GameManager : Singleton { [SerializeField] private PlayerStats playerStats; @@ -20,6 +20,9 @@ public class GameManager : Singleton private void Start() { + // 오디오 초기화 + InitializeAudio(); + // PlayerStats의 하루 종료 이벤트 구독 if (playerStats == null) { diff --git a/Assets/KSH/GameManager.cs.meta b/Assets/Scripts/Common/GameManager.cs.meta similarity index 100% rename from Assets/KSH/GameManager.cs.meta rename to Assets/Scripts/Common/GameManager.cs.meta diff --git a/Assets/Scripts/Common/GameUtility.meta b/Assets/Scripts/Common/GameUtility.meta new file mode 100644 index 00000000..efd277a0 --- /dev/null +++ b/Assets/Scripts/Common/GameUtility.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0dc4cdf0dfb7ed14cb56b18f7ff733f4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Common/GameUtility/GameSound.cs b/Assets/Scripts/Common/GameUtility/GameSound.cs new file mode 100644 index 00000000..5ab5116e --- /dev/null +++ b/Assets/Scripts/Common/GameUtility/GameSound.cs @@ -0,0 +1,184 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +// 게임 매니저의 오디오 관련 부분 클래스 +public partial class GameManager : Singleton +{ + // 오디오 클립 참조 + [Header("오디오 설정")] + [SerializeField] private AudioClip housingBGM; + [SerializeField] private AudioClip dungeonBGM; + [SerializeField] private AudioClip gameOverBGM; + [SerializeField] private AudioClip victoryBGM; + + [SerializeField] private AudioClip buttonClickSFX; + + [Header("몬스터 효과음")] + [SerializeField] private AudioClip monsterAttackSFX; + [SerializeField] private AudioClip monsterDeathSFX; + + // 씬에 따른 배경음 맵핑 + private Dictionary sceneBGMMap = new Dictionary(); + + // 현재 재생 중인 BGM 트랙 + private string currentBGMTrack = ""; + + // 오디오 관련 초기화 + private void InitializeAudio() + { + // 씬-BGM 맵핑 초기화 + sceneBGMMap.Clear(); + sceneBGMMap.Add("Housing", housingBGM); // 씬 이름, 해당 씬 BGM + sceneBGMMap.Add("Game", dungeonBGM); + + // 오디오 클립 등록 (초기화) + if (SoundManager.Instance != null) + { + // BGM 등록 + if (housingBGM != null) SoundManager.Instance.LoadAudioClip("HousingBGM", housingBGM); + if (dungeonBGM != null) SoundManager.Instance.LoadAudioClip("DungeonBGM", dungeonBGM); + if (gameOverBGM != null) SoundManager.Instance.LoadAudioClip("GameOverBGM", gameOverBGM); + if (victoryBGM != null) SoundManager.Instance.LoadAudioClip("VictoryBGM", victoryBGM); + + // SFX 등록 + if (buttonClickSFX != null) SoundManager.Instance.LoadAudioClip("ButtonClick", buttonClickSFX); + + // 몬스터 SFX 등록 + if (monsterAttackSFX != null) SoundManager.Instance.LoadAudioClip("MonsterAttack", monsterAttackSFX); + if (monsterDeathSFX != null) SoundManager.Instance.LoadAudioClip("MonsterDeath", monsterDeathSFX); + + // 저장된 볼륨 설정 로드 + // LoadVolumeSettings(); + + // 현재 씬에 맞는 배경음 재생 + // string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; + // HandleSceneAudio(currentSceneName); + } + else + { + Debug.LogWarning("SoundManager 인스턴스를 찾을 수 없습니다."); + } + } + + #region 볼륨 제어 + + // BGM 볼륨 설정 (0.0 ~ 1.0) + public void SetVolumeBGM(float value) + { + if (SoundManager.Instance == null) return; + + value = Mathf.Clamp01(value); // 혹시 모를 범위 제한 + SoundManager.Instance.SetBGMVolume(value); + + // 설정 저장 + // PlayerPrefs.SetFloat("BGMVolume", value); + // PlayerPrefs.Save(); + } + + // SFX 볼륨 설정 (0.0 ~ 1.0) + public void SetVolumeSFX(float value) + { + if (SoundManager.Instance == null) return; + + value = Mathf.Clamp01(value); + SoundManager.Instance.SetSFXVolume(value); + + // 설정 저장 + // PlayerPrefs.SetFloat("SFXVolume", value); + // PlayerPrefs.Save(); + } + + // PlayerPrefs에 저장된 볼륨 설정 불러오기 + // private void LoadVolumeSettings() + // { + // float bgmVolume = PlayerPrefs.GetFloat("BGMVolume", 1.0f); + // float sfxVolume = PlayerPrefs.GetFloat("SFXVolume", 1.0f); + // + // // 저장된 볼륨 설정 적용 + // if (SoundManager.Instance != null) + // { + // SoundManager.Instance.SetBGMVolume(bgmVolume); + // SoundManager.Instance.SetSFXVolume(sfxVolume); + // } + // } + + #endregion + + // 씬에 따른 오디오 처리 + private void HandleSceneAudio(string sceneName) + { + if (SoundManager.Instance == null) return; + + // 이미 같은 트랙이 재생 중이면 중복 재생하지 않음 + if (currentBGMTrack == sceneName) return; + + // 씬에 맞는 BGM 재생 + if (sceneBGMMap.TryGetValue(sceneName, out AudioClip bgmClip)) + { + if (bgmClip != null) + { + SoundManager.Instance.PlayBGM(bgmClip, true, 1.5f); + currentBGMTrack = sceneName; + } + } + } + + #region 배경음 제어 (게임 오버, 승리도 이쪽) + + // 게임 오버 시 호출 + public void PlayGameOverMusic() + { + if (SoundManager.Instance == null) return; + + if (gameOverBGM != null) + { + SoundManager.Instance.PlayBGM(gameOverBGM, true, 1.0f); + currentBGMTrack = "GameOver"; + } + } + + // 승리 시 호출 + public void PlayVictoryMusic() + { + if (SoundManager.Instance == null) return; + + if (victoryBGM != null) + { + SoundManager.Instance.PlayBGM(victoryBGM, true, 1.0f); + currentBGMTrack = "Victory"; + } + } + + #endregion + + #region 효과음 제어 + + // 버튼 클릭 효과음 재생 + public void PlayButtonClickSound() + { + if (SoundManager.Instance == null) return; + + SoundManager.Instance.PlaySFX("ButtonClick"); + } + + #endregion + + #region 몬스터 오디오 + + public void PlayMonsterAttackSound() + { + if (SoundManager.Instance == null) return; + + SoundManager.Instance.PlaySFX("MonsterAttack"); + } + + public void PlayMonsterDeathSound() + { + if (SoundManager.Instance == null) return; + + SoundManager.Instance.PlaySFX("MonsterDeath"); + } + + #endregion +} \ No newline at end of file diff --git a/Assets/Scripts/Common/GameUtility/GameSound.cs.meta b/Assets/Scripts/Common/GameUtility/GameSound.cs.meta new file mode 100644 index 00000000..2591456e --- /dev/null +++ b/Assets/Scripts/Common/GameUtility/GameSound.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f076e9ce37f6f564c961ee8a9393c09d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/Singleton.cs b/Assets/Scripts/Common/Singleton.cs similarity index 100% rename from Assets/KSH/Singleton.cs rename to Assets/Scripts/Common/Singleton.cs diff --git a/Assets/KSH/Singleton.cs.meta b/Assets/Scripts/Common/Singleton.cs.meta similarity index 100% rename from Assets/KSH/Singleton.cs.meta rename to Assets/Scripts/Common/Singleton.cs.meta diff --git a/Assets/Scripts/Common/SoundManager.cs b/Assets/Scripts/Common/SoundManager.cs new file mode 100644 index 00000000..31c9713b --- /dev/null +++ b/Assets/Scripts/Common/SoundManager.cs @@ -0,0 +1,272 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; + +// SoundManager: 효과음 및 배경음 재생, 정지, 볼륨 조절, 페이드 효과 등을 관리 +public class SoundManager : Singleton +{ + // 오디오 소스 관리 + private AudioSource bgmSource; + private List sfxSources = new List(); + + // 볼륨 설정 (0.0 ~ 1.0) + [Range(0f, 1f)] public float bgmVolume = 1f; + [Range(0f, 1f)] public float sfxVolume = 1f; + + // 오디오 클립 저장소 + private Dictionary audioClips = new Dictionary(); + + // 동시에 재생 가능한 효과음 수 + private int maxSfxSources = 5; + + // 페이드 효과 진행 여부 + private bool isFading = false; + + private void Awake() + { + InitializeAudioSources(); + } + + private void InitializeAudioSources() + { + // 배경음 오디오 소스가 없으면 생성 + if (bgmSource == null) + { + bgmSource = gameObject.AddComponent(); + bgmSource.loop = true; + bgmSource.volume = bgmVolume; + } + + // 효과음 오디오 소스가 부족하면 추가 생성 + for (int i = 0; i < maxSfxSources; i++) + { + AudioSource sfxSource = gameObject.AddComponent(); + sfxSource.loop = false; + sfxSource.volume = sfxVolume; + sfxSources.Add(sfxSource); + } + } + + // 싱글톤 클래스의 추상 메서드 구현 + protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + // 씬 전환 시 음악 전체 정지 (효과음, 배경음 모두) + // StopAllSounds(); + } + + #region 오디오 클립 관리 + + // 오디오 클립을 audioClips에 저장 (식별을 위한 이름 포함) + public void LoadAudioClip(string name, AudioClip clip) + { + if (string.IsNullOrEmpty(name) || clip == null) return; + + if (!audioClips.ContainsKey(name)) + { + audioClips.Add(name, clip); + } + else + { + audioClips[name] = clip; + } + } + + #endregion + + #region 배경음 (BGM) 메서드 + + // 이름으로 배경음을 재생 + public void PlayBGM(string clipName, bool fade = false, float fadeTime = 1f) + { + if (string.IsNullOrEmpty(clipName) || !audioClips.ContainsKey(clipName)) return; + + PlayBGM(audioClips[clipName], fade, fadeTime); + } + + // 오디오 클립으로 배경음을 재생 + public void PlayBGM(AudioClip clip, bool fade = false, float fadeTime = 1f) + { + if (clip == null) return; + + if (bgmSource == null) InitializeAudioSources(); // 초기화 안됐을 경우 다시 초기화 + + // 같은 클립이 이미 재생 중이면 중복 재생하지 않음 + if (bgmSource.clip == clip && bgmSource.isPlaying) + { + return; + } + + if (fade && !isFading) + { + StartCoroutine(FadeBGM(clip, fadeTime)); + } + else + { + bgmSource.clip = clip; + bgmSource.volume = bgmVolume; + bgmSource.Play(); + } + } + + // 배경음을 정지 + public void StopBGM(bool fade = false, float fadeTime = 1f) + { + if (bgmSource == null || !bgmSource.isPlaying) return; + + if (fade && !isFading) + { + StartCoroutine(FadeOutBGM(fadeTime)); + } + else + { + bgmSource.Stop(); + } + } + + // 배경음 볼륨을 설정 + public void SetBGMVolume(float volume) + { + bgmVolume = Mathf.Clamp01(volume); + if (bgmSource == null) InitializeAudioSources(); + + bgmSource.volume = bgmVolume; + } + + #endregion + + #region 효과음 (SFX) 메서드 + + // 이름으로 효과음을 재생 + public AudioSource PlaySFX(string clipName) + { + if (string.IsNullOrEmpty(clipName) || !audioClips.ContainsKey(clipName)) return null; + + return PlaySFX(audioClips[clipName]); + } + + // 오디오 클립으로 효과음을 재생 + public AudioSource PlaySFX(AudioClip clip) + { + if (clip == null) return null; + + if (sfxSources == null || sfxSources.Count == 0) InitializeAudioSources(); // 초기화 + + // 사용 가능한 효과음 소스 찾기 + AudioSource sfxSource = null; + foreach (var source in sfxSources) + { + if (!source.isPlaying) + { + sfxSource = source; + break; + } + } + + // 모든 소스가 사용 중이면 첫 번째 소스 재사용 + if (sfxSource == null && sfxSources.Count > 0) + { + sfxSource = sfxSources[0]; + } + + sfxSource.clip = clip; + sfxSource.volume = sfxVolume; + sfxSource.Play(); + + return sfxSource; + } + + // 모든 효과음을 정지 + public void StopAllSFX() + { + foreach (var source in sfxSources) + { + source.Stop(); + } + } + + // 효과음 볼륨을 설정 + public void SetSFXVolume(float volume) + { + sfxVolume = Mathf.Clamp01(volume); + foreach (var source in sfxSources) + { + source.volume = sfxVolume; + } + } + + #endregion + + #region 전체 사운드 제어 + + /// 모든 사운드(배경음, 효과음)를 정지 + public void StopAllSounds() + { + StopBGM(); + StopAllSFX(); + } + + #endregion + + #region 페이드 효과 + + // 배경음을 페이드 인/아웃하며 전환 + private IEnumerator FadeBGM(AudioClip newClip, float fadeTime) + { + isFading = true; + + // 현재 재생 중인 배경음이 있으면 페이드 아웃 + if (bgmSource.isPlaying) + { + float startVolume = bgmSource.volume; + float time = 0; + + while (time < fadeTime / 2) + { + bgmSource.volume = Mathf.Lerp(startVolume, 0, time / (fadeTime / 2)); + time += Time.deltaTime; + yield return null; + } + + bgmSource.Stop(); + } + + // 새 클립 설정 후 페이드 인 + bgmSource.clip = newClip; + bgmSource.volume = 0; + bgmSource.Play(); + + float fadeInTime = 0; + while (fadeInTime < fadeTime / 2) + { + bgmSource.volume = Mathf.Lerp(0, bgmVolume, fadeInTime / (fadeTime / 2)); + fadeInTime += Time.deltaTime; + yield return null; + } + + bgmSource.volume = bgmVolume; + isFading = false; + } + + // 배경음을 페이드 아웃 + private IEnumerator FadeOutBGM(float fadeTime) + { + isFading = true; + + float startVolume = bgmSource.volume; + float time = 0; + + while (time < fadeTime) + { + bgmSource.volume = Mathf.Lerp(startVolume, 0, time / fadeTime); + time += Time.deltaTime; + yield return null; + } + + bgmSource.Stop(); + bgmSource.volume = bgmVolume; + isFading = false; + } + + #endregion +} \ No newline at end of file diff --git a/Assets/Scripts/Common/SoundManager.cs.meta b/Assets/Scripts/Common/SoundManager.cs.meta new file mode 100644 index 00000000..17425e0c --- /dev/null +++ b/Assets/Scripts/Common/SoundManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9b22d6af280f6ed4aa3a6dbeed301cd6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Save.meta b/Assets/Scripts/Save.meta new file mode 100644 index 00000000..fc681efe --- /dev/null +++ b/Assets/Scripts/Save.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0bc3d5a0f81acf64b850cc1570498ebc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: