Persist equipment through scenes

equipment is persistent on scene change, and stats stay updated.
still missing item persistence through sessions.
This commit is contained in:
Pedro Gomes 2024-05-13 11:51:04 +01:00
parent d12942bff4
commit 8ce897c3aa
7 changed files with 92 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using UnityEngine.Events;
namespace Kryz.CharacterStats
@ -57,6 +58,11 @@ namespace Kryz.CharacterStats
return false;
}
public virtual bool HasModifiersFromSource(object source)
{
return StatModifiers.Any(mod => mod.Source == source);
}
public virtual bool RemoveAllModifiersFromSource(object source)
{
int numRemovals = statModifiers.RemoveAll(mod => mod.Source == source);

View File

@ -51,5 +51,10 @@ namespace Kryz.CharacterStats.Examples
}
return false;
}
public EquipmentSlot[] GetCurrentSlots()
{
return equipmentSlots;
}
}
}

View File

@ -32,6 +32,8 @@ namespace Kryz.CharacterStats.Examples
CharacterData characterData = new CharacterData();
string characterDataKey;
List<CharacterStat> statsCollection = new List<CharacterStat>();
protected override void Awake()
{
player = GetComponentInParent<RiftPlayer>();
@ -45,6 +47,12 @@ namespace Kryz.CharacterStats.Examples
base.Awake();
statsCollection.Add(Strength);
statsCollection.Add(Agility);
statsCollection.Add(Intelligence);
statsCollection.Add(Spirit);
statsCollection.Add(Vitality);
if (!photonView.IsMine) return;
if (GameStatePersistenceManager.Instance.HasDataForKey(characterDataKey))
@ -93,11 +101,20 @@ namespace Kryz.CharacterStats.Examples
equipmentPanel.OnItemRightClickedEvent.AddListener(UnequipFromEquipPanel);
RefreshCurrentEquipmentStats();
level.OnLevelUpEvent.AddListener(OnLevelUp);
level.OnExperienceChanged.AddListener(OnExperienceChanged);
experienceOnNPCDeath.Response.AddListener(level.GainExperience);
UpdateLevelOnOthers();
statPanel.UpdateStatValues();
}
private void Start()
{
if (photonView.IsMine)
onUpdateStatValues.Invoke();
}
private void OnExperienceChanged()
@ -235,5 +252,24 @@ namespace Kryz.CharacterStats.Examples
onUpdateStatValues.Invoke();
}
EquipmentSlot[] slots;
public void RefreshCurrentEquipmentStats()
{
slots = equipmentPanel.GetCurrentSlots();
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].Item == null) continue;
for (int j = 0; j < statsCollection.Count; j++)
{
if (statsCollection[j].HasModifiersFromSource(slots[i].Item))
{
break;
}
}
((EquippableItem)slots[i].Item).Equip(this);
}
}
}
}

View File

@ -7,7 +7,7 @@ using UnityEngine.Events;
public class Health : Resource
{
CharacterStats character;
protected CharacterStats character;
public UnityEvent<float> onMaxHealthChanged = new UnityEvent<float>();
public UnityEvent onDeath = new UnityEvent();
@ -15,7 +15,7 @@ public class Health : Resource
[HideInInspector]
public PhotonView photonView;
private void Awake()
protected virtual void Awake()
{
character = GetComponent<CharacterStats>();
photonView = GetComponent<PhotonView>();
@ -62,7 +62,7 @@ public class Health : Resource
return base.GetMaxValue();
}
public void CalculateMaxValueBasedOnStat()
public virtual void CalculateMaxValueBasedOnStat()
{
maxValue = baseMaxValue + character.Vitality.Value * 10f;

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 251d95c5c649ac648841385a5648c1a8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,23 @@
using Kryz.CharacterStats.Examples;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : Health
{
protected override void Awake()
{
base.Awake();
}
/*public override void CalculateMaxValueBasedOnStat()
{
maxValue = baseMaxValue + character.Vitality.Value * 10f;
maxValue *= ((((PlayerCharacterStats)character).level.currentLevel - 1) * 0.2f);
CalculateRegenValueBasedOnStat();
onMaxHealthChanged.Invoke(maxValue);
}*/
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e27fb8d81ab0d814ca4415089c513fe3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: