저장할 요소는 더 늘어날 수 있지만, 우선적으로 플레이어 능력치를 가지고 만들어봤다.
- 데이터를 관리할 스크립트
- 능력치가 변할 때 마다 자동 저장
- 게임 시작 시 이전 데이터를 받아 이어하기 가능
private void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
DataController.Instance.LoadGameData();
var _gameDate = DataController.Instance.gamaData;
att = _gameDate.player_Att;
getGold = _gameDate.player_Gold;
level = _gameDate.player_Level;
maxLevel = _gameDate.player_MaxLevel;
curreuntExp = _gameDate.player_CurrentExp;
maxExp = _gameDate.player_MaxExp;
currentHp = _gameDate.player_CurrentHP;
maxHp = _gameDate.player_MaxHP;
monsterHuntCount = _gameDate.player_MonsterHunterCount;
}
void Start()
{
weapon = GetComponentInChildren<Weapon>();
playerController = GetComponent<PlayerController>();
}
public float MaxHp
{
get
{
return maxHp;
}
set
{
maxHp = value;
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.PlayerHp(currentHp, maxHp);
DataController.Instance.gamaData.player_MaxHP = maxHp;
DataController.Instance.SaveGameData();
}
}
}
#region PlayerAttSet
public int Att
{
get
{
return att;
}
set
{
att = value;
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.PlayerAttText(att);
DataController.Instance.gamaData.player_Att = att;
DataController.Instance.SaveGameData();
}
}
}
#endregion
#region PlayerHpSet
public float CurrentHp
{
get
{
return currentHp;
}
set
{
currentHp = value;
if(currentHp > maxHp)
{
currentHp = maxHp;
}
if(currentHp <= 0)
{
currentHp = 0;
playerController.Die();
}
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.PlayerHp(currentHp, maxHp);
DataController.Instance.gamaData.player_CurrentHP = currentHp;
DataController.Instance.gamaData.player_MaxHP = maxHp;
DataController.Instance.SaveGameData();
}
}
}
#endregion
#region PlayerExp
public float CurreunExp
{
get
{
return curreuntExp;
}
set
{
curreuntExp = value;
if(curreuntExp >= maxExp)
{
curreuntExp = 0;
maxExp += maxExp;
level++;
Att += 20;
if(level >= maxLevel)
{
curreuntExp = 0;
level = maxLevel;
}
}
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.PlayerExpText(curreuntExp, maxExp);
PlayerState_UI.instance.PlayerLevel(level);
PlayerState_UI.instance.PlayerAttText(att);
DataController.Instance.gamaData.player_Level = level;
DataController.Instance.gamaData.player_CurrentExp = curreuntExp;
DataController.Instance.gamaData.player_MaxExp = maxExp;
DataController.Instance.SaveGameData();
}
}
}
#endregion
#region PlayerGold
public int GetGold
{
get
{
return getGold;
}
set
{
getGold = value;
if (getGold < 0)
{
getGold = 0;
}
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.PlayerGoldText(getGold);
DataController.Instance.gamaData.player_Gold = getGold;
DataController.Instance.SaveGameData();
}
}
}
#endregion
#region MonsterHunterCount
public int MonsterHunterCount
{
get
{
return monsterHuntCount;
}
set
{
monsterHuntCount = value;
if (PlayerState_UI.instance != null)
{
PlayerState_UI.instance.MonsterHunterCount(monsterHuntCount);
DataController.Instance.gamaData.player_MonsterHunterCount = monsterHuntCount;
DataController.Instance.SaveGameData();
}
}
}
#endregion