Anonymous Anonymous - 7 months ago
297 0

No description

Other

heal example pseudo code [small edits]

class Player {
	float health;
	float currentHealth;
	float healthRegeneration;
	float attack;
	float armor;

	init () {
		this.initHealth();
	}

	/**
	 * Sets `currentHealth` to the players `health` stat.
	 * This way, we can track the players `health` without modifying his stats.
	 */
	initHealth() {
		this.currentHealth = health;
	}

	heal(float value) {
		float netHeal = value;

		/**
		 * if the heal would bring the players `currentHealth` over his `health` stat, we want to reduce this amount so the `netHeal` is the difference
		 * ex: Player has 80/100 health. An incoming heal of 40 would be reduced to a `netHeal` of 20.
		 */
		if (this.currentHealth + netHeal >= this.health) {
			netHeal = this.health - this.currentHealth;
		}

		// applies the heal
		this.currentHealth += netHeal;
	}

	damage(float value) {
		float netDamage = value;
		
		// reduces the `netDamage` by any `armor` present
		netDamage -= armor;

		/**
		 * if the damage would bring the players `currentHealth` below 0, we want to reduce this amount so the `netDamage` is the difference
		 * ex: Player has 30/100 health. An incoming damage of 40 would be reduced to a `netDamage` of 10.
		 */
		if (this.currentHealth - netDamage <= 0) {
			netDamage -= this.currentHealth;
		}

		// applies the damage
		this.currentHealth -= netDamage;
	}

	/**
	 * Happens once per frame.
	 */
	update() {
		this.doHealthRegeneration();
	}

	/**
	 * attempts to heal the player for their `healthRegeneration` stat amount based on the time of the last frame.
	 */
	doHealthRegeneration() {
		this.heal(this.healthRegeneration * Time.deltaTime);
	}
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download