using UnityEngine;
using UnityEngine.UI;
using W.o.N_Stats;
public class Character : MonoBehaviour
{
[Header("Stats")]
public CharacterStat Health;
public CharacterStat Mana;
public CharacterStat Defence;
public CharacterStat Attack;
public CharacterStat AttackSpeed;
public CharacterStat Critical;
public CharacterStat LifeSteal;
public CharacterStat Range;
public CharacterStat ElementalMastery;
public CharacterStat Speed;
public CharacterStat Cooldown;
public CharacterStat Knockback;
public CharacterStat Recovery;
public CharacterStat Evade;
public CharacterStat Fortune;
code
Assistant
To locate the base values of the character's stats in your script, you'll need to look at how the CharacterStat class is defined and where these base values are initialized or set. The instances like , , , etc., are public fields in your class, which likely means they hold their base values internally.
User
Assistant
Yes, the CharacterStat class you provided is indeed where the base values for your character's stats are defined and managed. Specifically, the BaseValue field holds the initial value of each stat.
Key Points:
BaseValue: This is the field where the base value of the stat is stored. It's the initial value before any modifiers (e.g., from equipment or buffs) are applied.
Value Computation:
The Value property is where the final stat value is computed. It takes into account the BaseValue as well as any modifiers added via the AddModifier method.
The final computed value (_value) is cached until the BaseValue changes or a modifier is added/removed, as indicated by the flag.
User
Assistant
public int maxHealth;
public int maxMana;
public int maxDefense;
public int maxAttack;
public float maxAttackSpeed;
public int maxCritical;
public float maxLifeSteal;
public float maxRange;
public float maxElementalMastery;
public float maxSpeed;
public float maxCooldown;
public float maxKnockback;
public float maxRecovery;
public float maxEvade;
public float maxFortune;
[Header("Public")]
public Inventory Inventory;
public EquipmentPanel EquipmentPanel;
public Player player; // Reference to the Player script
private WeaponTypeDetector weaponTypeDetector;
[Header("Selected Seed")]
public SeedItem selectedSeed; // Reference to the selected seed item
public Color normalColor = Color.white;
public Color selectedColor = new Color(0.5f, 0.5f, 0.5f, 1f);
private static Character instance; // Reference to the character instance
[Header("Serialize Field")]
[SerializeField] CraftingWindow craftingWindow;
[SerializeField] StatPanel statPanel;
[SerializeField] ItemTooltip itemTooltip;
[SerializeField] Image draggableItem;
[SerializeField] DropItemArea dropItemArea;
[SerializeField] QuestionDialog reallyDropItemDialog;
private BaseItemSlot dragItemSlot;
private void OnValidate()
{
if (itemTooltip == null)
itemTooltip = FindObjectOfType<ItemTooltip>();
}
public void Awake()
{
statPanel.SetStats(Health, Mana, Defence, Attack, AttackSpeed, Critical, LifeSteal, Range, ElementalMastery, Speed, Cooldown, Knockback, Recovery, Evade, Fortune);
instance = this; // Assign the current instance to the static variable
statPanel.UpdateStatValues();
// Get the WeaponTypeDetector component
weaponTypeDetector = GetComponent<WeaponTypeDetector>();
// Setup Events:
// Right Click
Inventory.OnRightClickEvent += InventoryRightClick;
EquipmentPanel.OnRightClickEvent += EquipmentPanelRightClick;
// Pointer Enter
Inventory.OnPointerEnterEvent += ShowTooltip;
EquipmentPanel.OnPointerEnterEvent += ShowTooltip;
craftingWindow.OnPointerEnterEvent += ShowTooltip;
// Pointer Exit
Inventory.OnPointerExitEvent += HideTooltip;
EquipmentPanel.OnPointerExitEvent += HideTooltip;
craftingWindow.OnPointerExitEvent += HideTooltip;
// Begin Drag
Inventory.OnBeginDragEvent += BeginDrag;
EquipmentPanel.OnBeginDragEvent += BeginDrag;
// End Drag
Inventory.OnEndDragEvent += EndDrag;
EquipmentPanel.OnEndDragEvent += EndDrag;
// Drag
Inventory.OnDragEvent += Drag;
EquipmentPanel.OnDragEvent += Drag;
// Drop
Inventory.OnDropEvent += Drop;
EquipmentPanel.OnDropEvent += Drop;
dropItemArea.OnDropEvent += DropItemOutsideUI;
}
public static Character GetInstance()
{
if (instance == null)
{
instance = FindObjectOfType<Character>(); // Find the Character instance in the scene
}
return instance;
}
private void Update()
{
// Change the following line to continuously update stats in real-time
UpdateStatValues();
}
private void InventoryRightClick(BaseItemSlot itemSlot)
{
// Deselect the currently selected seed if the clicked item is not a seed item
if (!(itemSlot.Item is SeedItem))
{
DeselectSeed();
}
// Handle the action for the clicked item
if (itemSlot.Item is EquippableItem)
{
Equip((EquippableItem)itemSlot.Item);
}
else if (itemSlot.Item is UsableItem)
{
// Use the Usable Item
UsableItem usableItem = (UsableItem)itemSlot.Item;
usableItem.Use(player); // Assuming 'player' is a reference to the Player script
Inventory.RemoveItem(usableItem); // Remove the used item from the inventory
}
else if (itemSlot.Item is SeedItem)
{
SeedItem seedItem = (SeedItem)itemSlot.Item;
// If the clicked seed is the same as the currently selected seed, deselect it
if (selectedSeed == seedItem)
{
DeselectSeed();
}
else // If the clicked seed is different from the currently selected one, select it
{
// Check if there's a previously selected seed, and deselect it
if (selectedSeed != null)
{
DeselectSeed();
}
selectedSeed = seedItem;
Debug.Log("Selected seed: " + selectedSeed.name);
UpdateSeedTintColors();
}
}
UpdateMax();
AdjustStatsIfNeeded();
}
private void DeselectSeed()
{
if (selectedSeed != null)
{
Debug.Log("Deselected seed: " + selectedSeed.name);
selectedSeed = null;
UpdateSeedTintColors();
}
}
private void UpdateSeedTintColors()
{
// Update tint colors of seed items based on selection state
foreach (BaseItemSlot itemSlot in Inventory.ItemSlots)
{
if (itemSlot.Item is SeedItem seedItem)
{
Image seedImage = itemSlot.GetComponent<Image>();
if (seedItem == selectedSeed)
{
seedImage.color = selectedColor;
}
else
{
seedImage.color = normalColor;
}
}
}
}
private void EquipmentPanelRightClick(BaseItemSlot itemSlot)
{
if (itemSlot.Item is EquippableItem)
{
Unequip((EquippableItem)itemSlot.Item);
}
UpdateMax();
}
private void ShowTooltip(BaseItemSlot itemSlot)
{
if (itemSlot.Item != null)
{
itemTooltip.ShowTooltip(itemSlot.Item);
}
}
private void HideTooltip(BaseItemSlot itemSlot)
{
if (itemTooltip.gameObject.activeSelf)
{
itemTooltip.HideTooltip();
}
}
private void BeginDrag(BaseItemSlot itemSlot)
{
if (itemSlot.Item != null && !(itemSlot is EquipmentSlot))
{
dragItemSlot = itemSlot;
draggableItem.sprite = itemSlot.Item.Icon;
draggableItem.transform.position = Input.mousePosition;
draggableItem.gameObject.SetActive(true);
}
}
private void Drag(BaseItemSlot itemSlot)
{
if (dragItemSlot != null)
{
draggableItem.transform.position = Input.mousePosition;
}
}
private void EndDrag(BaseItemSlot itemSlot)
{
dragItemSlot = null;
draggableItem.gameObject.SetActive(false);
}
private void Drop(BaseItemSlot dropItemSlot)
{
if (dragItemSlot == null) return;
code
// Case 1: Prevent dropping equippable items into equipment slots if the item is already equipped
if (dragItemSlot.Item is EquippableItem && dropItemSlot is EquipmentSlot)
{
EndDrag(null);
return;
}
// Case 2: Try to stack the item if dropItemSlot can accept stacks
if (dropItemSlot.CanAddStack(dragItemSlot.Item))
{
AddStacks(dropItemSlot);
}
// Case 3: Swap items if both slots can receive each other's items
else if (dropItemSlot.CanReceiveItem(dragItemSlot.Item) && dragItemSlot.CanReceiveItem(dropItemSlot.Item))
{
SwapItems(dropItemSlot, dragItemSlot);
}
// Case 4: Move item to dropItemSlot if it’s a valid drop
else if (dropItemSlot.CanReceiveItem(dragItemSlot.Item))
{
MoveItemToSlot(dropItemSlot, dragItemSlot);
RemoveItemFromSlot(dragItemSlot);
}
EndDrag(null);
private void AddStacks(BaseItemSlot dropItemSlot)
{
int numAddableStacks = dropItemSlot.Item.MaximumStacks - dropItemSlot.Amount;
int stacksToAdd = Mathf.Min(numAddableStacks, dragItemSlot.Amount);
dropItemSlot.Amount += stacksToAdd;
dragItemSlot.Amount -= stacksToAdd;
}
private void SwapItems(BaseItemSlot dropItemSlot, BaseItemSlot originalSlot)
{
Item draggedItem = originalSlot.Item;
int draggedItemAmount = originalSlot.Amount;
originalSlot.Item = dropItemSlot.Item;
originalSlot.Amount = dropItemSlot.Amount;
dropItemSlot.Item = draggedItem;
dropItemSlot.Amount = draggedItemAmount;
if (originalSlot is EquipmentSlot)
{
((EquippableItem)originalSlot.Item)?.Equip(this);
}
if (dropItemSlot is EquipmentSlot)
{
((EquippableItem)dropItemSlot.Item)?.Equip(this);
}
statPanel.UpdateStatValues();
}
private void DropItemOutsideUI()
{
if (dragItemSlot == null) return;
code
if (dragItemSlot.Item != null)
{
// Check if the dialog should be shown or bypassed
if (reallyDropItemDialog.ShouldAskAgain())
{
// Show the confirmation dialog
reallyDropItemDialog.Show();
BaseItemSlot slot = dragItemSlot;
reallyDropItemDialog.OnYesEvent += () => DestroyItemInSlot(slot);
}
else
{
// Directly destroy the item if the user opted not to be asked again
DestroyItemInSlot(dragItemSlot);
}
}
}
code
private void DestroyItemInSlot(BaseItemSlot itemSlot)
{
// If the item is equipped, unequip first
if (itemSlot is EquipmentSlot)
{
EquippableItem equippableItem = (EquippableItem)itemSlot.Item;
equippableItem.Unequip(this);
AdjustStatsIfNeeded();
UpdateStatValues();
}
itemSlot.Item.Destroy();
itemSlot.Item = null;
}
public void Equip(EquippableItem item)
{
if (Inventory.RemoveItem(item))
{
EquippableItem previousItem;
if (EquipmentPanel.AddItem(item, out previousItem))
{
if (previousItem != null)
{
Inventory.AddItem(previousItem);
previousItem.Unequip(this);
}
item.Equip(this);
}
else
{
Inventory.AddItem(item);
}
// Update stats immediately after equipping
UpdateStatValues();
AdjustStatsIfNeeded();
// Update weapon type
weaponTypeDetector.ManualUpdateWeaponType();
}
}
public void Unequip(EquippableItem item)
{
if (Inventory.CanAddItem(item) && EquipmentPanel.RemoveItem(item))
{
item.Unequip(this);
// Update stats immediately after unequipping
UpdateStatValues();
Inventory.AddItem(item);
// Reset current stats to their maximum values
AdjustStatsIfNeeded();
// Update weapon type
weaponTypeDetector.ManualUpdateWeaponType();
}
}
// Method to call SetCurrentStatsToMax from the Player script
public void AdjustStatsIfNeeded()
{
player.AdjustStatsIfNeeded();
}
public void UpdateMax()
{
player.UpdateMaxStats();
}
private ItemContainer openItemContainer;
public void OpenChest(Chest chest)
{
OpenItemContainer(chest);
}
public void CloseChest(Chest chest)
{
CloseItemContainer(chest);
}
Here's a step-by-step guide on how to find and understand the base values for these stats:
Check the CharacterStat Class:
Since CharacterStat is a custom class, you should start by checking its implementation. Look for fields or properties that represent the base value, typically named as BaseValue or similar.
Initialization:
Examine the Awake, Start, or OnEnable methods in your Character class to see if the base values are initialized there.
Check if these values are initialized in the Unity Editor. If the CharacterStat is a Serializable class, the base values might be editable in the Inspector for your Character component.
External Data Loading:
In your script, you have methods like LoadStatsFromData, which suggests that character stats might be loaded from an external data source (e.g., saved files or default settings). Investigate where PlayerData is defined and initialized.
Inspector Values:
If CharacterStat is designed to be modifiable via the Unity Editor, check if the Character GameObject has any default values set through the Inspector view.
Default Constructor Values:
If the CharacterStat class has a constructor, check it to see if it initializes any default base values.
Here’s a general example of what a CharacterStat class might look like:
csharp
[System.Serializable]
public class CharacterStat
{
public float BaseValue; // This might be your base value field
// Other properties and methods...
public float Value
{
get
{
// Calculate and return the current stat value, perhaps considering buffs, debuffs, etc.
return BaseValue; // This is simplified
}
}
}
Ultimately, you'll need access to all relevant scripts to fully ascertain where and how the base values are set and used. If these values aren't directly visible in your script, they might be managed within the Unity Editor or another script component.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace W.o.N_Stats
{
[Serializable]
public class CharacterStat
{
public float BaseValue;
code
protected bool isDirty = true;
protected float lastBaseValue;
protected float _value;
public virtual float Value {
get {
if(isDirty || lastBaseValue != BaseValue) {
lastBaseValue = BaseValue;
_value = CalculateFinalValue();
isDirty = false;
}
return _value;
}
}
protected readonly List<StatModifier> statModifiers;
public readonly ReadOnlyCollection<StatModifier> StatModifiers;
public CharacterStat()
{
statModifiers = new List<StatModifier>();
StatModifiers = statModifiers.AsReadOnly();
}
public CharacterStat(float baseValue) : this()
{
BaseValue = baseValue;
}
public virtual void AddModifier(StatModifier mod)
{
isDirty = true;
statModifiers.Add(mod);
statModifiers.Sort(CompareModifierOrder);
}
public virtual bool RemoveModifier(StatModifier mod)
{
if (statModifiers.Remove(mod))
{
isDirty = true;
return true;
}
return false;
}
public virtual bool RemoveAllModifiersFromSource(object source)
{
bool didRemove = false;
for (int i = statModifiers.Count - 1; i >= 0; i--)
{
if (statModifiers[i].Source == source)
{
isDirty = true;
didRemove = true;
statModifiers.RemoveAt(i);
}
}
return didRemove;
}
protected virtual int CompareModifierOrder(StatModifier a, StatModifier b)
{
if (a.Order < b.Order)
return -1;
else if (a.Order > b.Order)
return 1;
return 0; //if (a.Order == b.Order)
}
protected virtual float CalculateFinalValue()
{
float finalValue = BaseValue;
float sumPercentAdd = 0;
for (int i = 0; i < statModifiers.Count; i++)
{
StatModifier mod = statModifiers[i];
if (mod.Type == StatModType.Flat)
{
finalValue += mod.Value;
}
else if (mod.Type == StatModType.PercentAdd)
{
sumPercentAdd += mod.Value;
if (i + 1 >= statModifiers.Count || statModifiers[i + 1].Type != StatModType.PercentAdd)
{
finalValue *= 1 + sumPercentAdd;
sumPercentAdd = 0;
}
}
else if (mod.Type == StatModType.PercentMult)
{
finalValue *= 1 + mod.Value;
}
}
// Round to the nearest whole number
return (float)Math.Round(finalValue);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CharacterStat Class
//This class is designed to manage and calculate the final value of a character's stat based on various modifiers. Here's a detailed explanation of each part:
//Fields and Properties:
//BaseValue: The initial value of the stat.
//isDirty: A flag to indicate if the final value needs to be recalculated.
//lastBaseValue: Stores the last base value to check if recalculation is needed.
//_value: The cached final value of the stat.
//Value: A property that returns the final value, recalculating it if necessary.
//Modifier List:
//statModifiers: A list of all stat modifiers applied to this stat.
//StatModifiers: A read-only collection of the stat modifiers for external access.
//Constructors:
//Default constructor initializes the modifier list.
//Overloaded constructor allows setting the base value during initialization.
//Methods:
//AddModifier(StatModifier mod): Adds a modifier and marks the stat as dirty for recalculation.
//RemoveModifier(StatModifier mod): Removes a modifier, marks the stat as dirty, and returns if the removal was successful.
//RemoveAllModifiersFromSource(object source): Removes all modifiers from a given source and returns if any were removed.
//CompareModifierOrder(StatModifier a, StatModifier b): Compares modifiers based on their order for sorting.
//CalculateFinalValue(): Calculates the final value of the stat based on all modifiers.
//Calculation Logic:
//Flat modifiers are added directly.
//PercentAdd modifiers are accumulated and applied together.
//PercentMult modifiers are applied multiplicatively.
//The final value is rounded to the nearest whole number.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
is this where i cna get characters base values?
isDirty
Modifiers:
Modifiers can be applied to alter the final value in various ways, such as through flat additions (StatModType.Flat), percentage additions (StatModType.PercentAdd), or percentage multipliers (StatModType.PercentMult).
Constructor Usage:
You can initialize an instance of CharacterStat with a specific base value using the overloaded constructor: new CharacterStat(float baseValue).
If you want to access or modify the base values for each stat in your Character class, you can directly access them through their respective CharacterStat objects, like Health.BaseValue, Mana.BaseValue, and so on. The LoadStatsFromData method in your Character script also demonstrates how you can set these base values programmatically using saved data.
using UnityEngine;
using UnityEngine.UI;
using W.o.N_Stats;
using System;
using System.Collections;
using UnityEngine.EventSystems;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
code
public class Player : MonoBehaviour
{
#region Stats Name And Player Info
// Property to set or get the player name
public string PlayerName
{
get { return PlayerPrefs.GetString("PlayerName", "Default Name"); }
set { PlayerPrefs.SetString("PlayerName", value); }
}
[Header("Level and Experience")]
public int PlayerLevel;
public int CurrentEXP;
public int ExpToLevelUp;
[Header("Health")]
public int currentHealth;
public int maxHealth;
[Header("Mana")]
public int currentMana;
public int maxMana;
[Header("Defense")]
public int currentDefense;
public int maxDefense;
[Header("Attack")]
public int currentAttack;
public int maxAttack;
[Header("Attack Speed")]
public float currentAttackSpeed;
public float maxAttackSpeed;
[Header("Critical")]
public int currentCritical;
public int maxCritical;
[Header("Life Steal")]
public float currentLifeSteal;
public float maxLifeSteal;
[Header("Range")]
public float currentRange;
public float maxRange;
[Header("Elemental Mastery")]
public float currentElementalMastery;
public float maxElementalMastery;
[Header("Cooldown")]
public float currentCooldown;
public float maxCooldown;
[Header("Knockback")]
public float currentKnockback;
public float maxKnockback;
[Header("Speed")]
public float currentSpeed;
public float maxSpeed;
[Header("Recovery")]
public float currentRecovery;
public float maxRecovery;
[Header("Evade")]
public float currentEvade;
public float maxEvade;
[Header("Fortune")]
public float currentFortune;
public float maxFortune;
[Header("Character and Stat Panel")]
public Character character;
public StatPanel statPanel;
public PlayerSkill playerSkill;
public Inventory inventory;
public EquipmentPanel equipmentPanel;
[Header("Hurt Effect")]
[SerializeField] private GameObject hurtParticlePrefab;
[SerializeField]
private PlayerDamageIndicator damageIndicatorScript; // Reference to the PlayerDamageIndicator script
public PlayerLevelUpText levelUpTextScript; // Reference to the PlayerLevelUpText script
#endregion
private float lastDamageTime = 0f; // Track the time of the last damage taken or mana used
private float lastRecoveryTime = 0f;
private bool hasEquippedItemRecently = false;
private float timeSinceEquippingItem = 0f;
private float equipItemRecoveryDelay = 30f;
[Header("Player Camera")]
public Camera playerCamera; // Assign the player's camera directly in the inspector
[Header("Button Assignment")]
public float attackRange = 2f;
public LayerMask mobsLayer; // Specify the layer for "Mobs"
[SerializeField] private bool displayGizmo = true; // Toggle to display Gizmo
// Event to notify UI that stats have changed
public Action OnStatsChanged { get; set; }
private void OnDrawGizmos()
{
if (displayGizmo)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, attackRange);
}
}
// Method to update the player's name
public void UpdatePlayerName(string newName)
{
PlayerName = newName;
}
#region Take Damage & Respawn
code
public void TakeDamage(int damage)
{
// Spawn the hurt particle effect if the prefab is assigned
if (hurtParticlePrefab != null)
{
GameObject hurtParticle = Instantiate(hurtParticlePrefab, transform.position, Quaternion.identity);
Destroy(hurtParticle, 0.45f); // Destroy the particle effect after 0.45 seconds
}
// Call the ShowDamageText method from PlayerDamageIndicator script and pass the damage value
if (damageIndicatorScript != null)
{
damageIndicatorScript.ShowDamageText(damage);
}
// Check if the player can evade
if (UnityEngine.Random.Range(0f, 100f) <= currentEvade)
{
Debug.Log($"{PlayerName} Evaded the Attack & Avoided {damage} Damage!");
}
else
{
// Calculate actual damage after considering defense
int actualDamage = Mathf.Max(0, damage - currentDefense);
// Damage defense first if there's any
if (currentDefense > 0)
{
int defenseDamage = Mathf.Max(0, damage - actualDamage);
currentDefense = Mathf.Max(0, currentDefense - damage);
// Log damage taken to defense if defense is greater than 0
if (defenseDamage > 0)
{
Debug.Log($"{PlayerName} Defense took {defenseDamage} damage!");
}
}
// If defense is reduced to 0, or no defense, damage health
if (currentDefense == 0)
{
int healthDamage = actualDamage;
currentHealth -= healthDamage;
// Log the health damage
Debug.Log($"{PlayerName} took {healthDamage} damage to health!");
// Check if health reaches 0
if (currentHealth <= 0)
{
Debug.Log($"{PlayerName} Died!");
// Respawn the player at the saved location and restore all stats to full
LoadPlayerData(PlayerName); // Respawn the player using saved data
RespawnPlayer();
}
}
}
// Update the last damage time to trigger recovery
lastDamageTime = Time.time;
}
private void RespawnPlayer()
{
// Restore all stats to full
SetCurrentStatsToMax(); // Reset current stats to their maximum values
// Teleport the player back to the saved location using the PlayerSaveLocation script
PlayerSaveLocation saveLocationScript = GetComponent<PlayerSaveLocation>();
if (saveLocationScript != null)
{
saveLocationScript.TeleportToSavedLocation();
}
}
#endregion
#region Start And Update
void Start()
{
// Construct the base directory path
code
SetStartingStats(); // Call this method to set starting stats
InitializeStats();
SetCurrentStatsToMax();
// Set the initial values for level and experience
PlayerLevel = 1;
CurrentEXP = 0;
ExpToLevelUp = 250;
string baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "World Of Normies", "SAVE DATA", PlayerName);
// Check if the player's directory exists
if (Directory.Exists(baseDirectory))
{
// Construct the full file path for the player's data file
string filePath = Path.Combine(baseDirectory, $"{PlayerName}_Data.sav");
// Check if the player's data file exists
if (File.Exists(filePath))
{
// Load player data from file
LoadPlayerData(PlayerName);
}
}
}
private void Update()
{
// This Bellow When An Item Is Equipped It Will Update The Stats on Both The UI And The Player
UpdateMaxStats();
// Check if enough time has passed since the last damage or mana/defense depletion
if (!hasEquippedItemRecently && Time.time - lastDamageTime >= 30f)
{
// Increment current health, mana, and defense towards their maximum values over time
if (Time.time - lastRecoveryTime >= 5f) // Check if 5 seconds have passed since last recovery
{
// Increment stats by maxRecovery amount
currentHealth = Mathf.Min(maxHealth, currentHealth + Mathf.RoundToInt(maxRecovery));
currentMana = Mathf.Min(maxMana, currentMana + Mathf.RoundToInt(maxRecovery));
currentDefense = Mathf.Min(maxDefense, currentDefense + Mathf.RoundToInt(maxRecovery));
// Update the last recovery time
lastRecoveryTime = Time.time;
}
}
// Update the time since equipping an item
if (hasEquippedItemRecently)
{
timeSinceEquippingItem += Time.deltaTime;
if (timeSinceEquippingItem >= equipItemRecoveryDelay)
{
// Enough time has passed after equipping an item, reset the flag and trigger recovery
hasEquippedItemRecently = false;
lastDamageTime = Time.time;
}
}
}
#endregion
#region Level Up
public void LevelUp()
{
// Call the LevelUpText method from PlayerLevelUpText script
if (levelUpTextScript != null)
{
levelUpTextScript.ShowLevelUpText();
}
code
#region Save And Load System
code
public void SavePlayerData()
{
// Create a PlayerData object and populate it with current player state
PlayerData data = new PlayerData();
data.playerName = PlayerName;
data.playerLevel = PlayerLevel;
data.currentEXP = CurrentEXP;
data.expToLevelUp = ExpToLevelUp;
data.currentHealth = currentHealth;
data.maxHealth = maxHealth;
data.currentMana = currentMana;
data.maxMana = maxMana;
data.currentDefense = currentDefense;
data.maxDefense = maxDefense;
data.currentAttack = currentAttack;
data.maxAttack = maxAttack;
data.currentAttackSpeed = currentAttackSpeed;
data.maxAttackSpeed = maxAttackSpeed;
data.currentCritical = currentCritical;
data.maxCritical = maxCritical;
data.currentLifeSteal = currentLifeSteal;
data.maxLifeSteal = maxLifeSteal;
data.currentRange = currentRange;
data.maxRange = maxRange;
data.currentElementalMastery = currentElementalMastery;
data.maxElementalMastery = maxElementalMastery;
data.currentSpeed = currentSpeed;
data.maxSpeed = maxSpeed;
data.currentCooldown = currentCooldown;
data.maxCooldown = maxCooldown;
data.currentKnockback = currentKnockback;
data.maxKnockback = maxKnockback;
data.currentRecovery = currentRecovery;
data.maxRecovery = maxRecovery;
data.currentEvade = currentEvade;
data.maxEvade = maxEvade;
data.currentFortune = currentFortune;
data.maxFortune = maxFortune;
data.playerPosX = transform.position.x;
data.playerPosY = transform.position.y;
data.playerPosZ = transform.position.z;
// Adjusted file path
string saveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "World Of Normies", "SAVE DATA", PlayerName);
string fileName = $"{PlayerName}_Data.sav"; // Adjusted file name here
string savePath = Path.Combine(saveDirectory, fileName);
try
{
// Create directory if it doesn't exist
Directory.CreateDirectory(saveDirectory);
using (FileStream fileStream = new FileStream(savePath, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, data);
}
Debug.Log($"<color=#ADD8E6><b>Player data saved successfully</b></color> to: {savePath}");
}
catch (Exception e)
{
Debug.LogError($"<color=red><b>Failed to save player data: {e.Message}</b></color>");
}
}
public void LoadPlayerData(string playerName)
{
// Construct the base directory path
string baseDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "World Of Normies", "SAVE DATA");
code
#endregion
}
when i save i need to save characters base values
and when i load i need to set characters base values to = players max values
To ensure that the character's base values are saved and loaded correctly, follow these steps to modify your saving and loading logic accordingly:
1. Update the PlayerData Class
The PlayerData class needs to accommodate storing the character's base values. Add fields for these values in your PlayerData class. Here's an example of additional fields you might add:
csharp
[Serializable]
public class PlayerData
{
public string playerName;
public int playerLevel;
public int currentEXP;
public int expToLevelUp;
public int currentHealth;
public int maxHealth;
public int currentMana;
public int maxMana;
public int currentDefense;
public int maxDefense;
public int currentAttack;
public int maxAttack;
public float currentAttackSpeed;
public float maxAttackSpeed;
public int currentCritical;
public int maxCritical;
public float currentLifeSteal;
public float maxLifeSteal;
public float currentRange;
public float maxRange;
public float currentElementalMastery;
public float maxElementalMastery;
public float currentSpeed;
public float maxSpeed;
public float currentCooldown;
public float maxCooldown;
public float currentKnockback;
public float maxKnockback;
public float currentRecovery;
public float maxRecovery;
public float currentEvade;
public float maxEvade;
public float currentFortune;
public float maxFortune;
// Add fields for the character's base values
public float healthBaseValue;
public float manaBaseValue;
public float defenseBaseValue;
public float attackBaseValue;
public float attackSpeedBaseValue;
public float criticalBaseValue;
public float lifeStealBaseValue;
public float rangeBaseValue;
public float elementalMasteryBaseValue;
public float speedBaseValue;
public float cooldownBaseValue;
public float knockbackBaseValue;
public float recoveryBaseValue;
public float evadeBaseValue;
public float fortuneBaseValue;
// Add fields to store position here as needed
public float playerPosX;
public float playerPosY;
public float playerPosZ;
}
2. Update Saving Logic
When saving, you'll want to ensure the PlayerData instance includes the character's current base values. Update the SavePlayerData method like this:
When loading, you'll want to set the character's base values to the player's max values stored. Update the LoadPlayerData method:
csharp
By following these steps, your save and load logic will handle the character's base values, ensuring they're properly managed between game sessions. This setup ensures consistency between the player's state and the character's attributes each time the game is loaded.
Not yet included in a published release.
PlayerLevel++;
CurrentEXP -= ExpToLevelUp;
ExpToLevelUp *= 2;
// Update character stats
character.Health.BaseValue += 5 * PlayerLevel;
character.Mana.BaseValue += 2 * PlayerLevel;
character.Defence.BaseValue += 1 * PlayerLevel;
character.Attack.BaseValue += 1 * PlayerLevel;
character.AttackSpeed.BaseValue += 0.008f * PlayerLevel;
character.Critical.BaseValue += 0.05F * PlayerLevel;
character.LifeSteal.BaseValue += 0.5f * PlayerLevel;
character.Range.BaseValue += 0.015f * PlayerLevel;
character.ElementalMastery.BaseValue += 0.13f * PlayerLevel;
character.Cooldown.BaseValue += 0.1f * PlayerLevel;
character.Knockback.BaseValue += 0.025f * PlayerLevel;
character.Speed.BaseValue += 0.005f * PlayerLevel;
character.Recovery.BaseValue += 0.5f * PlayerLevel;
character.Evade.BaseValue += 0.01f * PlayerLevel;
character.Fortune.BaseValue += 0.015f * PlayerLevel;
// Update stat values
character.UpdateStatValues();
// Update max stats
UpdateMaxStats();
// Set current stats to max
SetCurrentStatsToMax();
// Show updated stats on UI
statPanel.SetStats(character.Health, character.Mana, character.Defence, character.Attack , character.AttackSpeed, character.Critical, character.LifeSteal, character.Range, character.ElementalMastery, character.Speed, character.Cooldown, character.Knockback, character.Recovery, character.Evade, character.Fortune);
}
#endregion
private void SetStartingStats()
{
// Set starting stats for the player based on character stats
currentHealth = (int)character.Health.Value;
maxHealth = (int)character.Health.Value;
currentMana = (int)character.Mana.Value;
maxMana = (int)character.Mana.Value;
currentDefense = (int)character.Defence.Value;
maxDefense = (int)character.Defence.Value;
currentAttack = (int)character.Attack.Value;
maxAttack = (int)character.Attack.Value;
currentAttackSpeed = character.AttackSpeed.Value;
maxAttackSpeed = character.AttackSpeed.Value;
currentCritical = (int)character.Critical.Value;
maxCritical = (int)character.Critical.Value;
currentLifeSteal = character.LifeSteal.Value;
maxLifeSteal = character.LifeSteal.Value;
currentRange = character.Range.Value;
maxRange = character.Range.Value;
currentElementalMastery = character.ElementalMastery.Value;
maxElementalMastery = character.ElementalMastery.Value;
currentSpeed = character.Speed.Value;
maxSpeed = character.Speed.Value;
currentCooldown = character.Cooldown.Value;
maxCooldown = character.Cooldown.Value;
currentKnockback = character.Knockback.Value;
maxKnockback = character.Knockback.Value;
currentRecovery = character.Recovery.Value;
maxRecovery = character.Recovery.Value;
currentEvade = character.Evade.Value;
maxEvade = character.Evade.Value;
currentFortune = character.Fortune.Value;
maxFortune = character.Fortune.Value;
// Call UpdateStatValues to apply the changes
character.UpdateStatValues();
// Update max and current stats
UpdateMaxStats();
SetCurrentStatsToMax();
}
// Update the maximum stats of the character
public void UpdateMaxStats()
{
// Update max stats for the player based on character stats
maxHealth = (int)character.Health.Value;
maxMana = (int)character.Mana.Value;
maxDefense = (int)character.Defence.Value;
maxAttack = (int)character.Attack.Value;
maxAttackSpeed = character.AttackSpeed.Value;
maxCritical = (int)character.Critical.Value;
maxLifeSteal = character.LifeSteal.Value;
maxRange = character.Range.Value;
maxElementalMastery = character.ElementalMastery.Value;
maxSpeed = character.Speed.Value;
maxCooldown = character.Cooldown.Value;
maxKnockback = character.Knockback.Value;
maxRecovery = character.Recovery.Value;
maxEvade = character.Evade.Value;
maxFortune = character.Fortune.Value;
}
// Check if any current stats have changed due to equipping items
public void CurrentStatsChange()
{
// Mark that an item has been equipped recently
hasEquippedItemRecently = true;
timeSinceEquippingItem = 0f;
}
public void InitializeStats()
{
// Update max stats
UpdateMaxStats();
// Initialize current stats
maxHealth = Mathf.RoundToInt(character.Health.Value);
currentHealth = maxHealth;
maxMana = Mathf.RoundToInt(character.Mana.Value);
currentMana = maxMana;
maxDefense = Mathf.RoundToInt(character.Defence.Value);
currentDefense = maxDefense;
maxAttack = Mathf.RoundToInt(character.Attack.Value);
currentAttack = maxAttack;
maxAttackSpeed = character.AttackSpeed.Value;
currentAttackSpeed = maxAttackSpeed;
maxCritical = Mathf.RoundToInt(character.Critical.Value);
currentCritical = maxCritical;
maxLifeSteal = Mathf.RoundToInt(character.LifeSteal.Value);
currentLifeSteal = maxLifeSteal;
maxRange = Mathf.RoundToInt(character.Range.Value);
currentRange = maxRange;
maxElementalMastery = Mathf.RoundToInt(character.ElementalMastery.Value);
currentElementalMastery = maxElementalMastery;
maxSpeed = Mathf.RoundToInt(character.Speed.Value);
currentSpeed = maxSpeed;
maxCooldown = character.Cooldown.Value;
currentCooldown = maxCooldown;
maxKnockback = Mathf.RoundToInt(character.Knockback.Value);
currentKnockback = maxKnockback;
maxRecovery = character.Recovery.Value;
currentRecovery = maxRecovery;
maxEvade = character.Evade.Value;
currentEvade = maxEvade;
maxFortune = Mathf.RoundToInt(character.Fortune.Value);
currentFortune = maxFortune;
// Replace UpdateCurrentStats with the appropriate method
// (e.g., SetCurrentStatsToMax or any other method that updates your current stats)
SetCurrentStatsToMax();
}
public void SetCurrentStatsToMax()
{
currentHealth = maxHealth;
currentMana = maxMana;
currentDefense = maxDefense;
currentAttack = maxAttack;
currentAttackSpeed = maxAttackSpeed;
currentCritical = maxCritical;
currentLifeSteal = maxLifeSteal;
currentRange = maxRange;
currentElementalMastery = maxElementalMastery;
currentCooldown = maxCooldown;
currentKnockback = maxKnockback;
currentSpeed = maxSpeed;
currentRecovery = maxRecovery;
currentEvade = maxEvade;
currentFortune = maxFortune;
// Reset the last recovery time to prevent immediate recovery
lastRecoveryTime = Time.time;
}
public void AdjustStatsIfNeeded()
{
// Health
int newMaxHealth = Mathf.RoundToInt(character.Health.Value);
if (newMaxHealth < currentHealth)
{
currentHealth = newMaxHealth;
}
// Mana
int newMaxMana = Mathf.RoundToInt(character.Mana.Value);
if (newMaxMana < currentMana)
{
currentMana = newMaxMana;
}
// Defense
int newMaxDefense = Mathf.RoundToInt(character.Defence.Value);
if (newMaxDefense < currentDefense)
{
currentDefense = newMaxDefense;
}
// Attack
int newMaxAttack = Mathf.RoundToInt(character.Attack.Value);
if (newMaxAttack < currentAttack)
{
currentAttack = newMaxAttack;
}
// Attack Speed
float newMaxAttackSpeed = character.AttackSpeed.Value;
if (newMaxAttackSpeed < currentAttackSpeed)
{
currentAttackSpeed = newMaxAttackSpeed;
}
// Critical
int newMaxCritical = Mathf.RoundToInt(character.Critical.Value);
if (newMaxCritical < currentCritical)
{
currentCritical = newMaxCritical;
}
// LifeSteal
int newMaxLifeSteal = Mathf.RoundToInt(character.LifeSteal.Value);
if (newMaxLifeSteal < currentLifeSteal)
{
currentLifeSteal = newMaxLifeSteal;
}
// Range
int newMaxRange = Mathf.RoundToInt(character.Range.Value);
if (newMaxRange < currentRange)
{
currentRange = newMaxRange;
}
// ElementalMastery
int newMaxElementalMastery = Mathf.RoundToInt(character.ElementalMastery.Value);
if (newMaxElementalMastery < currentElementalMastery)
{
currentElementalMastery = newMaxElementalMastery;
}
// Speed
float newMaxSpeed = character.Speed.Value;
if (newMaxSpeed < currentSpeed)
{
currentSpeed = newMaxSpeed;
}
// Cooldown
float newMaxCooldown = character.Cooldown.Value;
if (newMaxCooldown < currentCooldown)
{
currentCooldown = newMaxCooldown;
}
// Knockback
int newMaxKnockback = Mathf.RoundToInt(character.Knockback.Value);
if (newMaxKnockback < currentKnockback)
{
currentKnockback = newMaxKnockback;
}
// Recovery
float newMaxRecovery = character.Recovery.Value;
if (newMaxRecovery < currentRecovery)
{
currentRecovery = newMaxRecovery;
}
// Evade
float newMaxEvade = character.Evade.Value;
if (newMaxEvade < currentEvade)
{
currentEvade = newMaxEvade;
}
// Fortune
int newMaxFortune = Mathf.RoundToInt(character.Fortune.Value);
if (newMaxFortune < currentFortune)
{
currentFortune = newMaxFortune;
}
CurrentStatsChange();
}
public void IncreaseExperience(int amount)
{
CurrentEXP += amount;
while (CurrentEXP >= ExpToLevelUp)
{
LevelUp();
}
}
public void AddStats(int healthToAdd, int defenseToAdd, int manaToAdd)
{
// Check if adding health would exceed the maximum
if (currentHealth < maxHealth)
{
// Calculate the new health after adding
int newHealth = currentHealth + healthToAdd;
// Ensure the new health doesn't exceed the maximum
newHealth = Mathf.Clamp(newHealth, 0, maxHealth);
// If the new health is different from the current health, apply the change
if (newHealth != currentHealth)
{
currentHealth = newHealth;
CurrentStatsChange(); // Notify stats change if needed
}
}
// Check if adding defense would exceed the maximum
if (currentDefense < maxDefense)
{
// Calculate the new defense after adding
int newDefense = currentDefense + defenseToAdd;
// Ensure the new defense doesn't exceed the maximum
newDefense = Mathf.Clamp(newDefense, 0, maxDefense);
// If the new defense is different from the current defense, apply the change
if (newDefense != currentDefense)
{
currentDefense = newDefense;
CurrentStatsChange(); // Notify stats change if needed
}
}
// Check if adding mana would exceed the maximum
if (currentMana < maxMana)
{
// Calculate the new mana after adding
int newMana = currentMana + manaToAdd;
// Ensure the new mana doesn't exceed the maximum
newMana = Mathf.Clamp(newMana, 0, maxMana);
// If the new mana is different from the current mana, apply the change
if (newMana != currentMana)
{
currentMana = newMana;
CurrentStatsChange(); // Notify stats change if needed
}
}
}
// This allows the UsableItem to start coroutines on the player
public new Coroutine StartCoroutine(IEnumerator routine)
{
return base.StartCoroutine(routine);
}
// Check if the base directory exists
if (!Directory.Exists(baseDirectory))
{
Debug.LogWarning("<color=red><b>No saved player data found.</b></color>");
return;
}
// Append the player's folder based on PlayerName
string playerDirectory = Path.Combine(baseDirectory, PlayerName);
// Ensure the player's directory exists
if (!Directory.Exists(playerDirectory))
{
Debug.LogWarning($"<color=red><b>No saved data found for player: {PlayerName}</b></color>");
return;
}
// Construct the full file path
string filePath = Path.Combine(playerDirectory, $"{PlayerName}_Data.sav");
// Check if the file exists
if (File.Exists(filePath))
{
BinaryFormatter formatter = new BinaryFormatter();
try
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
PlayerData data = formatter.Deserialize(fileStream) as PlayerData;
// Assign loaded data back to the player (optional, depends on your game's logic)
PlayerName = data.playerName;
PlayerLevel = data.playerLevel;
CurrentEXP = data.currentEXP;
ExpToLevelUp = data.expToLevelUp;
currentHealth = data.currentHealth;
maxHealth = data.maxHealth;
currentMana = data.currentMana;
maxMana = data.maxMana;
currentDefense = data.currentDefense;
maxDefense = data.maxDefense;
currentAttack = data.currentAttack;
maxAttack = data.maxAttack;
currentAttackSpeed = data.currentAttackSpeed;
maxAttackSpeed = data.maxAttackSpeed;
currentCritical = data.currentCritical;
maxCritical = data.maxCritical;
currentLifeSteal = data.currentLifeSteal;
maxLifeSteal = data.maxLifeSteal;
currentRange = data.currentRange;
maxRange = data.maxRange;
currentElementalMastery = data.currentElementalMastery;
maxElementalMastery = data.maxElementalMastery;
currentSpeed = data.currentSpeed;
maxSpeed = data.maxSpeed;
currentCooldown = data.currentCooldown;
maxCooldown = data.maxCooldown;
currentKnockback = data.currentKnockback;
maxKnockback = data.maxKnockback;
currentRecovery = data.currentRecovery;
maxRecovery = data.maxRecovery;
currentEvade = data.currentEvade;
maxEvade = data.maxEvade;
currentFortune = data.currentFortune;
maxFortune = data.maxFortune;
// Assuming 'character' is an instance of the Character class
character.LoadStatsFromData(data);
playerSkill.LoadSkillData(PlayerName);
// Position (example)
Vector3 playerPosition = new Vector3(data.playerPosX, data.playerPosY, data.playerPosZ);
transform.position = playerPosition;
Debug.Log($"<color=#00FF00><b>Loaded PlayerData</b></color>");
}
}
catch (Exception e)
{
Debug.LogError($"<color=red><b>Failed to load player data: {e.Message}</b></color>");
}
}
else
{
Debug.LogWarning("<color=red><b>No saved player data found.</b></color>");
}
}