Here’s my entire code, if you know anyways of improving it could you say please, I want to learn how to make my code more efficient.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class PlayerStats : MonoBehaviour {
public bool gameStarted = false;
//Health
public int healthRegenAmount; public float healthRegenTimer;
[SerializeField] private bool isRegeningHealth = false;
bool statsUpdateRunning;
[Header("Stats")]
public int[] currentStats;
public int[] totalStats;
public Dictionary<SkillTypes, Skill> skills = new Dictionary<SkillTypes, Skill>() {
//Combat Skills
{ SkillTypes.Health, new Skill() }, { SkillTypes.Melee, new Skill() }, { SkillTypes.Range, new Skill() }, { SkillTypes.Magic, new Skill() },
{ SkillTypes.Defence, new Skill() }, { SkillTypes.Prayer, new Skill() }, { SkillTypes.Slayer, new Skill() },
//Gathering Skills
{ SkillTypes.Mining, new Skill() }, { SkillTypes.Fishing, new Skill() }, { SkillTypes.Woodcutting, new Skill() },
{ SkillTypes.Hunting, new Skill() }, { SkillTypes.Farming, new Skill() }, { SkillTypes.Thieving, new Skill() },
//Production Skills
{ SkillTypes.Cooking, new Skill() }, { SkillTypes.Smithing, new Skill() }, { SkillTypes.Fletching, new Skill() }, { SkillTypes.Firemaking, new Skill() },
{ SkillTypes.Herblore, new Skill() }, { SkillTypes.Crafting, new Skill() }, { SkillTypes.Construction, new Skill() }
};
void Awake() {
currentStats = new int[skills.Count];
totalStats = new int[skills.Count];
if (!gameStarted) {
for (int i = 0; i < skills.Count; i++) {
currentStats[i] = skills[(SkillTypes)i].current;
totalStats[i] = skills[(SkillTypes)i].total;
}
}
}
// Use this for initialization
void Start() {
if (!gameStarted) {
skills[SkillTypes.Health].current = skills[SkillTypes.Health].total;
gameStarted = true;
StartCoroutine(UpdateStats());
}
}
// Update is called once per frame
void Update() {
//Regen health
if (skills[SkillTypes.Health].current != skills[SkillTypes.Health].total && !isRegeningHealth) {
StartCoroutine(HealthRegen());
}
if (Input.GetKeyDown(KeyCode.A)) {
skills[SkillTypes.Construction].current += 1;
skills[SkillTypes.Construction].total = skills[SkillTypes.Construction].current;
StatsToUpdate();
}
if (Input.GetKeyDown(KeyCode.S)) {
CheckExpAmount();
}
if (!statsUpdateRunning) {
StartCoroutine(UpdateStats());
}
}
IEnumerator HealthRegen() {
isRegeningHealth = true;
while (skills[SkillTypes.Health].current < skills[SkillTypes.Health].total) {
skills[SkillTypes.Health].current += healthRegenAmount;
if (skills[SkillTypes.Health].current > skills[SkillTypes.Health].total) {
skills[SkillTypes.Health].current = skills[SkillTypes.Health].total;
}
yield return new WaitForSeconds(healthRegenTimer);
}
isRegeningHealth = false;
}
IEnumerator UpdateStats() {
statsUpdateRunning = true;
while (gameStarted) {
StatsToUpdate();
yield return new WaitForSeconds(60f);
}
statsUpdateRunning = false;
}
private void StatsToUpdate() {
for (int i = 0; i < skills.Count; i++) {
currentStats[i] = skills[(SkillTypes)i].current;
totalStats[i] = skills[(SkillTypes)i].total;
}
print("Updated stats!");
}
public void CheckExpAmount() {
for (int i = 0; i < skills.Count; i++) {
if (skills[(SkillTypes)i].currentExp >= skills[(SkillTypes)i].expToLevel) {
skills[(SkillTypes)i].total += 1;
print("i = " + i); //know what's been leveled up
}
}
}
}
public enum SkillTypes {
//Combat Skills
Health, Melee, Range, Magic, Defence, Prayer, Slayer,
//Gathering Skills
Mining, Fishing, Woodcutting, Hunting, Farming, Thieving,
//Production Skills
Cooking, Smithing, Fletching, Firemaking, Herblore, Crafting, Construction
}
public class Skill {
public int current = 1;
public int total = 1;
public int currentExp = 110;
public int expToLevel = 100;
}