Skip to content
Snippets Groups Projects
PlayerStats.cs 8.22 KiB
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

/* 
	 * PlayerStats: Stores all numerical stats of player.  For player appearance, see setAppearance.cs
	 * - Any changes to health, ingame stats, currency, etc will use a getting and/or setting from here
	 * - This scripts should always be a component of "Player" (or the root object containing all player parts)
	 */
public class PlayerStats : MonoBehaviour {

	public static float maxHealth;
	public static float curHealth;
	public static string playerName;
	public static int vitality;
    public static int strength;
    public static int defense;
    public static int agility;
    public static int luck;

    public static float stamina;
    public static float staminaMax;

    //below will act as the invetory
    public static int healthPotions = 0;
    public static int strengthPotions = 0;
    public static int defensePotions = 0;
    public static int agilityPotions = 0;
    public static bool offenseBlessing;
    public static bool defenseBlessing;
    public static bool luckBlessing;
	private static bool created; // Was the character creation used? (debugging)
	private bool isDead;

	public WeaponStats defaultWeapon; // Incase currentWeapon not set properly.  Set this in inspector!
    public ArmourStats defaultShield;
    private static ArmourStats currentShield;
	private static WeaponStats currentWeapon;
	EnterDoor spawn;

	void Start() {

		if (!created) {
			setDefaultStats();
		}

        if (SceneManager.GetActiveScene().name == "arena") { // apply blessings
            //heal(); Remove comments to heal upon arena entry
            if (getOffenseBlessing() == true) {
                agility += 1;
                strength += 1;
            }
            if (getDefenseBlessing() == true) {
                defense += 1;
                vitality += 1;
            }
            if (getLuckBlessing() == true) {
                luck += 2;
            }
            
        }

        // stamina starts at 3 (at agility level 1), increase by 0.25 for every point in agility.
        staminaMax = (float)(5 + ((agility - 1) * 0.25));
        stamina = staminaMax;

        updateWeapon();
        updateShield();
    }
    void Update() {
        if (stamina < staminaMax) {
            stamina += Time.deltaTime; // refill stamina bar if not full
        }
    }
	/*
	 * Send damage to player
	 */ 
	public void Damage(int amount) {

        if (ArenaCombatControl.block == true) {
            curHealth -= (int)(amount * 0.5f);
        }
        else {
            curHealth -= amount - (int)(defense * 0.25f);
        }

        if (curHealth <= 0) {
			curHealth = 0;
			isDead = true;
			StartCoroutine("waitandload");
		}
	}

	/*
	 * Check if player is dead
	 */
	public bool isPlayerDead(){
		return isDead;
	}

	/*
	 * Use all players stats to calculate a slash damage output
	 * Sent to enemy, the enemy then applies damage reduction and subtracts it's health
	 */ 
	public int getDamageOutput(){
		int final = Random.Range (currentWeapon.damageMin, currentWeapon.damageMax);
		final += strength;
		return final;
	}

	IEnumerator waitandload() {
        if (SceneManager.GetActiveScene().name == "arena") { // deactivate blessings
            if (getOffenseBlessing() == true) {
                agility -= 1;
                strength -= 1;
                setOffenseBlessing(false);
            }
            if (getDefenseBlessing() == true) {
                defense -= 1;
                vitality -= 1;
                setDefenseBlessing(false);
            }
            if (getLuckBlessing() == true) {
                luck -= 2;
                setLuckBlessing(false);
            }

        }
        //leave time for a death animation and a defeat message, then load the main scene
        yield return new WaitForSeconds(4.0f);
		SceneManager.LoadScene("GameOver");
	}

	public void ChangeHealthBarp(int damage) {
		GameObject healthbar = GameObject.Find("healthbarFullp");
		float newsize = healthbar.transform.localScale.x - ((damage / maxHealth) * 0.3f);
		healthbar.transform.localScale = new Vector3(newsize, 0.3f, 1f);
		if (curHealth <= 0) {
			healthbar.GetComponent<SpriteRenderer>().enabled = false;
		}
	}

