Anonymous Anonymous - 8 months ago
329 1

No description

Other

heal example pseudo code

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

	init () {
		this.initHealth();
	}

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

	heal(float value) {
		float netHeal = value;

		if (this.currentHealth + netHeal >= this.health) {
			/**
			 * 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.
			 */
			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 (this.currentHealth - netDamage <= 0) {
			/**
			 * 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.
			 */
			netDamage = netDamage - this.currentHealth;
		}

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

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

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