393 lines
18 KiB
C#
393 lines
18 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
|
||
public class SaveTrigger : MonoBehaviour
|
||
{
|
||
[SerializeField] private Transform savedPlayerPosition;
|
||
[SerializeField] private List<string> forceToSaveAdditiveScene;
|
||
|
||
public string collisionTag = "Player";
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
if (other.CompareTag(collisionTag))
|
||
{
|
||
SaveGame();
|
||
}
|
||
}
|
||
|
||
public void SaveGame()
|
||
{
|
||
#region Old implementation
|
||
//// Save player data to slot 1
|
||
//GameSaveData dataSlot1 = new GameSaveData();
|
||
//dataSlot1.saveSlot = 0;
|
||
//dataSlot1.playerData.playerPosition = savedPlayerPosition.position;
|
||
//dataSlot1.playerData.activeCameraName = CameraManager.GetInstance().GetCurrentActiveCameraName(); // Implement this function
|
||
//dataSlot1.playerData.saveTriggersThatNeedToBeDeactivated.Add(gameObject.name);
|
||
//SaveSlotManager.SavePlayerData(dataSlot1);
|
||
//this.gameObject.SetActive(false);
|
||
#endregion
|
||
|
||
// Load existing data
|
||
GameSaveData existingData = SaveSlotManager.LoadPlayerData(SaveSlotManager.GetInstance().selectedSaveSlotID); // Assuming saveSlotID is 0
|
||
|
||
if (existingData != null)
|
||
{
|
||
existingData.playerData.playerPosition = savedPlayerPosition.position;
|
||
existingData.playerData.playerRotation = savedPlayerPosition.rotation;
|
||
//existingData.playerData.activeCameraName = CameraManager.GetInstance().GetCurrentActiveCameraName(); // Implement this function
|
||
// Check if the game object's name is already in the list
|
||
if (!existingData.playerData.saveTriggersThatNeedToBeDeactivated.Contains(gameObject.name))
|
||
{
|
||
// If not, add it to the list
|
||
existingData.playerData.saveTriggersThatNeedToBeDeactivated.Add(gameObject.name);
|
||
}
|
||
existingData.playerData.continueButtonEnabled = true;
|
||
//Save player Controller state:
|
||
existingData.playerData.playerControllerShouldBeEnabledOnGameLoad = GameProgressManager.GetInstance().playerControllerShouldBeEnabledOnGameLoad;
|
||
//Save Camera Controller state:
|
||
existingData.playerData.playerCameraControllerShouldBeEnabledOnGameLoad = GameProgressManager.GetInstance().playerCameraControllerShouldBeEnabledOnGameLoad;
|
||
//Set Sanity state:
|
||
existingData.playerData.playerSanityShouldBeEnabledOnGameLoad = GameProgressManager.GetInstance().playerSanityShouldBeEnabledOnGameLoad;
|
||
// Set the initial cutscene flag
|
||
existingData.gameProgressData.initialCutscenePlayed = GameProgressManager.GetInstance().initialCutscenePlayed;
|
||
//Fireflies.
|
||
existingData.gameProgressData.firefliesAreDead = GameProgressManager.GetInstance().firefliesAreDead;
|
||
//Save the state of the storyline
|
||
existingData.gameProgressData.currentStorylineState = GameProgressManager.GetInstance().currentStorylineState;
|
||
|
||
//Save the global storyline state
|
||
existingData.gameProgressData.currentGlobalStorylineState = GameProgressManager.GetInstance().currentGlobalStorylineState;
|
||
|
||
//Save Current Active scene
|
||
existingData.gameProgressData.currentActiveScene = SceneManager.GetActiveScene().name;
|
||
//Save Current AdditiveScenes
|
||
existingData.gameProgressData.currentAdditiveScenes = GetAdditiveScenes().ToArray();
|
||
//Save Current NecessaryScenes that will always have to be loaded until the game decides otherwise.
|
||
existingData.gameProgressData.currentNecessaryScenesToKeepLoaded = RoomManager.GetInstance().necessarySceneNamesToKeepLoaded;
|
||
|
||
//Saves all the inventory items
|
||
SaveInventoryItems(existingData);
|
||
//#region Force to save at start of game
|
||
//if (forceToSaveAdditiveScene.Count > 0)
|
||
//{
|
||
// existingData.gameProgressData.currentAdditiveScenes = forceToSaveAdditiveScene.ToArray();
|
||
//}
|
||
//#endregion
|
||
//Save If Necessary Objects of scene:
|
||
if (SaveableObjectsOnSceneManager.GetInstance() != null)
|
||
{
|
||
SaveableObjectsOnSceneManager.GetInstance().SaveSceneData(existingData);
|
||
}
|
||
//Save Room Triggers:
|
||
//existingData.sceneSaveData.RoomTriggerData.Add(RoomManager.GetInstance().roomConnections);
|
||
// Έλεγχος αν το αντικείμενο υπάρχει ήδη στη λίστα
|
||
SaveRoomTriggers(existingData);
|
||
|
||
//Save date and time:
|
||
existingData.saveTimestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
|
||
SaveSlotManager.UpdateAndSaveData(existingData);
|
||
SavingUIPrompt();
|
||
this.gameObject.SetActive(false);
|
||
}
|
||
|
||
//public void SavePlayerControllerState(bool shouldBeEnabled)
|
||
//{
|
||
// // Load existing data
|
||
// GameSaveData existingData = SaveSlotManager.LoadPlayerData(SaveSlotManager.GetInstance().selectedSaveSlotID);
|
||
// if (shouldBeEnabled == true)
|
||
// {
|
||
// existingData.playerData.playerControllerShouldBeEnabledOnGameLoad = true;
|
||
// }
|
||
// else
|
||
// {
|
||
// existingData.playerData.playerControllerShouldBeEnabledOnGameLoad = true;
|
||
// }
|
||
//}
|
||
|
||
//public void SavePlayerCameraControllerState(bool shouldBeEnabled)
|
||
//{
|
||
// // Load existing data
|
||
// GameSaveData existingData = SaveSlotManager.LoadPlayerData(SaveSlotManager.GetInstance().selectedSaveSlotID);
|
||
// if (shouldBeEnabled == true)
|
||
// {
|
||
// existingData.playerData.playerCameraControllerShouldBeEnabledOnGameLoad = true;
|
||
// }
|
||
// else
|
||
// {
|
||
// existingData.playerData.playerCameraControllerShouldBeEnabledOnGameLoad = true;
|
||
// }
|
||
//}
|
||
public void SaveRoomTriggers(GameSaveData saveData)
|
||
{
|
||
if (saveData.sceneSaveData.RoomTriggerData == null)
|
||
{
|
||
saveData.sceneSaveData.RoomTriggerData = new List<RoomTriggerData>();
|
||
}
|
||
|
||
// Χάρτης για γρήγορη πρόσβαση στα υπάρχοντα
|
||
Dictionary<string, RoomTriggerData> existingDataMap = saveData.sceneSaveData.RoomTriggerData
|
||
.ToDictionary(rt => rt.roomSceneName, rt => rt);
|
||
|
||
foreach (var snapshot in RoomManager.GetInstance().roomTriggersSnapshotData)
|
||
{
|
||
if (existingDataMap.ContainsKey(snapshot.roomSceneName))
|
||
{
|
||
// Αν υπάρχει ήδη, αντικατάσταση
|
||
existingDataMap[snapshot.roomSceneName].activeCategoryIndex = snapshot.activeCategoryIndex;
|
||
}
|
||
else
|
||
{
|
||
// Αν είναι νέο, προσθήκη
|
||
RoomTriggerData newData = new RoomTriggerData
|
||
{
|
||
roomSceneName = snapshot.roomSceneName,
|
||
activeCategoryIndex = snapshot.activeCategoryIndex
|
||
};
|
||
saveData.sceneSaveData.RoomTriggerData.Add(newData);
|
||
}
|
||
}
|
||
|
||
Debug.Log($"[SaveTrigger] Saved/Updated {RoomManager.GetInstance().roomTriggersSnapshotData.Count} RoomTriggers (total: {saveData.sceneSaveData.RoomTriggerData.Count}).");
|
||
}
|
||
|
||
#region Inventory Related
|
||
|
||
public void SaveInventoryItems(GameSaveData existingData)
|
||
{
|
||
//Save KeyPickups
|
||
SaveKeyItems(existingData);
|
||
//Save Regular items
|
||
SaveRegularItems(existingData);
|
||
//Save Obtained items from the environment in order to not reappear
|
||
SaveEnvironmentPickedUpItems(existingData);
|
||
//Save Flashlight Batteries Count and environment state
|
||
SaveBatteries(existingData);
|
||
//Save Pills count and Environment state
|
||
SavePills(existingData);
|
||
//Save Pocket watch if obtained or not
|
||
SavePocketWatch(existingData);
|
||
//Save Diary Entry if obtained
|
||
//SaveDiaryEntry(existingData);
|
||
SaveManager.GetInstance().SaveDiary(existingData);
|
||
//Save Flashlight batter life
|
||
existingData.inventoryData.Flashlight.flashlightBatteryLife = FlashlightController.GetInstance()._flashlightLife;
|
||
//Save Candle Holder
|
||
SaveCandleHolder(existingData);
|
||
}
|
||
|
||
public void SaveCandleHolder(GameSaveData existingData)
|
||
{
|
||
if (existingData.inventoryData.CandleHolder.candleHolderIsCollected != true)
|
||
{
|
||
existingData.inventoryData.CandleHolder.candleHolderIsCollected = InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedCandleHolderItemData.candleHolderIsCollected;
|
||
existingData.inventoryData.CandleHolder.hasCandle = InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedCandleHolderItemData.hasCandle;
|
||
existingData.inventoryData.CandleHolder.candleLife = InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedCandleHolderItemData.candleLife;
|
||
}
|
||
}
|
||
|
||
public void SaveRegularItems(GameSaveData existingData)
|
||
{
|
||
|
||
|
||
//for (int i = existingData.inventoryData.KeyPickupInventoryItems.Count - 1; i >= 0; i--)
|
||
//{
|
||
// var key = existingData.inventoryData.KeyPickupInventoryItems[i];
|
||
|
||
// for (int j = 0; j < KeysManager.GetInstance().keysToRemoveFromSavedData.Count; j++)
|
||
// {
|
||
// if (key.KeyNameID.Contains(KeysManager.GetInstance().keysToRemoveFromSavedData[j]))
|
||
// {
|
||
// existingData.inventoryData.KeyPickupInventoryItems.RemoveAt(i);
|
||
// KeysManager.GetInstance().keysToRemoveFromSavedData.RemoveAt(j);
|
||
// print("removed key: " + key.KeyNameID + "From saved data");
|
||
// break; // Avoid unnecessary iterations after removal
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
foreach (var RegularItemData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedRegularItemsInventoryData)
|
||
{
|
||
// Έλεγχος αν υπάρχει ήδη στοιχείο με το ίδιο όνομα
|
||
if (!existingData.inventoryData.RegularItems.Any(item => item.ItemName == RegularItemData.ItemName))
|
||
{
|
||
existingData.inventoryData.RegularItems.Add(RegularItemData);
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
public void SaveEnvironmentPickedUpItems(GameSaveData existingData)
|
||
{
|
||
foreach (var ObtainedItemData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilyObtainedItemsData)
|
||
{
|
||
existingData.inventoryData.ObtainedItems.Add(ObtainedItemData);
|
||
}
|
||
}
|
||
|
||
public void SaveKeyItems(GameSaveData existingData)
|
||
{
|
||
//foreach (var KeyPickupData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedKeysInventoryItemsData)
|
||
//{
|
||
// existingData.inventoryData.KeyPickupInventoryItems.Add(KeyPickupData);
|
||
//}
|
||
|
||
//var temporaryKeys = InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedKeysInventoryItemsData;
|
||
//// Διατήρηση μόνο των στοιχείων που υπάρχουν και στη temporary λίστα
|
||
//existingData.inventoryData.KeyPickupInventoryItems = existingData.inventoryData.KeyPickupInventoryItems
|
||
// .Where(item => temporaryKeys.Any(tempItem => tempItem.KeyNameID == item.KeyNameID))
|
||
// .ToList();
|
||
for (int i = existingData.inventoryData.KeyPickupInventoryItems.Count - 1; i >= 0; i--)
|
||
{
|
||
var key = existingData.inventoryData.KeyPickupInventoryItems[i];
|
||
|
||
for (int j = 0; j < KeysManager.GetInstance().keysToRemoveFromSavedData.Count; j++)
|
||
{
|
||
if (key.KeyNameID.Contains(KeysManager.GetInstance().keysToRemoveFromSavedData[j]))
|
||
{
|
||
existingData.inventoryData.KeyPickupInventoryItems.RemoveAt(i);
|
||
KeysManager.GetInstance().keysToRemoveFromSavedData.RemoveAt(j);
|
||
print("removed key: " + key.KeyNameID + "From saved data");
|
||
break; // Avoid unnecessary iterations after removal
|
||
}
|
||
}
|
||
}
|
||
|
||
foreach (var KeyPickupData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedKeysInventoryItemsData)
|
||
{
|
||
// Έλεγχος αν υπάρχει ήδη στοιχείο με το ίδιο όνομα
|
||
if (!existingData.inventoryData.KeyPickupInventoryItems.Any(item => item.KeyNameID == KeyPickupData.KeyNameID))
|
||
{
|
||
existingData.inventoryData.KeyPickupInventoryItems.Add(KeyPickupData);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
public void SaveBatteries(GameSaveData existingData)
|
||
{
|
||
existingData.inventoryData.BatteriesPickup.BatteriesCollected = BatteryManager.GetInstance().batteriesCollected;
|
||
foreach (var IndividualBatteriesPickupData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedBatteriesInventoryItemsData)
|
||
{
|
||
existingData.inventoryData.IndividualBatteriesPickup.Add(IndividualBatteriesPickupData);
|
||
}
|
||
}
|
||
|
||
public void SavePills(GameSaveData existingData)
|
||
{
|
||
existingData.inventoryData.PillsPickup.PillsCollected = PlayerHealthManager.GetInstance().pillsCollected;
|
||
foreach (var IndividualPillsPickupData in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedPillsInventoryItemsData)
|
||
{
|
||
existingData.inventoryData.IndividualPillsPickup.Add(IndividualPillsPickupData);
|
||
}
|
||
}
|
||
|
||
public void SavePocketWatch(GameSaveData existingData)
|
||
{
|
||
//FIX!!!
|
||
if (existingData.inventoryData.pocketWatchInventoryItem.pocketWatchCollected != true)
|
||
{
|
||
existingData.inventoryData.pocketWatchInventoryItem.pocketWatchCollected = InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedPocketWatchInventoryItemData.pocketWatchCollected;
|
||
}
|
||
}
|
||
|
||
public void SaveDiaryEntry(GameSaveData existingData)
|
||
{
|
||
//if (InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedDiaryEntryInventoryData != null
|
||
// && InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedDiaryEntryInventoryData.Count > 0)
|
||
//{
|
||
// foreach (var diaryEntry in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedDiaryEntryInventoryData)
|
||
// {
|
||
// existingData.collectablesData.DiaryEntryItem.Add(diaryEntry);
|
||
// }
|
||
// //AFTER FOREACH DELETE TEMPORARILY DIARY ENTRY DATA:
|
||
// InventoryManager.GetInstance().DeleteTemporarilyDiaryEntryInventoryItemData();
|
||
//}
|
||
StartCoroutine(SaveDiaryCoroutine(existingData));
|
||
}
|
||
#endregion
|
||
|
||
IEnumerator SaveDiaryCoroutine(GameSaveData existingData)
|
||
{
|
||
print("Is trying to save diary");
|
||
if (InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedDiaryEntryInventoryData != null)
|
||
{
|
||
foreach (var diaryEntry in InventoryManager.GetInstance().temporarilySavedInventoryItemsData.temporarilySavedDiaryEntryInventoryData)
|
||
{
|
||
existingData.collectablesData.DiaryEntryItem.Add(diaryEntry);
|
||
print("Saved diary entry!");
|
||
}
|
||
//AFTER FOREACH DELETE TEMPORARILY DIARY ENTRY DATA:
|
||
yield return new WaitForSecondsRealtime(0.5f);
|
||
InventoryManager.GetInstance().DeleteTemporarilyDiaryEntryInventoryItemData();
|
||
print("Deleted temporarily diary entry data");
|
||
|
||
}
|
||
}
|
||
|
||
public void SaveWhiteLedgesOptions(bool whiteLedgesOn)
|
||
{
|
||
// Load existing data
|
||
GameSaveData existingData = SaveSlotManager.LoadPlayerData(SaveSlotManager.GetInstance().selectedSaveSlotID); // Assuming saveSlotID is 0
|
||
|
||
if (existingData != null)
|
||
{
|
||
// Update game options data
|
||
existingData.gameOptionsData.whiteLedgesAreOn = whiteLedgesOn;
|
||
}
|
||
|
||
SaveSlotManager.UpdateAndSaveData(existingData);
|
||
SavingUIPrompt();
|
||
}
|
||
|
||
public void SavingUIPrompt()
|
||
{
|
||
SaveSlotManager.GetInstance().savingUIPlayable.Play();
|
||
}
|
||
|
||
List<string> GetAdditiveScenes()
|
||
{
|
||
List<string> additiveScenes = new List<string>();
|
||
|
||
// Get the name of the main scene
|
||
string mainSceneName = SceneManager.GetActiveScene().name;
|
||
|
||
// Get the number of loaded scenes
|
||
int sceneCount = SceneManager.sceneCount;
|
||
|
||
// Iterate through each loaded scene
|
||
for (int i = 0; i < sceneCount; i++)
|
||
{
|
||
Scene scene = SceneManager.GetSceneAt(i);
|
||
|
||
// Check if the scene is an additive scene
|
||
if (scene.isLoaded && scene.name != mainSceneName && scene.name != "Systems&Player")
|
||
{
|
||
Debug.Log("Additive Scene Name: " + scene.name);
|
||
additiveScenes.Add(scene.name);
|
||
// Do something with the additive scene
|
||
}
|
||
}
|
||
return additiveScenes;
|
||
|
||
}
|
||
|
||
public void SaveAfterSeconds(float seconds)
|
||
{
|
||
StartCoroutine(SaveAfterSecondsCoroutine(seconds));
|
||
}
|
||
|
||
IEnumerator SaveAfterSecondsCoroutine(float seconds)
|
||
{
|
||
yield return new WaitForSecondsRealtime(seconds);
|
||
SaveGame();
|
||
}
|
||
}
|