	/*
	 * Update the current weapon sprite
	 */ 
	private void updateWeapon() {
		GameObject weapon = GameObject.FindWithTag("currentWeapon"); // Get player weapon from scene
		SpriteRenderer spriteRenderer = weapon.GetComponent<SpriteRenderer> ();
		if (currentWeapon != null) {// If current not assigned, set default
			spriteRenderer.sprite = currentWeapon.image; // Set correct image of current weapon
		}
		else {
			currentWeapon = defaultWeapon;
			spriteRenderer.sprite = defaultWeapon.image;
		}

	}

	/*
	 * Update the current shield sprite
	 */ 
    private void updateShield() {
        GameObject armour = GameObject.FindWithTag("currentShield"); // Get player shield
        SpriteRenderer spriteRenderer = armour.GetComponent<SpriteRenderer>();
        if (currentShield != null) { // If current not assigned, set default
            spriteRenderer.sprite = currentShield.image; // Set correct image of current shield
        }
        else {
            currentShield = defaultShield;
            spriteRenderer.sprite = defaultShield.image;
        }

    }

    // needed for shop buttons
    public WeaponStats getWeapon() {
        return currentWeapon;
    }

    public ArmourStats getShield() {
        return currentShield;
    }

    public static float getHealth() {
		return curHealth;
	}
		
	public void setWeapon(WeaponStats newWeapon) {
		currentWeapon = newWeapon;
		updateWeapon();
	}

    public void setShield(ArmourStats newShield) {
        currentShield = newShield;
        updateShield();
    }

	public void setName(string newName) {
		playerName = newName;
	}

	public void setVitality(int newVitality) {
		vitality = newVitality;
		setMaxHealth(); // Vitality affects HP amount
	}

	// Calculated from vitality
	public void setMaxHealth() { // public to set portrait
		maxHealth = vitality * 10; // base health of 10
	}

	public void setStrength(int newStrength) {
		strength = newStrength;
	}

	public void setDefense(int newDefense) {
		defense = newDefense;
	}

	public void setAgility(int newAgility) {
		agility = newAgility;
	}

	public void setLuck(int newLuck) {
		luck = newLuck;
	}

    public void heal() {
        curHealth = maxHealth;
    }

    // inventory methods - for buying and consuming potions, blessing stuff
    public void addHealthPot(int amount) {
        healthPotions += amount;
    }
    public void addStrengthPot(int amount) {
        strengthPotions += amount;
    }
    public void addDefensePot(int amount) {
        defensePotions += amount;
    }
    public void addAgilityPot(int amount) {
        agilityPotions += amount;
    }
    public void subtractHealthPot(int amount) {
        healthPotions -= amount;
    }
    public void subtractStrengthPot(int amount) {
        strengthPotions -= amount;
    }
    public void subtractDefensePot(int amount) {
        defensePotions -= amount;
    }
    public void subtractAgilityPot(int amount) {
        agilityPotions -= amount;
    }
    // blessings
    public void setOffenseBlessing(bool option) {
        offenseBlessing = option;
    }
    public void setDefenseBlessing(bool option) {
        defenseBlessing = option;
    }
    public void setLuckBlessing(bool option) {
        luckBlessing = option;
    }

    public int getHealthPot() { // maybe to be used by A.I. decisions based on how many potions the player has?
        return healthPotions;
    }
    public int getStrengthPot() {
        return strengthPotions;
    }
    public int getDefensePot() {
        return defensePotions;
    }
    public int getAgilityPot() {
        return agilityPotions;
    }
    public bool getOffenseBlessing() {
        return offenseBlessing;
    }
    public bool getDefenseBlessing() {
        return defenseBlessing;
    }
    public bool getLuckBlessing() {
        return luckBlessing;
    }

    public string getName() {
        return playerName;
    }

	public void wasCreated(){
		created = true;
	}

	// The default stats incase creation skipped
	private void setDefaultStats(){
		setName ("No Name");
		setVitality (5);
		curHealth = maxHealth;
		setStrength (5);
		setDefense (5);
		setAgility (5);
		setLuck (5);
	}
}