2010-10-02 14:41:29 +02:00
|
|
|
function StatusBars() {}
|
|
|
|
|
|
|
|
StatusBars.prototype.Schema =
|
2010-10-03 02:30:43 +02:00
|
|
|
"<element name='BarWidth'>" +
|
|
|
|
"<data type='decimal'/>" +
|
|
|
|
"</element>" +
|
|
|
|
"<element name='BarHeight'>" +
|
|
|
|
"<data type='decimal'/>" +
|
|
|
|
"</element>" +
|
2010-10-02 14:41:29 +02:00
|
|
|
"<element name='HeightOffset'>" +
|
|
|
|
"<data type='decimal'/>" +
|
|
|
|
"</element>";
|
|
|
|
|
|
|
|
// TODO: should add rank icon too
|
|
|
|
|
|
|
|
StatusBars.prototype.Init = function()
|
|
|
|
{
|
|
|
|
this.enabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
StatusBars.prototype.SetEnabled = function(enabled)
|
|
|
|
{
|
|
|
|
// Quick return if no change
|
|
|
|
if (enabled == this.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the displayed sprites
|
|
|
|
|
|
|
|
this.enabled = enabled;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
this.RegenerateSprites();
|
|
|
|
else
|
|
|
|
this.ResetSprites();
|
|
|
|
};
|
|
|
|
|
|
|
|
StatusBars.prototype.OnHealthChanged = function(msg)
|
|
|
|
{
|
|
|
|
if (this.enabled)
|
|
|
|
this.RegenerateSprites();
|
|
|
|
};
|
|
|
|
|
2010-10-03 02:30:43 +02:00
|
|
|
StatusBars.prototype.OnResourceSupplyChanged = function(msg)
|
|
|
|
{
|
|
|
|
if (this.enabled)
|
|
|
|
this.RegenerateSprites();
|
|
|
|
};
|
|
|
|
|
2010-10-02 14:41:29 +02:00
|
|
|
StatusBars.prototype.ResetSprites = function()
|
|
|
|
{
|
|
|
|
var cmpOverlayRenderer = Engine.QueryInterface(this.entity, IID_OverlayRenderer);
|
|
|
|
cmpOverlayRenderer.Reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
StatusBars.prototype.RegenerateSprites = function()
|
|
|
|
{
|
|
|
|
var cmpOverlayRenderer = Engine.QueryInterface(this.entity, IID_OverlayRenderer);
|
|
|
|
cmpOverlayRenderer.Reset();
|
|
|
|
|
|
|
|
// Size of health bar (in world-space units)
|
2010-10-03 02:30:43 +02:00
|
|
|
var width = +this.template.BarWidth;
|
|
|
|
var height = +this.template.BarHeight;
|
2010-10-02 14:41:29 +02:00
|
|
|
|
2010-10-03 02:30:43 +02:00
|
|
|
// World-space offset from the unit's position
|
2010-10-02 14:41:29 +02:00
|
|
|
var offset = { "x": 0, "y": +this.template.HeightOffset, "z": 0 };
|
|
|
|
|
2010-10-03 02:30:43 +02:00
|
|
|
// Billboard offset of next bar
|
|
|
|
var yoffset = 0;
|
2010-10-02 14:41:29 +02:00
|
|
|
|
2010-10-03 02:30:43 +02:00
|
|
|
var AddBar = function(type, amount)
|
|
|
|
{
|
2010-10-02 14:41:29 +02:00
|
|
|
cmpOverlayRenderer.AddSprite(
|
2010-10-03 02:30:43 +02:00
|
|
|
"art/textures/ui/session/icons/"+type+"_bg.png",
|
|
|
|
{ "x": -width/2, "y": -height/2 + yoffset }, { "x": width/2, "y": height/2 + yoffset },
|
2010-10-02 14:41:29 +02:00
|
|
|
offset
|
|
|
|
);
|
|
|
|
|
|
|
|
cmpOverlayRenderer.AddSprite(
|
2010-10-03 02:30:43 +02:00
|
|
|
"art/textures/ui/session/icons/"+type+"_fg.png",
|
|
|
|
{ "x": -width/2, "y": -height/2 + yoffset }, { "x": width*(amount - 0.5), "y": height/2 + yoffset },
|
2010-10-02 14:41:29 +02:00
|
|
|
offset
|
|
|
|
);
|
2010-10-03 02:30:43 +02:00
|
|
|
|
|
|
|
yoffset -= height * 1.2;
|
|
|
|
};
|
|
|
|
|
|
|
|
var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
|
|
|
|
if (cmpHealth)
|
|
|
|
{
|
|
|
|
AddBar("health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints());
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
|
|
|
|
if (cmpResourceSupply)
|
|
|
|
{
|
|
|
|
AddBar("supply", cmpResourceSupply.GetCurrentAmount() / cmpResourceSupply.GetMaxAmount());
|
2010-10-02 14:41:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.RegisterComponentType(IID_StatusBars, "StatusBars", StatusBars);
|