feat: 캐릭터 레벨업 시 경험치, 공격력, 방어력, 잔여 경험치 데이터 업데이트 로직 적용
This commit is contained in:
@@ -7,6 +7,8 @@ namespace TON
|
||||
public class CharacterBase : MonoBehaviour, IDamage
|
||||
{
|
||||
|
||||
[SerializeField] //
|
||||
private PlayerData playerData;
|
||||
public float currentHP;
|
||||
public float maxHP;
|
||||
public float currentSP;
|
||||
@@ -41,42 +43,28 @@ namespace TON
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
int playerIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0);
|
||||
PlayerData playerData = PlayerDataManager.Singleton.playersData[playerIndex];
|
||||
// int playerIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0);
|
||||
PlayerDataManager.Singleton.SetCurrentUserData();
|
||||
playerData = PlayerDataManager.Singleton.player;
|
||||
|
||||
currentHP = maxHP = playerData.hp;
|
||||
currentSP = maxSP = playerData.mp;
|
||||
}
|
||||
|
||||
|
||||
public int level = 1; // 현재 레벨
|
||||
public int exp = 0; // 현재 경험치
|
||||
public int expVariable = 10; // 경험치 변수 (조정 가능)
|
||||
|
||||
// 현재 레벨에서 다음 레벨까지 필요한 경험치 계산
|
||||
private int GetRequiredExp(int currentLevel)
|
||||
{
|
||||
return (6 * currentLevel * currentLevel) + (currentLevel * expVariable);
|
||||
}
|
||||
|
||||
// 경험치 추가 및 레벨업 처리
|
||||
public void AddExp(int amount)
|
||||
{
|
||||
exp += amount; // 경험치 추가
|
||||
bool leveledUp = false; // 레벨업 여부 체크
|
||||
|
||||
while (exp >= GetRequiredExp(level)) // 경험치가 충분하면 반복해서 레벨업
|
||||
{
|
||||
exp -= GetRequiredExp(level); // 초과 경험치 유지
|
||||
level++; // 레벨 증가
|
||||
leveledUp = true;
|
||||
}
|
||||
bool leveledUp = PlayerDataManager.Singleton.UpdateExpericence(amount);
|
||||
|
||||
if (leveledUp)
|
||||
{
|
||||
// 경험치와 레벨 데이터를 파일에 업데이트 한다.
|
||||
Debug.Log($"레벨업! 현재 레벨: {level}, 남은 경험치: {exp}");
|
||||
// TODO: 레벨업 시 처리할 내용 추가
|
||||
Debug.Log($"레벨업! ");
|
||||
}
|
||||
|
||||
// 경험치와 변경된 데이터를 파일에 업데이트 한다.
|
||||
PlayerDataManager.Singleton.UpdatePlayerData();
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TON
|
||||
{
|
||||
public class PlayerDataManager : SingletonBase<PlayerDataManager>
|
||||
{
|
||||
// 사용자가 생성해둔 플레이어 데이터를 싱글톤으로 전역 사용하기 위함
|
||||
public List<PlayerData> playersData { get; private set; }
|
||||
|
||||
public PlayerData player { get; private set; }
|
||||
|
||||
[SerializeField]
|
||||
private int expVariable = 50; // 경험치 변수 (조정 가능)
|
||||
[SerializeField]
|
||||
private int attackGrowthFactor = 50; // 공격력 성장 변수 (조정 가능)
|
||||
[SerializeField]
|
||||
private int defensiveGrowthFactor = 200; // 방어력 성장 변수 (조정 가능)
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
LoadPlayerData();
|
||||
}
|
||||
|
||||
private void LoadPlayerData()
|
||||
{
|
||||
if (playersData != null)
|
||||
{
|
||||
playersData.Clear();
|
||||
}
|
||||
|
||||
JSONLoader.SaveJsonToPersistentData("player");
|
||||
playersData = JSONLoader.LoadJsonFromPersistentData<List<PlayerData>>("player");
|
||||
if (playersData == null)
|
||||
{
|
||||
playersData = new List<PlayerData>();
|
||||
}
|
||||
}
|
||||
|
||||
// 공격력과 방어력 업데이트
|
||||
private void UpdateStats(int currentLevel)
|
||||
{
|
||||
player.attackPower *= 1 + (currentLevel - 1) / attackGrowthFactor;
|
||||
player.defensivePower *= 1 + (currentLevel - 1) / defensiveGrowthFactor;
|
||||
}
|
||||
|
||||
// 현재 레벨에서 다음 레벨까지 필요한 경험치 계산
|
||||
private int GetRequiredExp(int currentLevel)
|
||||
{
|
||||
return (6 * currentLevel * currentLevel) + (currentLevel * expVariable);
|
||||
}
|
||||
|
||||
public bool UpdateExpericence(int amount)
|
||||
{
|
||||
// 경험치 추가
|
||||
player.experience += amount;
|
||||
|
||||
// 추가된 경험치로 인한 현재 경험치가 레벨업에 필요한 경험치보다 크거나 같다면 레벨업
|
||||
int requireLevelUpExp = GetRequiredExp(player.level);
|
||||
if (player.experience >= requireLevelUpExp)
|
||||
{
|
||||
// 레벨업 후 초과된 경험치를 반영하기 위해 다시 계산
|
||||
player.experience -= requireLevelUpExp;
|
||||
// 레벨업으로 인한 공격력/방어력 업데이트
|
||||
UpdateStats(player.level);
|
||||
// 레벨 증가
|
||||
player.level++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void UpdatePlayerData()
|
||||
{
|
||||
int index = playersData.FindIndex(x => x.id == player.id);
|
||||
if (index > -1)
|
||||
{
|
||||
playersData[index] = player;
|
||||
JSONLoader.SaveUpdatedJsonToPersistentData(playersData, "player");
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCurrentUserData()
|
||||
{
|
||||
int characterId = PlayerPrefs.GetInt("SelectedPlayerIndex", -1);
|
||||
if (characterId > -1)
|
||||
{
|
||||
player = playersData[characterId];
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("유효하지 않은 캐릭터 정보 입니다.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f52606f26bff734428ec67cdb89e0330
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user