Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/StatusBars.js
const NATURAL_COLOR = "255 255 255 255"; // pure white | const g_NaturalColor = "255 255 255 255"; // pure white | ||||
bb: make it a g_FooBar | |||||
Not Done Inline ActionsMaybe a prototype property would look preferable (if that works with de/serialization) elexis: Maybe a prototype property would look preferable (if that works with de/serialization) | |||||
function StatusBars() {} | function StatusBars() {} | ||||
StatusBars.prototype.Schema = | StatusBars.prototype.Schema = | ||||
"<element name='BarWidth'>" + | "<element name='BarWidth'>" + | ||||
"<data type='decimal'/>" + | "<data type='decimal'/>" + | ||||
"</element>" + | "</element>" + | ||||
"<element name='BarHeight'>" + | "<element name='BarHeight'>" + | ||||
"<data type='decimal'/>" + | "<data type='decimal'/>" + | ||||
"</element>" + | "</element>" + | ||||
"<element name='HeightOffset'>" + | "<element name='HeightOffset'>" + | ||||
"<data type='decimal'/>" + | "<data type='decimal'/>" + | ||||
"</element>"; | "</element>"; | ||||
/** | /** | ||||
* For every sprite, the code will call their "Add" method when regenerating | * For every sprite, the code will call their "Add" method when regenerating | ||||
* the sprites. Every sprite adder should return the height it needs. | * the sprites. Every sprite adder should return the height it needs. | ||||
* | * | ||||
* Modders who need extra sprites can just modify this array, and | * Modders who need extra sprites can just modify this array, and | ||||
* provide the right methods. | * provide the right methods. | ||||
*/ | */ | ||||
StatusBars.prototype.Sprites = | StatusBars.prototype.Sprites = [ | ||||
Done Inline Actionsmove opening tag a line up and fix the indentation of the block. bb: move opening tag a line up and fix the indentation of the block. | |||||
[ | |||||
"PackBar", | "PackBar", | ||||
"ResourceSupplyBar", | "ResourceSupplyBar", | ||||
"CaptureBar", | "CaptureBar", | ||||
"HealthBar", | "HealthBar", | ||||
"AuraIcons", | "AuraIcons", | ||||
Done Inline Actionstrailling comma bb: trailling comma | |||||
]; | ]; | ||||
StatusBars.prototype.Init = function() | StatusBars.prototype.Init = function() | ||||
{ | { | ||||
this.enabled = false; | this.enabled = false; | ||||
this.auraSources = new Map(); | this.auraSources = new Map(); | ||||
}; | }; | ||||
/** | /** | ||||
* Don't serialise this.enabled since it's modified by the GUI | * Don't serialise this.enabled since it's modified by the GUI. | ||||
Done Inline Actionsperiod bb: period | |||||
*/ | */ | ||||
StatusBars.prototype.Serialize = function() | StatusBars.prototype.Serialize = function() | ||||
{ | { | ||||
return {"auraSources": this.auraSources}; | return { "auraSources": this.auraSources }; | ||||
}; | }; | ||||
StatusBars.prototype.Deserialize = function(data) | StatusBars.prototype.Deserialize = function(data) | ||||
{ | { | ||||
this.Init(); | this.Init(); | ||||
this.auraSources = data.auraSources; | this.auraSources = data.auraSources; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
StatusBars.prototype.RegenerateSprites = function() | StatusBars.prototype.RegenerateSprites = function() | ||||
{ | { | ||||
let cmpOverlayRenderer = Engine.QueryInterface(this.entity, IID_OverlayRenderer); | let cmpOverlayRenderer = Engine.QueryInterface(this.entity, IID_OverlayRenderer); | ||||
cmpOverlayRenderer.Reset(); | cmpOverlayRenderer.Reset(); | ||||
let yoffset = 0; | let yoffset = 0; | ||||
for (let sprite of this.Sprites) | for (let sprite of this.Sprites) | ||||
yoffset += this["Add"+sprite](cmpOverlayRenderer, yoffset); | yoffset += this["Add" + sprite](cmpOverlayRenderer, yoffset); | ||||
}; | }; | ||||
// Internal helper functions | // Internal helper functions | ||||
/** | /** | ||||
* Generic piece of code to add a bar | * Generic piece of code to add a bar. | ||||
Done Inline Actionsperiod bb: period | |||||
*/ | */ | ||||
StatusBars.prototype.AddBar = function(cmpOverlayRenderer, yoffset, type, amount) | StatusBars.prototype.AddBar = function(cmpOverlayRenderer, yoffset, type, amount) | ||||
{ | { | ||||
// Size of health bar (in world-space units) | // Size of health bar (in world-space units) | ||||
let width = +this.template.BarWidth; | let width = +this.template.BarWidth; | ||||
let height = +this.template.BarHeight; | let height = +this.template.BarHeight; | ||||
// World-space offset from the unit's position | // World-space offset from the unit's position | ||||
let offset = { "x": 0, "y": +this.template.HeightOffset, "z": 0 }; | let offset = { "x": 0, "y": +this.template.HeightOffset, "z": 0 }; | ||||
// background | // background | ||||
cmpOverlayRenderer.AddSprite( | cmpOverlayRenderer.AddSprite( | ||||
"art/textures/ui/session/icons/"+type+"_bg.png", | "art/textures/ui/session/icons/" + type + "_bg.png", | ||||
{ "x": -width/2, "y": yoffset }, | { "x": -width / 2, "y": yoffset }, | ||||
{ "x": width/2, "y": height + yoffset}, | { "x": width / 2, "y": height + yoffset }, | ||||
offset, | offset, | ||||
NATURAL_COLOR | g_NaturalColor | ||||
); | ); | ||||
// foreground | // foreground | ||||
cmpOverlayRenderer.AddSprite( | cmpOverlayRenderer.AddSprite( | ||||
"art/textures/ui/session/icons/"+type+"_fg.png", | "art/textures/ui/session/icons/" + type + "_fg.png", | ||||
{ "x": -width/2, "y": yoffset}, | { "x": -width / 2, "y": yoffset }, | ||||
{ "x": width*(amount - 0.5), "y": height + yoffset}, | { "x": width * (amount - 0.5), "y": height + yoffset }, | ||||
offset, | offset, | ||||
NATURAL_COLOR | g_NaturalColor | ||||
); | ); | ||||
return height * 1.2; | return height * 1.2; | ||||
}; | }; | ||||
StatusBars.prototype.AddPackBar = function(cmpOverlayRenderer, yoffset) | StatusBars.prototype.AddPackBar = function(cmpOverlayRenderer, yoffset) | ||||
{ | { | ||||
if (!this.enabled) | if (!this.enabled) | ||||
return 0; | return 0; | ||||
let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | ||||
if (!cmpPack || !cmpPack.IsPacking()) | if (!cmpPack || !cmpPack.IsPacking()) | ||||
return 0; | return 0; | ||||
return this.AddBar(cmpOverlayRenderer, yoffset, "pack", cmpPack.GetProgress()); | return this.AddBar(cmpOverlayRenderer, yoffset, "pack", cmpPack.GetProgress()); | ||||
}; | }; | ||||
StatusBars.prototype.AddHealthBar = function(cmpOverlayRenderer, yoffset) | StatusBars.prototype.AddHealthBar = function(cmpOverlayRenderer, yoffset) | ||||
{ | { | ||||
if (!this.enabled) | if (!this.enabled) | ||||
return 0; | return 0; | ||||
let cmpHealth = QueryMiragedInterface(this.entity, IID_Health); | let cmpHealth = QueryMiragedInterface(this.entity, IID_Health); | ||||
if (!cmpHealth || !cmpHealth.GetHitpoints() > 0) | if (!cmpHealth || cmpHealth.GetHitpoints() <= 0) | ||||
return 0; | return 0; | ||||
return this.AddBar(cmpOverlayRenderer, yoffset, "health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints()); | return this.AddBar(cmpOverlayRenderer, yoffset, "health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints()); | ||||
}; | }; | ||||
StatusBars.prototype.AddResourceSupplyBar = function(cmpOverlayRenderer, yoffset) | StatusBars.prototype.AddResourceSupplyBar = function(cmpOverlayRenderer, yoffset) | ||||
{ | { | ||||
if (!this.enabled) | if (!this.enabled) | ||||
Show All 35 Lines | StatusBars.prototype.AddCaptureBar = function(cmpOverlayRenderer, yoffset) | ||||
{ | { | ||||
let c = QueryPlayerIDInterface(playerID).GetColor(); | let c = QueryPlayerIDInterface(playerID).GetColor(); | ||||
let strColor = (c.r * 255) + " " + (c.g * 255) + " " + (c.b * 255) + " 255"; | let strColor = (c.r * 255) + " " + (c.g * 255) + " " + (c.b * 255) + " 255"; | ||||
let size = width * cp[playerID] / cmpCapturable.GetMaxCapturePoints(); | let size = width * cp[playerID] / cmpCapturable.GetMaxCapturePoints(); | ||||
cmpOverlayRenderer.AddSprite( | cmpOverlayRenderer.AddSprite( | ||||
"art/textures/ui/session/icons/capture_bar.png", | "art/textures/ui/session/icons/capture_bar.png", | ||||
{ "x": startSize, "y": yoffset }, | { "x": startSize, "y": yoffset }, | ||||
{ "x": startSize + size, "y": height + yoffset}, | { "x": startSize + size, "y": height + yoffset }, | ||||
offset, | offset, | ||||
strColor | strColor | ||||
); | ); | ||||
return size + startSize; | return size + startSize; | ||||
}; | }; | ||||
// first handle the owner's points, to keep those points on the left for clarity | // First handle the owner's points, to keep those points on the left for clarity | ||||
Done Inline Actionsuse capital letter bb: use capital letter | |||||
let size = setCaptureBarPart(owner, -width / 2); | let size = setCaptureBarPart(owner, -width / 2); | ||||
for (let i in cp) | for (let i in cp) | ||||
if (i != owner && cp[i] > 0) | if (i != owner && cp[i] > 0) | ||||
size = setCaptureBarPart(i, size); | size = setCaptureBarPart(i, size); | ||||
return height * 1.2; | return height * 1.2; | ||||
}; | }; | ||||
Show All 19 Lines | StatusBars.prototype.AddAuraIcons = function(cmpOverlayRenderer, yoffset) | ||||
let offset = { "x": 0, "y": +this.template.HeightOffset + yoffset, "z": 0 }; | let offset = { "x": 0, "y": +this.template.HeightOffset + yoffset, "z": 0 }; | ||||
let iconSize = +this.template.BarWidth / 2; | let iconSize = +this.template.BarWidth / 2; | ||||
let xoffset = -iconSize * (iconSet.size - 1) * 0.6; | let xoffset = -iconSize * (iconSet.size - 1) * 0.6; | ||||
for (let icon of iconSet) | for (let icon of iconSet) | ||||
{ | { | ||||
cmpOverlayRenderer.AddSprite( | cmpOverlayRenderer.AddSprite( | ||||
icon, | icon, | ||||
{ "x": xoffset - iconSize/2, "y": yoffset }, | { "x": xoffset - iconSize / 2, "y": yoffset }, | ||||
{ "x": xoffset + iconSize/2, "y": iconSize + yoffset }, | { "x": xoffset + iconSize / 2, "y": iconSize + yoffset }, | ||||
offset, | offset, | ||||
NATURAL_COLOR | g_NaturalColor | ||||
); | ); | ||||
xoffset += iconSize * 1.2; | xoffset += iconSize * 1.2; | ||||
} | } | ||||
return iconSize + this.template.BarHeight / 2; | return iconSize + this.template.BarHeight / 2; | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_StatusBars, "StatusBars", StatusBars); | Engine.RegisterComponentType(IID_StatusBars, "StatusBars", StatusBars); |
Wildfire Games · Phabricator
make it a g_FooBar