Skip to content

Commit

Permalink
fix: Use app settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Aug 1, 2023
1 parent fe04ce6 commit bfcf260
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 233 deletions.
71 changes: 40 additions & 31 deletions Assets/_BossFight/Resources/BF_Settings.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
--- !u!1 &165316
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 5
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 427038}
- component: {fileID: 11423690}
- component: {fileID: 2040626121319543717}
- component: {fileID: 11476276}
- component: {fileID: 11477830}
m_Layer: 0
Expand All @@ -20,46 +22,63 @@ GameObject:
m_IsActive: 1
--- !u!4 &427038
Transform:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 165316}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &11423690
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 165316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 44537bddf53394d4ba5246f002f0fbb0, type: 3}
m_Name:
m_EditorClassIdentifier:
mType: 0
--- !u!114 &2040626121319543717
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 165316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e15937d15e61fb34cbf14c466ec06293, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &11476276
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 165316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9764ce3382cb2f94a9fe5100d919e8c9, type: 3}
m_Name:
m_EditorClassIdentifier:
mFullFilePath:
mFullFileName:
CHARACTERS_IN_TEAM: 2
CHARACTERS_IN_GAME:
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 2}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 2}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 2}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 2}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 3}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 3}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 3}
- {fileID: 11474578, guid: 73ce54cd4bf0c4247a77c697d93cd090, type: 3}
FILE_PATH: SavedData/
FILE_NAME: BF_GameData
TOTAL_MOB_IN_SCENE: 15
Expand All @@ -68,24 +87,14 @@ MonoBehaviour:
LEVEL_SELECTED_NAME:
--- !u!114 &11477830
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 165316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9163d7bc70d879d4fa24769c3703d4c1, type: 3}
m_Name:
m_EditorClassIdentifier:
EXP_BASE: 'Get exp '
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_ParentPrefab: {fileID: 0}
m_RootGameObject: {fileID: 165316}
m_IsPrefabParent: 1
2 changes: 1 addition & 1 deletion Assets/_BossFight/Scripts/BF_GoldObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ protected override void Awake()
/// <param name="other"> collider detection if needed. </param>
public void AfterPicked(Collider other)
{
BF_GameSettings.GAME_DATA.Cash += mCashValue;
BF_AppSettings.instance.APP_DATA.Cash += mCashValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* $File: BF_GameData.cs $
* $File: BF_AppData.cs $
* $Date: $
* $Revision: $
* $Creator: Jen-Chieh Shen $
Expand All @@ -13,7 +13,7 @@
/// Game Data design here.
/// </summary>
[Serializable] // NOTE(jenchieh): we need this for `JCS_BinGameData'.
public class BF_GameData : JCS_BinData
public class BF_AppData : JCS_BinData
{
/* Variables */

Expand Down Expand Up @@ -47,7 +47,7 @@ public struct ItemIdInclude

/* Functions */

public BF_GameData()
public BF_AppData()
{

// get the length of the enum.
Expand Down
132 changes: 132 additions & 0 deletions Assets/_BossFight/Scripts/Settings/BF_AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* $File: BF_AppSettings.cs $
* $Date: $
* $Revision: $
* $Creator: Jen-Chieh Shen $
* $Notice: See LICENSE.txt for modification and distribution information
* Copyright (c) 2023 by Shen, Jen-Chieh $
*/
using System.IO;
using UnityEngine;
using JCSUnity;
using MyBox;

public class BF_AppSettings : JCS_Settings<BF_AppSettings>
{
/* Variables */

[Separator("Check Variables (BF_GameSettings)")]

[SerializeField]
[ReadOnly]
private string mFullFilePath = "";

[SerializeField]
[ReadOnly]
private string mFullFileName = "";

[Separator("Runtime Variables (BF_AppSettings)")]

[Header("- Save Load")]

public string FILE_PATH = "SavedData/";

public string FILE_NAME = "BF_AppData";

public BF_AppData APP_DATA = null;

/* Setter & Getter */

/* Functions */

private void Awake()
{
instance = CheckSingleton(instance, this);
}

private void Start()
{
// IMPORTANT: initial the path before save and load!
InitPath();

// only load once
if (APP_DATA == null)
LoadAppData();

// set load and save game data
var apps = JCS_ApplicationSettings.instance;
apps.SAVE_APP_DATA_FUNC = SaveAppData;
apps.LOAD_APP_DATA_FUNC = LoadAppData;
}

/// <summary>
/// Instead of Unity Engine's scripting layer's DontDestroyOnLoad.
/// I would like to use own define to transfer the old instance
/// to the newer instance.
///
/// Every time when unity load the scene. The script have been
/// reset, in order not to lose the original setting.
/// transfer the data from old instance to new instance.
/// </summary>
/// <param name="_old"> old instance </param>
/// <param name="_new"> new instance </param>
protected override void TransferData(BF_AppSettings _old, BF_AppSettings _new)
{
_new.APP_DATA = _old.APP_DATA;
}

/// <summary>
/// Initialize the path base on the JCSUnity Framework's
/// format.
/// </summary>
private void InitPath()
{
var apps = JCS_ApplicationSettings.instance;

mFullFilePath = JCS_Path.Combine(Application.persistentDataPath, apps.DATA_PATH, FILE_PATH);
mFullFileName = FILE_NAME + apps.DATA_EXTENSION;
}

private void LoadAppData()
{
JCS_IO.CreateDirectory(mFullFilePath);

// if file does not exist, create the default value file!
if (!File.Exists(mFullFilePath + mFullFileName))
{
CreateDefaultGameData();
return;
}

// else we just load the data commonly.
APP_DATA = BF_AppData.LoadFromFile<BF_AppData>(mFullFilePath, mFullFileName);
}

/// <summary>
/// Use only when player "First" play this game or
/// "Reset" the game.
/// </summary>
private void CreateDefaultGameData()
{
APP_DATA = new BF_AppData();

// Set game data's default values
{
APP_DATA.Name = "";
APP_DATA.Cash = 1500; // [default: 1500]
}

SaveAppData();
}

private void SaveAppData()
{
if (APP_DATA == null)
{
JCS_Debug.LogError("Save Data without data");
return;
}

APP_DATA.Save<BF_AppData>(mFullFilePath, mFullFileName);
}
}
11 changes: 11 additions & 0 deletions Assets/_BossFight/Scripts/Settings/BF_AppSettings.cs.meta

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

Loading

0 comments on commit bfcf260

Please sign in to comment.