2025-02-17 18:53:40 +00:00

122 lines
4.1 KiB
C#

using Kryz.CharacterStats.Examples;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildManager : MonoBehaviour
{
[SerializeField] private GameTag characterClass;
[SerializeField] private List<BuildSlot> defaultBuildSlots = new List<BuildSlot>();
[SerializeField] private List<AbilityKeyBinder> abilityKeyBinders = new List<AbilityKeyBinder>();
[Header("Events:")]
[SerializeField] private GameEvent_BuildManager onBuildManagerInitialized;
[Header("Runtime Data:")]
[SerializeField] private List<BuildSlot> selectedBuildSlots = new List<BuildSlot>();
BuildData buildData = new BuildData();
PhotonView owner;
PlayerCharacterStats playerCharacterStats;
public GameTag CharacterClass => characterClass;
public List<BuildSlot> SelectedBuildSlots => selectedBuildSlots;
private void Awake()
{
owner = GetComponentInParent<PhotonView>();
playerCharacterStats = owner.GetComponent<PlayerCharacterStats>();
playerCharacterStats.level.OnLevelUpEvent.AddListener(CheckForSlotUnlockables);
}
// Start is called before the first frame update
void Start()
{
if (!owner.IsMine)
{
this.enabled = false;
return;
}
for (int i = 0; i < defaultBuildSlots.Count; i++)
{
selectedBuildSlots[i].ability = defaultBuildSlots[i].ability;
selectedBuildSlots[i].binderSlot = defaultBuildSlots[i].binderSlot;
}
TryGetSavedBuildData();
CheckForSlotUnlockables();
onBuildManagerInitialized.Raise(this);
}
private void TryGetSavedBuildData()
{
buildData = PlayerDataHandler.Instance.LoadCharacterBuildData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value);
if (buildData == null)
{
SaveCurrentBuild();
}
else
{
InitializeLoadedBuild();
}
}
private void SaveCurrentBuild()
{
if (buildData == null)
buildData = new BuildData();
for (int i = 0; i < buildData.buildSlotsdata.Length; i++)
{
buildData.buildSlotsdata[i].abilityIndex = AbilityIndexer.Instance.Abilities.IndexOf(selectedBuildSlots[i].ability);
buildData.buildSlotsdata[i].binderSlotIndex = selectedBuildSlots[i].binderSlot;
abilityKeyBinders[selectedBuildSlots[i].binderSlot].BindAbility(selectedBuildSlots[i].ability);
}
PlayerDataHandler.Instance.SaveCharacterBuildData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, buildData);
}
private void InitializeLoadedBuild()
{
for (int i = 0; i < buildData.buildSlotsdata.Length; i++)
{
selectedBuildSlots[i].ability = AbilityIndexer.Instance.Abilities[buildData.buildSlotsdata[i].abilityIndex];
selectedBuildSlots[i].binderSlot = buildData.buildSlotsdata[i].binderSlotIndex;
abilityKeyBinders[selectedBuildSlots[i].binderSlot].BindAbility(selectedBuildSlots[i].ability);
}
}
public void UpdateBuildOnAbilitiesChanged(int slotIndex, BaseAbility ability)
{
selectedBuildSlots[slotIndex].ability = ability;
selectedBuildSlots[slotIndex].binderSlot = slotIndex;
abilityKeyBinders[selectedBuildSlots[slotIndex].binderSlot].BindAbility(selectedBuildSlots[slotIndex].ability);
SaveCurrentBuild();
}
private void CheckForSlotUnlockables()
{
abilityKeyBinders[0].SetUnlockAbilitySlot(true);
abilityKeyBinders[1].SetUnlockAbilitySlot(playerCharacterStats.level.currentLevel >= GameConstants.GameBalancing.AbilitySlot_II_LevelRequired);
abilityKeyBinders[2].SetUnlockAbilitySlot(playerCharacterStats.level.currentLevel >= GameConstants.GameBalancing.AbilitySlot_III_LevelRequired);
//abilityKeyBinders[3].SetUnlockAbilitySlot(playerCharacterStats.level.currentLevel >= GameConstants.GameBalancing.AbilitySlot_IV_LevelRequired);
}
}