mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace style for JavaScript code. This plugin was however updated the last time over 9 years ago and will be incompatible with ESLint v10, as that [removes `context.getSourceCode()`][1], the plugin relies on. To keep the eslint config working with ESLint v10, this replaces `eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule from `@stylistic/eslint-plugin`, a package we already use. While `@stylistic/brace-style` doesn't offer an option to format braces in exactly the same way as before, the "allman" style seems to be the one closest to the existing code. [1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members [2]: https://eslint.style/rules/brace-style
142 lines
4.3 KiB
JavaScript
142 lines
4.3 KiB
JavaScript
/**
|
|
* This class sets up a shortcut to a specific entity in the GUI panel.
|
|
* The button shows the portrait a tooltip with state information and a health and/or capture bar.
|
|
* Clicking the button selects and jumps to to the entity.
|
|
*/
|
|
class PanelEntity
|
|
{
|
|
constructor(selection, entityID, buttonID, orderKey)
|
|
{
|
|
this.selection = selection;
|
|
this.hitpoints = undefined;
|
|
this.capturePoints = undefined;
|
|
|
|
/**
|
|
* Public property
|
|
*/
|
|
this.entityID = entityID;
|
|
|
|
/**
|
|
* Public property
|
|
*/
|
|
this.orderKey = orderKey;
|
|
|
|
this.overlayName = "panelEntityHitOverlay[" + buttonID + "]";
|
|
this.panelEntityHealthBar = Engine.GetGUIObjectByName("panelEntityHealthBar[" + buttonID + "]");
|
|
this.panelEntityCaptureBar = Engine.GetGUIObjectByName("panelEntityCapture[" + buttonID + "]");
|
|
this.panelEntButton = Engine.GetGUIObjectByName("panelEntityButton[" + buttonID + "]");
|
|
this.panelEntButton.onPress = this.onPress.bind(this);
|
|
this.panelEntButton.onDoublePress = this.onDoublePress.bind(this);
|
|
this.panelEntButton.hidden = false;
|
|
|
|
const entityState = GetEntityState(entityID);
|
|
const template = GetTemplateData(entityState.template);
|
|
this.nameTooltip = setStringTags(g_SpecificNamesPrimary ? template.name.specific : template.name.generic, this.NameTags) + "\n";
|
|
|
|
Engine.GetGUIObjectByName("panelEntityHealthSection[" + buttonID + "]").hidden = !entityState.hitpoints;
|
|
|
|
const captureSection = Engine.GetGUIObjectByName("panelEntityCaptureSection[" + buttonID + "]");
|
|
captureSection.hidden = !entityState.capturePoints;
|
|
if (entityState.capturePoints)
|
|
{
|
|
this.capturePoints = entityState.capturePoints;
|
|
if (!entityState.hitpoints)
|
|
captureSection.size = Engine.GetGUIObjectByName("panelEntitySectionPosTop[" + buttonID + "]").size;
|
|
}
|
|
|
|
Engine.GetGUIObjectByName("panelEntityImage[" + buttonID + "]").sprite =
|
|
"stretched:" + this.PortraitDirectory + template.icon;
|
|
}
|
|
|
|
destroy()
|
|
{
|
|
this.panelEntButton.hidden = true;
|
|
stopColorFade(this.overlayName);
|
|
}
|
|
|
|
update(i, reposition)
|
|
{
|
|
// TODO: Instead of instant position changes, animate button movement.
|
|
if (reposition)
|
|
setPanelObjectPosition(this.panelEntButton, i, Infinity);
|
|
|
|
const entityState = GetEntityState(this.entityID);
|
|
this.updateHitpointsBar(entityState);
|
|
this.updateCapturePointsBar(entityState);
|
|
|
|
this.panelEntButton.tooltip =
|
|
this.nameTooltip +
|
|
this.Tooltips.map(tooltip => tooltip(entityState)).filter(tip => tip).join("\n");
|
|
}
|
|
|
|
updateHitpointsBar(entityState)
|
|
{
|
|
if (!entityState.hitpoints)
|
|
return;
|
|
|
|
if (this.hitpoints != entityState.hitpoints)
|
|
this.panelEntityHealthBar.size.rright = 100 * entityState.hitpoints / entityState.maxHitpoints;
|
|
|
|
if (entityState.hitpoints < this.hitpoints)
|
|
this.onAttacked();
|
|
this.hitpoints = entityState.hitpoints;
|
|
}
|
|
|
|
updateCapturePointsBar(entityState)
|
|
{
|
|
if (!entityState.capturePoints)
|
|
return;
|
|
|
|
const playerParts = this.panelEntityCaptureBar.children;
|
|
const setCaptureBarPart = function(player, startSize)
|
|
{
|
|
const captureBar = playerParts[player];
|
|
captureBar.size.rleft = startSize;
|
|
captureBar.size.rright = startSize + 100 * Math.max(0, Math.min(1, entityState.capturePoints[player] / entityState.maxCapturePoints));
|
|
captureBar.sprite = "color:" + g_DiplomacyColors.getPlayerColor(player, 128);
|
|
captureBar.hidden = false;
|
|
return captureBar.size.rright;
|
|
};
|
|
|
|
let size = setCaptureBarPart(entityState.player, 0);
|
|
for (const i in entityState.capturePoints)
|
|
if (i != entityState.player)
|
|
size = setCaptureBarPart(i, size);
|
|
|
|
if (entityState.capturePoints[entityState.player] < this.capturePoints[entityState.player])
|
|
this.onAttacked();
|
|
|
|
this.capturePoints = entityState.capturePoints;
|
|
}
|
|
|
|
onAttacked()
|
|
{
|
|
startColorFade(this.overlayName, 100, 0, colorFade_attackUnit, true, smoothColorFadeRestart_attackUnit);
|
|
}
|
|
|
|
onPress()
|
|
{
|
|
if (!Engine.HotkeyIsPressed("selection.add"))
|
|
this.selection.reset();
|
|
|
|
this.selection.addList([this.entityID]);
|
|
}
|
|
|
|
onDoublePress()
|
|
{
|
|
this.selection.selectAndMoveTo(getEntityOrHolder(this.entityID));
|
|
}
|
|
}
|
|
|
|
PanelEntity.prototype.NameTags = { "font": "sans-bold-16" };
|
|
|
|
PanelEntity.prototype.PortraitDirectory = "session/portraits/";
|
|
|
|
PanelEntity.prototype.Tooltips = [
|
|
getCurrentHealthTooltip,
|
|
getCurrentCaptureTooltip,
|
|
getAttackTooltip,
|
|
getResistanceTooltip,
|
|
getEntityTooltip,
|
|
getAurasTooltip
|
|
];
|