Health.js cleanup. Fixes Vulkan complaints

- Var → let
- Simplify some lines
- add break to

Reviewed by: @elexis
Differential Revision: https://code.wildfiregames.com/D1770
This was SVN commit r22075.
This commit is contained in:
Stan
2019-01-31 13:34:13 +00:00
parent 111f850927
commit 700baef3b4
@@ -64,8 +64,6 @@ Health.prototype.Init = function()
this.UpdateActor(); this.UpdateActor();
}; };
//// Interface functions ////
/** /**
* Returns the current hitpoint value. * Returns the current hitpoint value.
* This is 0 if (and only if) the unit is dead. * This is 0 if (and only if) the unit is dead.
@@ -91,17 +89,12 @@ Health.prototype.SetHitpoints = function(value)
if (cmpFogging) if (cmpFogging)
cmpFogging.Activate(); cmpFogging.Activate();
var old = this.hitpoints; let old = this.hitpoints;
this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value)); this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value));
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (cmpRangeManager) if (cmpRangeManager)
{ cmpRangeManager.SetEntityFlag(this.entity, "injured", this.hitpoints < this.GetMaxHitpoints());
if (this.hitpoints < this.GetMaxHitpoints())
cmpRangeManager.SetEntityFlag(this.entity, "injured", true);
else
cmpRangeManager.SetEntityFlag(this.entity, "injured", false);
}
this.RegisterHealthChanged(old); this.RegisterHealthChanged(old);
}; };
@@ -113,9 +106,9 @@ Health.prototype.IsRepairable = function()
Health.prototype.IsUnhealable = function() Health.prototype.IsUnhealable = function()
{ {
return (this.template.Unhealable == "true" return (this.template.Unhealable == "true" ||
|| this.GetHitpoints() <= 0 this.GetHitpoints() <= 0 ||
|| this.GetHitpoints() >= this.GetMaxHitpoints()); this.GetHitpoints() >= this.GetMaxHitpoints());
}; };
Health.prototype.GetIdleRegenRate = function() Health.prototype.GetIdleRegenRate = function()
@@ -151,13 +144,13 @@ Health.prototype.CheckRegenTimer = function()
{ {
// check if we need a timer // check if we need a timer
if (this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0 || if (this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0 ||
this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() >= 0 && this.GetIdleRegenRate() >= 0 || this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() >= 0 && this.GetIdleRegenRate() >= 0 ||
this.GetHitpoints() == 0) this.GetHitpoints() == 0)
{ {
// we don't need a timer, disable if one exists // we don't need a timer, disable if one exists
if (this.regenTimer) if (this.regenTimer)
{ {
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
cmpTimer.CancelTimer(this.regenTimer); cmpTimer.CancelTimer(this.regenTimer);
this.regenTimer = undefined; this.regenTimer = undefined;
} }
@@ -168,7 +161,7 @@ Health.prototype.CheckRegenTimer = function()
if (this.regenTimer) if (this.regenTimer)
return; return;
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.regenTimer = cmpTimer.SetInterval(this.entity, IID_Health, "ExecuteRegeneration", 1000, 1000, null); this.regenTimer = cmpTimer.SetInterval(this.entity, IID_Health, "ExecuteRegeneration", 1000, 1000, null);
}; };
@@ -188,14 +181,14 @@ Health.prototype.Reduce = function(amount)
if (cmpFogging) if (cmpFogging)
cmpFogging.Activate(); cmpFogging.Activate();
var state = { "killed": false }; let state = { "killed": false };
if (amount >= 0 && this.hitpoints == this.GetMaxHitpoints()) if (amount >= 0 && this.hitpoints == this.GetMaxHitpoints())
{ {
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (cmpRangeManager) if (cmpRangeManager)
cmpRangeManager.SetEntityFlag(this.entity, "injured", true); cmpRangeManager.SetEntityFlag(this.entity, "injured", true);
} }
var oldHitpoints = this.hitpoints; let oldHitpoints = this.hitpoints;
if (amount >= this.hitpoints) if (amount >= this.hitpoints)
{ {
// If this is the first time we reached 0, then die. // If this is the first time we reached 0, then die.
@@ -227,6 +220,7 @@ Health.prototype.Reduce = function(amount)
let resource = this.CreateCorpse(true); let resource = this.CreateCorpse(true);
if (resource != INVALID_ENTITY) if (resource != INVALID_ENTITY)
Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource }); Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource });
break;
} }
case "vanish": case "vanish":
@@ -258,42 +252,40 @@ Health.prototype.Increase = function(amount)
cmpFogging.Activate(); cmpFogging.Activate();
if (this.hitpoints == this.GetMaxHitpoints()) if (this.hitpoints == this.GetMaxHitpoints())
return {"old": this.hitpoints, "new":this.hitpoints}; return { "old": this.hitpoints, "new": this.hitpoints };
// If we're already dead, don't allow resurrection // If we're already dead, don't allow resurrection
if (this.hitpoints == 0) if (this.hitpoints == 0)
return undefined; return undefined;
var old = this.hitpoints; let old = this.hitpoints;
this.hitpoints = Math.min(this.hitpoints + amount, this.GetMaxHitpoints()); this.hitpoints = Math.min(this.hitpoints + amount, this.GetMaxHitpoints());
if (this.hitpoints == this.GetMaxHitpoints()) if (this.hitpoints == this.GetMaxHitpoints())
{ {
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (cmpRangeManager) if (cmpRangeManager)
cmpRangeManager.SetEntityFlag(this.entity, "injured", false); cmpRangeManager.SetEntityFlag(this.entity, "injured", false);
} }
this.RegisterHealthChanged(old); this.RegisterHealthChanged(old);
return { "old": old, "new": this.hitpoints}; return { "old": old, "new": this.hitpoints };
}; };
//// Private functions ////
Health.prototype.CreateCorpse = function(leaveResources) Health.prototype.CreateCorpse = function(leaveResources)
{ {
// If the unit died while not in the world, don't create any corpse for it // If the unit died while not in the world, don't create any corpse for it
// since there's nowhere for the corpse to be placed // since there's nowhere for the corpse to be placed
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
if (!cmpPosition.IsInWorld()) if (!cmpPosition.IsInWorld())
return INVALID_ENTITY; return INVALID_ENTITY;
// Either creates a static local version of the current entity, or a // Either creates a static local version of the current entity, or a
// persistent corpse retaining the ResourceSupply element of the parent. // persistent corpse retaining the ResourceSupply element of the parent.
var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
var templateName = cmpTemplateManager.GetCurrentTemplateName(this.entity); let templateName = cmpTemplateManager.GetCurrentTemplateName(this.entity);
var corpse; let corpse;
if (leaveResources) if (leaveResources)
corpse = Engine.AddEntity("resource|" + templateName); corpse = Engine.AddEntity("resource|" + templateName);
else else
@@ -301,19 +293,19 @@ Health.prototype.CreateCorpse = function(leaveResources)
// Copy various parameters so it looks just like us // Copy various parameters so it looks just like us
var cmpCorpsePosition = Engine.QueryInterface(corpse, IID_Position); let cmpCorpsePosition = Engine.QueryInterface(corpse, IID_Position);
var pos = cmpPosition.GetPosition(); let pos = cmpPosition.GetPosition();
cmpCorpsePosition.JumpTo(pos.x, pos.z); cmpCorpsePosition.JumpTo(pos.x, pos.z);
var rot = cmpPosition.GetRotation(); let rot = cmpPosition.GetRotation();
cmpCorpsePosition.SetYRotation(rot.y); cmpCorpsePosition.SetYRotation(rot.y);
cmpCorpsePosition.SetXZRotation(rot.x, rot.z); cmpCorpsePosition.SetXZRotation(rot.x, rot.z);
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
var cmpCorpseOwnership = Engine.QueryInterface(corpse, IID_Ownership); let cmpCorpseOwnership = Engine.QueryInterface(corpse, IID_Ownership);
cmpCorpseOwnership.SetOwner(cmpOwnership.GetOwner()); cmpCorpseOwnership.SetOwner(cmpOwnership.GetOwner());
var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
var cmpCorpseVisual = Engine.QueryInterface(corpse, IID_Visual); let cmpCorpseVisual = Engine.QueryInterface(corpse, IID_Visual);
cmpCorpseVisual.SetActorSeed(cmpVisual.GetActorSeed()); cmpCorpseVisual.SetActorSeed(cmpVisual.GetActorSeed());
// Make it fall over // Make it fall over
@@ -326,23 +318,23 @@ Health.prototype.CreateDeathSpawnedEntity = function()
{ {
// If the unit died while not in the world, don't spawn a death entity for it // If the unit died while not in the world, don't spawn a death entity for it
// since there's nowhere for it to be placed // since there's nowhere for it to be placed
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
if (!cmpPosition.IsInWorld()) if (!cmpPosition.IsInWorld())
return INVALID_ENTITY; return INVALID_ENTITY;
// Create SpawnEntityOnDeath entity // Create SpawnEntityOnDeath entity
var spawnedEntity = Engine.AddLocalEntity(this.template.SpawnEntityOnDeath); let spawnedEntity = Engine.AddLocalEntity(this.template.SpawnEntityOnDeath);
// Move to same position // Move to same position
var cmpSpawnedPosition = Engine.QueryInterface(spawnedEntity, IID_Position); let cmpSpawnedPosition = Engine.QueryInterface(spawnedEntity, IID_Position);
var pos = cmpPosition.GetPosition(); let pos = cmpPosition.GetPosition();
cmpSpawnedPosition.JumpTo(pos.x, pos.z); cmpSpawnedPosition.JumpTo(pos.x, pos.z);
var rot = cmpPosition.GetRotation(); let rot = cmpPosition.GetRotation();
cmpSpawnedPosition.SetYRotation(rot.y); cmpSpawnedPosition.SetYRotation(rot.y);
cmpSpawnedPosition.SetXZRotation(rot.x, rot.z); cmpSpawnedPosition.SetXZRotation(rot.x, rot.z);
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
var cmpSpawnedOwnership = Engine.QueryInterface(spawnedEntity, IID_Ownership); let cmpSpawnedOwnership = Engine.QueryInterface(spawnedEntity, IID_Ownership);
if (cmpOwnership && cmpSpawnedOwnership) if (cmpOwnership && cmpSpawnedOwnership)
cmpSpawnedOwnership.SetOwner(cmpOwnership.GetOwner()); cmpSpawnedOwnership.SetOwner(cmpOwnership.GetOwner());