Skip to content
Snippets Groups Projects
Commit a5be93bf authored by MarcelRyan's avatar MarcelRyan
Browse files

[FEAT] Pushing save for debugging

parent f0476358
1 merge request!3Save debug
......@@ -49,6 +49,22 @@ public class FileDataHandler
return loadedData;
}
public void Delete()
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
if (File.Exists(fullPath))
{
File.Delete(fullPath);
Debug.Log("File deleted successfully.");
}
else
{
Debug.LogWarning("File not found.");
}
}
public void Save(GameData data)
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
......
......@@ -33,8 +33,25 @@ public class GameData
this.baseDamage = 40;
this.currentPosition = new Vector3(0f, 0f, 0f);
this.currentWeapon = 0;
this.currentScene = 4;
this.currentQuest = 1;
this.currentScene = 0;
this.currentQuest = 0;
this.shopKeeperTime = 120f;
}
public GameData(int currentScene)
{
this.score = 0;
this.coin = 0;
this.pets = new List<string>();
this.petsHealth = new List<double>();
this.playerName = string.Empty;
this.playerSpeed = 6;
this.playerHealth = 100;
this.baseDamage = 40;
this.currentPosition = new Vector3(0f, 0f, 0f);
this.currentWeapon = 0;
this.currentScene = currentScene;
this.currentQuest = 0;
this.shopKeeperTime = 120f;
}
......
......@@ -22,6 +22,7 @@ public class SaveRadius : MonoBehaviour
{
timer = 0;
DataPersistenceManager.instance.SaveGame();
DataPersistenceManager.instance.SafeFromSafehouse();
}
}
......
......@@ -44,12 +44,14 @@ public class MainMenu : MonoBehaviour
public void PlayGame()
{
Debug.Log("On New Game Clicked");
LevelManager.Instance.Next();
DataPersistenceManager.instance.NewGame();
DataPersistenceManager.instance.LoadGame();
LevelManager.Instance.Next();
}
public void QuitGame()
{
DataPersistenceManager.instance.DeleteFile();
Application.Quit();
}
......
......@@ -8,9 +8,11 @@ public class DataPersistenceManager : MonoBehaviour
[Header("File Storage Config")]
[SerializeField] private string fileName;
private FileDataHandler dataHandler;
public static DataPersistenceManager instance { get; private set; }
public static DataPersistenceManager instance { get; private set; }
private GameData gameData;
private List<IDataPersistence> dataPersistenceList;
private bool newGame = false;
private bool saveSafeHouse = false;
private void Awake()
{
......@@ -36,10 +38,15 @@ public class DataPersistenceManager : MonoBehaviour
private void OnApplicationQuit()
{
if (gameData.playerHealth > 0)
this.dataHandler.Delete();
}
public void DeleteFile()
{
if (!saveSafeHouse)
{
SaveGame();
return;
this.dataHandler.Delete();
this.gameData = null;
}
}
......@@ -57,24 +64,41 @@ public class DataPersistenceManager : MonoBehaviour
public void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
this.dataPersistenceList = FindAllDataPersistence();
LoadGame();
if (scene.name.Contains("MainMenu") || scene.name.Contains("Level"))
{
this.dataPersistenceList = FindAllDataPersistence();
LoadGame();
}
}
public void OnSceneUnloaded(Scene scene)
{
SaveGame();
Debug.Log("OnSceneUnloaded called");
if (scene.name.Contains("03") || scene.name.Contains("04") || scene.name.Contains("05") || scene.name.Contains("06"))
{
SaveGame();
}
}
public void NewGame()
{
this.gameData = new GameData();
this.newGame = true;
}
public void RetryGame(int scene)
{
this.gameData = new GameData(scene);
}
public void LoadGame()
{
// TODO - load saved data
this.gameData = dataHandler.Load();
if (!newGame)
{
Debug.Log("Masuk ga");
this.gameData = dataHandler.Load();
}
// if no data can be loaded, dont continue
if (this.gameData == null || this.gameData.playerHealth <= 0)
......@@ -106,8 +130,24 @@ public class DataPersistenceManager : MonoBehaviour
Debug.Log("Save Game called");
}
public void SaveSceneGame()
{
// TODO - pass the data to other scripts
foreach (IDataPersistence dataPersistence in dataPersistenceList)
{
dataPersistence.SaveData(ref gameData);
}
Debug.Log("Save Scene Game called");
}
public bool HasGameData()
{
return gameData != null;
}
public void SafeFromSafehouse()
{
this.saveSafeHouse = true;
}
}
......@@ -38,11 +38,13 @@ namespace Nightmare
public void ResetLevel()
{
DataPersistenceManager.instance.RetryGame(1);
LevelManager.Instance.ResetLevel();
}
public void ExitGame()
{
DataPersistenceManager.instance.DeleteFile();
LevelManager.Instance.Exit();
}
......
......@@ -73,12 +73,14 @@ namespace Nightmare
{
this.currentIndex = data.currentScene;
this.currentQuest = data.currentQuest;
Debug.Log("Load data scene and quest " + this.currentIndex + " " + this.currentQuest);
}
public void SaveData(ref GameData data)
{
data.currentScene = this.currentIndex;
data.currentQuest = this.currentQuest;
Debug.Log("Save data scene and quest " + this.currentIndex + " " + this.currentQuest);
}
private void Update()
......
......@@ -13,19 +13,19 @@ public class PetManager : MonoBehaviour, IDataPersistence
player = GameObject.FindGameObjectWithTag("Player");
}
public void SpawnPet(string petTag, double petHealth)
public void SpawnPet(string petTag, double petHealth, Vector3 playerPosition)
{
GameObject pet = null;
if (petTag == "PetHealer")
{
pet = Instantiate(PetHealer, player.transform.position, player.transform.rotation);
pet = Instantiate(PetHealer, playerPosition, Quaternion.identity);
PetHealth healthofPet = pet.GetComponent<PetHealth>();
healthofPet.currentHealth = petHealth;
}
else if (petTag == "PetFighter")
{
pet = Instantiate(PetFighter, player.transform.position, player.transform.rotation);
pet = Instantiate(PetFighter, playerPosition, Quaternion.identity);
PetHealth healthofPet = pet.GetComponent<PetHealth>();
healthofPet.currentHealth = petHealth;
......@@ -34,14 +34,10 @@ public class PetManager : MonoBehaviour, IDataPersistence
public void LoadData(GameData data)
{
if (player == null)
{
player = GameObject.FindGameObjectWithTag("Player");
}
for (int i = 0; i < data.pets.Count; i++)
{
SpawnPet(data.pets[i], data.petsHealth[i]);
SpawnPet(data.pets[i], data.petsHealth[i], data.currentPosition);
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment