Skip to content

Commit

Permalink
Merge pull request danielcollishaw#26 from danielcollishaw/save_system
Browse files Browse the repository at this point in the history
Save System Implemented
  • Loading branch information
danielcollishaw authored Apr 25, 2023
2 parents dc48b3e + bf9fcf6 commit 80f31d4
Show file tree
Hide file tree
Showing 261 changed files with 269 additions and 62,233 deletions.
63 changes: 63 additions & 0 deletions Assets/Scripts/GameSave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#region Copyright
/*---------------------------------------------------------------*/
/* File: GameSave.cs */
/* Firefly */
/* AI For Game Programming - CAP 4053 */
/* Copyright (c) 2023 Serenity Studios */
/* All rights reserved. */
/* Made with love, by Justin Sasso. */
/*---------------------------------------------------------------*/
#endregion

using System;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;

public class GameSave
{
public bool[] LevelsUnlocked { get; set; } = new bool[] { };

private const string SAVE_PATH = "save.json";
public GameSave() { }

public static GameSave LoadGameSave()
{
string jsonData;

try
{
using (StreamReader streamRdr = new StreamReader(CreatePath()))
{
jsonData = streamRdr.ReadToEnd();
}
}
catch (IOException ex)
{
Debug.Log("Couldn't LoadGameSave. Could be first startup: " + ex);
return new GameSave();
}

return DeserializeGameSave(jsonData);
}
public static bool SaveGameSave(GameSave gameSave)
{
string jsonData = SerializeGameSave(gameSave);

using (StreamWriter outputFile = new StreamWriter(CreatePath()))
{
outputFile.Write(jsonData);
}

return true;
}
private static string CreatePath()
{
string path = string.Format("{0}{1}", Application.dataPath, SAVE_PATH);
Debug.Log("Path created: " + path); // DEBUG
return path;
}

private static string SerializeGameSave(GameSave gameSave) => JsonConvert.SerializeObject(gameSave);
private static GameSave DeserializeGameSave(string jsonData) => JsonConvert.DeserializeObject<GameSave>(jsonData);
}
11 changes: 11 additions & 0 deletions Assets/Scripts/GameSave.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions Assets/Scripts/LevelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#endregion

using System;
using System.Text.Json;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

Expand All @@ -20,8 +21,18 @@ public static LevelManager Instance
{
get => instance;
}
public GameSave GameSave
{
get => gameSave;
}
public List<bool> UnlockedLevels
{
get => unlockedLevels;
}

private static LevelManager instance;
private GameSave gameSave;
private List<bool> unlockedLevels;

private void Awake()
{
Expand All @@ -34,11 +45,15 @@ private void Awake()
instance = this;
DontDestroyOnLoad(gameObject);

gameSave = GameSave.LoadGameSave();
unlockedLevels = gameSave.LevelsUnlocked.ToList();

Scene activeScene = SceneManager.GetActiveScene();
Debug.Log($"Welcome to {activeScene.name}!");
}
private bool Save()
private void OnDestroy()
{
return false;
gameSave.LevelsUnlocked = unlockedLevels.ToArray();
GameSave.SaveGameSave(gameSave);
}
}
15 changes: 14 additions & 1 deletion Assets/overworld/scenes/overworld.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
m_IndirectSpecularColor: {r: 0.18028334, g: 0.22571328, b: 0.3069217, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -419,6 +419,7 @@ GameObject:
m_Component:
- component: {fileID: 633880898}
- component: {fileID: 633880897}
- component: {fileID: 633880899}
m_Layer: 0
m_Name: Overworld
m_TagString: Untagged
Expand Down Expand Up @@ -453,6 +454,18 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &633880899
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 633880896}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ea161042421d7c34e86ff6c9324769eb, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &748575373
GameObject:
m_ObjectHideFlags: 0
Expand Down
12 changes: 10 additions & 2 deletions Assets/overworld/scripts/Overworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class Overworld : MonoBehaviour
Expand All @@ -25,10 +24,19 @@ private void Start()
private void DetectLevels()
{
OverworldLevel[] foundOverworldLevels = FindObjectsOfType<OverworldLevel>();
List<bool> unlockedLevels = LevelManager.Instance.UnlockedLevels;

for (int i = 0; i < foundOverworldLevels.Length; i++)
{
allOverworldLevels.Add(foundOverworldLevels[i]);
OverworldLevel level = foundOverworldLevels[i];

if (unlockedLevels.Count <= i)
{
unlockedLevels.Add(false);
}

level.Init(unlockedLevels[i]);
allOverworldLevels.Add(level);
}
}
}
15 changes: 5 additions & 10 deletions Assets/overworld/scripts/OverworldLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public Camera DevinCamera
private bool canLoad = true;
private bool canChooseLevel = false;
private bool levelLoading = false;

private bool unlocked;

private void Start()
{
Expand Down Expand Up @@ -74,18 +76,11 @@ private void Start()
Debug.Log("OverworldLevel>Couldn't get devinCamera.");
}
}
public void Init(string levelName)
public void Init(bool unlocked)
{
/*this.levelName = levelName;
this.unlocked = unlocked;

if (levelTextComp.TryGetComponent<TextMeshPro>(out var levelText))
{
levelText.text = levelName;
}
else
{
Debug.Log("Level text wasn't found!");
}*/
Debug.Log(levelName + "- unlocked: " + unlocked); // DEBUG
}
private void Update()
{
Expand Down
1 change: 1 addition & 0 deletions Assetssave.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"LevelsUnlocked":[false,false]}
Binary file not shown.
Binary file not shown.
23 changes: 0 additions & 23 deletions Packages/Microsoft.Bcl.AsyncInterfaces.7.0.0/LICENSE.TXT

This file was deleted.

Binary file not shown.
Loading

0 comments on commit 80f31d4

Please sign in to comment.