Files
pgs/Assets/Scripts/State/Stats.cs
2026-02-21 16:58:22 -08:00

133 lines
4.3 KiB
C#

using UnityEngine;
using System.Collections;
public class Stats {
public static readonly int maxLevel = 100;
public static readonly int statsGainedOnLevelUp = 2;
public static readonly int hpGainedOnLevelUp = 50;
public static readonly int skillPointsGainedOnLevelUp = 1;
public static readonly float maxDex = 500.0f;
public int pwr = 30; // Power
public int dex = 30; // Dexterity
public int stb = 0; // Damage stability
public int def = 0; // Defense
public int level = 1;
public int exp = 0;
public int maxExp = 100;
public int totalExp = 0;
public int availableStatPoints = 0;
public int hp = 100;
public int maxHP = 100;
public int currentLevelCap = 50;
public StatsCache cache = new StatsCache();
public void updateCache() {
// Initialize stat totals from base stats.
int _pwr = pwr;
int _dex = dex;
int _stb = stb;
int _def = def;
int _maxHP = maxHP;
int _attack = 10;
float _pwrPercentage = 0;
float _dexPercentage = 0;
float _stbPercentage = 0;
float _defPercentage = 0;
float _maxHPPercentage = 0;
float _attackPercentage = 0;
// Tally up the total stats of all items.
foreach (System.Collections.Generic.KeyValuePair<ItemType, Item> KV in State.state.inventory.equipped) {
if (KV.Value == null) continue;
Item equip = KV.Value;
_pwr += equip.data.equipStats.pwr;
_dex += equip.data.equipStats.dex;
_stb += equip.data.equipStats.stb;
_def += equip.data.equipStats.def;
_maxHP += equip.data.equipStats.maxHP;
_attack += equip.data.equipStats.attack;
_pwrPercentage += equip.data.equipStats.pwrPercentage;
_dexPercentage += equip.data.equipStats.dexPercentage;
_stbPercentage += equip.data.equipStats.stbPercentage;
_defPercentage += equip.data.equipStats.defPercentage;
_maxHPPercentage += equip.data.equipStats.maxHPPercentage;
_attackPercentage += equip.data.equipStats.attackPercentage;
}
// Calculate final results!
cache.totalPwr = _pwr*(1+_pwrPercentage/100);
cache.totalDex = _dex*(1+_dexPercentage/100);
cache.totalStb = _stb*(1+_stbPercentage/100);
cache.totalDef = _def*(1+_defPercentage/100);
cache.totalMaxHP = _maxHP*(1+_maxHPPercentage/100);
cache.totalAttack = _attack*(1+_attackPercentage/100);
cache.totalAvoid = Mathf.Max(0, _dex/maxDex);
cache.baseDamage = cache.totalAttack * (cache.totalPwr + 0.3f*cache.totalDex);
}
// Returns true if you leveled up.
public bool gainExp(int exp_) {
if (level == currentLevelCap) return false;
totalExp += exp_;
exp += exp_;
bool leveledUp = false;
// Level up however many times.
while (exp >= maxExp) {
if (level < currentLevelCap) {
leveledUp = true;
level++;
maxHP += hpGainedOnLevelUp;
hp = maxHP;
availableStatPoints += statsGainedOnLevelUp;
} else {
break;
}
exp -= maxExp;
maxExp = StatsData.levelExp[level];
}
if (level == currentLevelCap) {
exp = 0;
}
if (leveledUp) {
this.updateCache();
}
return leveledUp;
}
// Returns the amount of damage taken.
public int takeDamage(int damage) {
int dmg = Mathf.Max(damage - (int)cache.totalDef, 1);
hp = Mathf.Max(0, hp - dmg);
// this.updateCache();
return dmg;
}
public void allocateStat(int pwr_ = 0, int dex_ = 0, int stb_ = 0, int def_ = 0) {
this.pwr += pwr_;
this.dex += dex_;
this.stb += stb_;
this.def += def_;
this.updateCache();
}
}
public class StatsCache {
// Calculate these in order.
public float totalPwr;
public float totalDex;
public float totalMaxHP;
public float totalAttack;
public float totalStb;
public float baseDamage;
public float totalAvoid;
public float totalDef;
}