Skip to content

Commit

Permalink
Added more advanced save paths, and also added code to fallback on if…
Browse files Browse the repository at this point in the history
… no save exists.
  • Loading branch information
TraceEntertains committed May 5, 2022
1 parent 2015d50 commit d26df0d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
23 changes: 21 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Text_Based_Game
{
internal class Program
{
public static Player currentPlayer = new Player();
public static string appDataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public static string appDataFolderName = "\\GTBRPGEE";
public static string fullPath;
public static bool mainLoop = true;
static void Main(string[] args)
{
Expand All @@ -26,6 +30,9 @@ static void Main(string[] args)

static void Start()
{
if (Directory.Exists(appDataDirectory + appDataFolderName) == false)
Directory.CreateDirectory(appDataDirectory + appDataFolderName);
fullPath = appDataDirectory + appDataFolderName;
Clear();
WriteLine("Generic Text Based RPG - Epic Edition\n");
WriteLine("(N)ew Game");
Expand All @@ -37,8 +44,20 @@ static void Start()
}
else if (input == "l")
{
currentPlayer = SaveManager.LoadGame();
Shop.RunShop(currentPlayer);
Clear();
if (File.Exists(fullPath + "\\save.json"))
{
currentPlayer = SaveManager.LoadGame();
Shop.RunShop(currentPlayer);
Clear();
}
else
{
WriteLine("You do not have a save.");
ReadKey(true);
Clear();
Start();
}
Clear();
}
}
Expand Down
4 changes: 2 additions & 2 deletions SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class SaveManager
{
public static Player LoadGame()
{
FileStream save = File.OpenRead(Directory.GetCurrentDirectory() + "\\save.json");
FileStream save = File.OpenRead(Program.fullPath + "\\save.json");
Player player = JsonSerializer.Deserialize<Player>(save);
save.Close();
return player;
}

public static void SaveGame(Player player)
{
File.WriteAllText(Directory.GetCurrentDirectory() + "\\save.json", JsonSerializer.Serialize(player));
File.WriteAllText(Program.fullPath + "\\save.json", JsonSerializer.Serialize(player));
}
}
}

0 comments on commit d26df0d

Please sign in to comment.