88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
public static class AswidFoodManager
|
|
{
|
|
// Runtime dictionary holding the 4th food slot per player
|
|
public static Dictionary<Player, Player.Food> ExtraFoods = new Dictionary<Player, Player.Food>();
|
|
|
|
// Called when player spawns to load the 4th food from custom data
|
|
public static void LoadFood(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
// If they are no longer an Aswid, clear it out
|
|
if (!AswidRace.IsAswid())
|
|
{
|
|
ClearFood(player);
|
|
return;
|
|
}
|
|
|
|
if (!player.m_customData.ContainsKey("AswidFood_Prefab")) return;
|
|
|
|
string prefabName = player.m_customData["AswidFood_Prefab"];
|
|
GameObject prefab = ObjectDB.instance.GetItemPrefab(prefabName);
|
|
if (prefab == null) return;
|
|
|
|
ItemDrop itemDrop = prefab.GetComponent<ItemDrop>();
|
|
if (itemDrop == null) return;
|
|
|
|
// Build food object
|
|
Player.Food food = new Player.Food();
|
|
food.m_item = itemDrop.m_itemData.Clone(); // Clone to be safe
|
|
|
|
// Try parse remaining time
|
|
if (player.m_customData.TryGetValue("AswidFood_Time", out string timeStr) && float.TryParse(timeStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out float time))
|
|
{
|
|
food.m_time = time;
|
|
}
|
|
|
|
food.m_health = itemDrop.m_itemData.m_shared.m_food;
|
|
food.m_stamina = itemDrop.m_itemData.m_shared.m_foodStamina;
|
|
food.m_eitr = itemDrop.m_itemData.m_shared.m_foodEitr;
|
|
|
|
ExtraFoods[player] = food;
|
|
}
|
|
|
|
// Called to save the current runtime 4th food to custom data for persistence
|
|
public static void SaveFood(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
if (ExtraFoods.TryGetValue(player, out Player.Food food) && food != null && food.m_item != null)
|
|
{
|
|
player.m_customData["AswidFood_Prefab"] = food.m_item.m_dropPrefab.name;
|
|
player.m_customData["AswidFood_Time"] = food.m_time.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
|
}
|
|
else
|
|
{
|
|
player.m_customData.Remove("AswidFood_Prefab");
|
|
player.m_customData.Remove("AswidFood_Time");
|
|
}
|
|
}
|
|
|
|
public static void ClearFood(Player player)
|
|
{
|
|
ExtraFoods.Remove(player);
|
|
if (player != null)
|
|
{
|
|
player.m_customData.Remove("AswidFood_Prefab");
|
|
player.m_customData.Remove("AswidFood_Time");
|
|
}
|
|
}
|
|
|
|
public static void SetExtraFood(Player player, ItemDrop.ItemData item)
|
|
{
|
|
Player.Food food = new Player.Food();
|
|
food.m_item = item.Clone();
|
|
food.m_time = 0f;
|
|
food.m_health = item.m_shared.m_food;
|
|
food.m_stamina = item.m_shared.m_foodStamina;
|
|
food.m_eitr = item.m_shared.m_foodEitr;
|
|
ExtraFoods[player] = food;
|
|
SaveFood(player);
|
|
}
|
|
}
|
|
}
|