Skip to content

Instantly share code, notes, and snippets.

@ironchefpython
Created April 28, 2012 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironchefpython/2516464 to your computer and use it in GitHub Desktop.
Save ironchefpython/2516464 to your computer and use it in GitHub Desktop.
health.js
var fallingDamageSpeedThreshold = 20;
var excessSpeedDamageMultiplier = 10;
var FULL_HEALTH_EVENT = manager.getEvent("full_health");
var NO_HEALTH_EVENT = manager.getEvent("no_health");
var applyDamage = function(entity, amount, instigator) {
if (this.currentHealth <= 0) {
return;
}
this.timeSinceLastDamage = 0;
this.currentHealth -= amount;
if (this.currentHealth <= 0) {
entity.send(NO_HEALTH_EVENT, instigator);
}
}
Health = manager.registerPrototype({
"id": "Health",
"properties": {
"maxHealth": manager.numberType,
"regenRate": manager.numberType,
"waitBeforeRegen": manager.numberType,
"currentHealth": manager.numberType,
"timeSinceLastDamage": manager.numberType,
"partialRegen": manager.numberType,
"ratio": manager.calculatedType(manager.numberType, function() {
return this.currentHealth / this.maxHealth;
})
},
"methods": {
"fullHeal": function() {
this.currentHealth = this.maxHealth;
},
},
"constructor": function(maxHealth, regenRate, waitBeforeRegen) {
this.timeSinceLastDamage = 0;
this.partialRegen = 0;
this.maxHealth = maxHealth;
this.regenRate = regenRate;
this.waitBeforeRegen = waitBeforeRegen;
this.currentHealth = this.maxHealth;
},
"handlers": {
"damage": function(event) {
applyDamage.call(this, event.target, event.amount, event.instigator);
},
"land": function(event) {
if (event.velocity.y < 0 && -event.velocity.y > thisfallingDamageSpeedThreshold) {
var damage = Math.int((-event.velocity.y - fallingDamageSpeedThreshold) * excessSpeedDamageMultiplier);
if (damage > 0) {
applyDamage.call(this, entity.target, damage, null);
}
}
}
},
"update": function(delta, entity) {
if (health.currentHealth <= 0 || health.currentHealth == health.maxHealth || health.regenRate == 0) {
return;
}
this.timeSinceLastDamage += delta;
if (this.timeSinceLastDamage >= this.waitBeforeRegen) {
this.partialRegen += delta * this.regenRate;
if (this.partialRegen >= 1) {
this.currentHealth = Math.round(Math.min(this.maxHealth, this.currentHealth + this.partialRegen));
this.partialRegen %= 1;
if (this.currentHealth == this.maxHealth) {
entity.send(FULL_HEALTH_EVENT);
}
}
}
}
});
console.log(Health.timeSinceLastDamage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment