mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-08-15 14:43:32 -07:00
Simplify GetTemplateData calls
Previously it was made so that "template data" was a sort of super type of "entity state" and that the former could always be replaced by the latter. Using this we can simplify some code passages by removing GetTemplateData calls and using the entity's state directly instead.
This commit is contained in:
parent
35bf79fdf3
commit
874d35888a
6 changed files with 24 additions and 32 deletions
|
|
@ -30,8 +30,7 @@ class PanelEntity
|
||||||
this.panelEntButton.hidden = false;
|
this.panelEntButton.hidden = false;
|
||||||
|
|
||||||
const entityState = GetEntityState(entityID);
|
const entityState = GetEntityState(entityID);
|
||||||
const template = GetTemplateData(entityState.templateName);
|
this.nameTooltip = setStringTags(g_SpecificNamesPrimary ? entityState.name.specific : entityState.name.generic, this.NameTags) + "\n";
|
||||||
this.nameTooltip = setStringTags(g_SpecificNamesPrimary ? template.name.specific : template.name.generic, this.NameTags) + "\n";
|
|
||||||
|
|
||||||
Engine.GetGUIObjectByName("panelEntityHealthSection[" + buttonID + "]").hidden = !entityState.hitpoints;
|
Engine.GetGUIObjectByName("panelEntityHealthSection[" + buttonID + "]").hidden = !entityState.hitpoints;
|
||||||
|
|
||||||
|
|
@ -45,7 +44,7 @@ class PanelEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine.GetGUIObjectByName("panelEntityImage[" + buttonID + "]").sprite =
|
Engine.GetGUIObjectByName("panelEntityImage[" + buttonID + "]").sprite =
|
||||||
"stretched:" + this.PortraitDirectory + template.icon;
|
"stretched:" + this.PortraitDirectory + entityState.icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy()
|
destroy()
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ EntityGroups.prototype.add = function(ents)
|
||||||
{
|
{
|
||||||
if (this.ents[ent])
|
if (this.ents[ent])
|
||||||
continue;
|
continue;
|
||||||
var entState = GetEntityState(ent);
|
const entState = GetEntityState(ent);
|
||||||
|
|
||||||
// When this function is called during group rebuild, deleted
|
// When this function is called during group rebuild, deleted
|
||||||
// entities will not yet have been removed, so entities might
|
// entities will not yet have been removed, so entities might
|
||||||
|
|
@ -82,8 +82,7 @@ EntityGroups.prototype.add = function(ents)
|
||||||
if (!entState)
|
if (!entState)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var templateName = entState.templateName;
|
let key = entState.selectionGroupName || entState.templateName;
|
||||||
var key = GetTemplateData(templateName, entState.player).selectionGroupName || templateName;
|
|
||||||
|
|
||||||
// Group the ents by player and template
|
// Group the ents by player and template
|
||||||
if (entState.player !== undefined)
|
if (entState.player !== undefined)
|
||||||
|
|
|
||||||
|
|
@ -48,15 +48,13 @@ function updateGarrisonHealthBar(entState, selection)
|
||||||
// Fills out information that most entities have
|
// Fills out information that most entities have
|
||||||
function displaySingle(entState)
|
function displaySingle(entState)
|
||||||
{
|
{
|
||||||
const template = GetTemplateData(entState.templateName);
|
const primaryName = g_SpecificNamesPrimary ? entState.name.specific : entState.name.generic;
|
||||||
|
|
||||||
const primaryName = g_SpecificNamesPrimary ? template.name.specific : template.name.generic;
|
|
||||||
let secondaryName;
|
let secondaryName;
|
||||||
if (g_ShowSecondaryNames)
|
if (g_ShowSecondaryNames)
|
||||||
secondaryName = g_SpecificNamesPrimary ? template.name.generic : template.name.specific;
|
secondaryName = g_SpecificNamesPrimary ? entState.name.generic : entState.name.specific;
|
||||||
|
|
||||||
// If packed, add that to the generic name (reduces template clutter).
|
// If packed, add that to the generic name (reduces template clutter).
|
||||||
if (template.pack && template.pack.state == "packed")
|
if (entState.pack?.state === "packed")
|
||||||
{
|
{
|
||||||
if (secondaryName && g_ShowSecondaryNames)
|
if (secondaryName && g_ShowSecondaryNames)
|
||||||
secondaryName = sprintf(translate("%(secondaryName)s — Packed"), { "secondaryName": secondaryName });
|
secondaryName = sprintf(translate("%(secondaryName)s — Packed"), { "secondaryName": secondaryName });
|
||||||
|
|
@ -327,8 +325,8 @@ function displaySingle(entState)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: we should require all entities to have icons
|
// TODO: we should require all entities to have icons
|
||||||
Engine.GetGUIObjectByName("icon").sprite = template.icon ? ("stretched:session/portraits/" + template.icon) : "BackgroundBlack";
|
Engine.GetGUIObjectByName("icon").sprite = entState.icon ? ("stretched:session/portraits/" + entState.icon) : "BackgroundBlack";
|
||||||
if (template.icon)
|
if (entState.icon)
|
||||||
{
|
{
|
||||||
const iconBorder = Engine.GetGUIObjectByName("iconBorder");
|
const iconBorder = Engine.GetGUIObjectByName("iconBorder");
|
||||||
|
|
||||||
|
|
@ -373,7 +371,7 @@ function displaySingle(entState)
|
||||||
getAurasTooltip,
|
getAurasTooltip,
|
||||||
getEntityTooltip,
|
getEntityTooltip,
|
||||||
getTreasureTooltip
|
getTreasureTooltip
|
||||||
].map(func => func(template)));
|
].map(func => func(entState)));
|
||||||
|
|
||||||
const leftClickTooltip = hasClass(entState, "Unit") ? getFollowOnLeftClickTooltip() : getFocusOnLeftClickTooltip();
|
const leftClickTooltip = hasClass(entState, "Unit") ? getFollowOnLeftClickTooltip() : getFocusOnLeftClickTooltip();
|
||||||
iconTooltips.push(leftClickTooltip + " " + getTemplateViewerOnRightClickTooltip());
|
iconTooltips.push(leftClickTooltip + " " + getTemplateViewerOnRightClickTooltip());
|
||||||
|
|
|
||||||
|
|
@ -343,14 +343,12 @@ g_SelectionPanels.Garrison = {
|
||||||
"setupButton": function(data)
|
"setupButton": function(data)
|
||||||
{
|
{
|
||||||
const entState = GetEntityState(data.item.ents[0]);
|
const entState = GetEntityState(data.item.ents[0]);
|
||||||
|
if (!entState)
|
||||||
const template = GetTemplateData(entState.templateName, entState.player);
|
|
||||||
if (!template)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
data.button.onPress = function()
|
data.button.onPress = function()
|
||||||
{
|
{
|
||||||
unloadTemplate(template.selectionGroupName || entState.templateName, entState.player);
|
unloadTemplate(entState.selectionGroupName || entState.templateName, entState.player);
|
||||||
};
|
};
|
||||||
|
|
||||||
data.countDisplay.caption = data.item.ents.length > 1 ? data.item.ents.length : "";
|
data.countDisplay.caption = data.item.ents.length > 1 ? data.item.ents.length : "";
|
||||||
|
|
@ -360,9 +358,9 @@ g_SelectionPanels.Garrison = {
|
||||||
data.button.enabled = canUngarrison;
|
data.button.enabled = canUngarrison;
|
||||||
|
|
||||||
data.button.tooltip = (canUngarrison ?
|
data.button.tooltip = (canUngarrison ?
|
||||||
sprintf(translate("Unload %(name)s"), { "name": getEntityNames(template) }) + "\n" +
|
sprintf(translate("Unload %(name)s"), { "name": getEntityNames(entState) }) + "\n" +
|
||||||
translate("Single-click to unload 1. Shift-click to unload all of this type.") :
|
translate("Single-click to unload 1. Shift-click to unload all of this type.") :
|
||||||
getEntityNames(template)) + "\n" +
|
getEntityNames(entState)) + "\n" +
|
||||||
sprintf(translate("Player: %(playername)s"), {
|
sprintf(translate("Player: %(playername)s"), {
|
||||||
"playername": g_Players[entState.player].name
|
"playername": g_Players[entState.player].name
|
||||||
});
|
});
|
||||||
|
|
@ -374,7 +372,7 @@ g_SelectionPanels.Garrison = {
|
||||||
// also appear disabled to the owner of the structure.
|
// also appear disabled to the owner of the structure.
|
||||||
data.icon.sprite =
|
data.icon.sprite =
|
||||||
(canUngarrison || g_IsObserver ? "" : "grayscale:") +
|
(canUngarrison || g_IsObserver ? "" : "grayscale:") +
|
||||||
"stretched:session/portraits/" + template.icon;
|
"stretched:session/portraits/" + entState.icon;
|
||||||
|
|
||||||
setPanelObjectPosition(data.button, data.i, data.rowLength);
|
setPanelObjectPosition(data.button, data.i, data.rowLength);
|
||||||
|
|
||||||
|
|
@ -1048,7 +1046,7 @@ g_SelectionPanels.Research = {
|
||||||
{
|
{
|
||||||
showTemplateDetails(
|
showTemplateDetails(
|
||||||
t,
|
t,
|
||||||
GetTemplateData(baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).templateName, state.player).nativeCiv
|
baseData.unitEntStates.find(state => state.id == baseData.item.researchFacilityId).nativeCiv
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -1114,8 +1112,7 @@ g_SelectionPanels.Selection = {
|
||||||
"setupButton": function(data)
|
"setupButton": function(data)
|
||||||
{
|
{
|
||||||
const entState = GetEntityState(data.item.ents[0]);
|
const entState = GetEntityState(data.item.ents[0]);
|
||||||
const template = GetTemplateData(entState.templateName, entState.player);
|
if (!entState)
|
||||||
if (!template)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const ent of data.item.ents)
|
for (const ent of data.item.ents)
|
||||||
|
|
@ -1152,7 +1149,7 @@ g_SelectionPanels.Selection = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const unitOwner = GetEntityState(data.item.ents[0]).player;
|
const unitOwner = GetEntityState(data.item.ents[0]).player;
|
||||||
let tooltip = getEntityNames(template);
|
let tooltip = getEntityNames(entState);
|
||||||
if (data.carried)
|
if (data.carried)
|
||||||
tooltip += "\n" + Object.keys(data.carried).map(res =>
|
tooltip += "\n" + Object.keys(data.carried).map(res =>
|
||||||
resourceIcon(res) + data.carried[res]
|
resourceIcon(res) + data.carried[res]
|
||||||
|
|
@ -1177,8 +1174,8 @@ g_SelectionPanels.Selection = {
|
||||||
};
|
};
|
||||||
data.button.onPressRight = function() { removeFromSelectionGroup(data.item.key); };
|
data.button.onPressRight = function() { removeFromSelectionGroup(data.item.key); };
|
||||||
|
|
||||||
if (template.icon)
|
if (entState.icon)
|
||||||
data.icon.sprite = "stretched:session/portraits/" + template.icon;
|
data.icon.sprite = "stretched:session/portraits/" + entState.icon;
|
||||||
|
|
||||||
setPanelObjectPosition(data.button, data.i, data.rowLength);
|
setPanelObjectPosition(data.button, data.i, data.rowLength);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -824,8 +824,7 @@ function updateGroups()
|
||||||
// Determine the sum of the costs of a given template
|
// Determine the sum of the costs of a given template
|
||||||
const getCostSum = (ent) =>
|
const getCostSum = (ent) =>
|
||||||
{
|
{
|
||||||
const entState = GetEntityState(ent);
|
const cost = GetEntityState(ent).cost;
|
||||||
const cost = GetTemplateData(entState.templateName, entState.player).cost;
|
|
||||||
return cost ? Object.keys(cost).map(key => cost[key]).reduce((sum, cur) => sum + cur) : 0;
|
return cost ? Object.keys(cost).map(key => cost[key]).reduce((sum, cur) => sum + cur) : 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -842,12 +841,12 @@ function updateGroups()
|
||||||
// Choose the icon of the most common template (or the most costly if it's not unique)
|
// Choose the icon of the most common template (or the most costly if it's not unique)
|
||||||
if (g_Groups.groups[i].getTotalCount() > 0)
|
if (g_Groups.groups[i].getTotalCount() > 0)
|
||||||
{
|
{
|
||||||
const icon = GetTemplateData(GetEntityState(g_Groups.groups[i].getEntsGrouped().reduce((pre, cur) =>
|
const icon = GetEntityState(g_Groups.groups[i].getEntsGrouped().reduce((pre, cur) =>
|
||||||
{
|
{
|
||||||
if (pre.ents.length == cur.ents.length)
|
if (pre.ents.length == cur.ents.length)
|
||||||
return getCostSum(pre.ents[0]) > getCostSum(cur.ents[0]) ? pre : cur;
|
return getCostSum(pre.ents[0]) > getCostSum(cur.ents[0]) ? pre : cur;
|
||||||
return pre.ents.length > cur.ents.length ? pre : cur;
|
return pre.ents.length > cur.ents.length ? pre : cur;
|
||||||
}).ents[0]).templateName).icon;
|
}).ents[0]).icon;
|
||||||
|
|
||||||
Engine.GetGUIObjectByName("unitGroupIcon[" + i + "]").sprite =
|
Engine.GetGUIObjectByName("unitGroupIcon[" + i + "]").sprite =
|
||||||
icon ? ("stretched:session/portraits/" + icon) : "groupsIcon";
|
icon ? ("stretched:session/portraits/" + icon) : "groupsIcon";
|
||||||
|
|
|
||||||
|
|
@ -1250,7 +1250,7 @@ var g_UnitActions =
|
||||||
let trader;
|
let trader;
|
||||||
if (entState.trainer?.entities?.length)
|
if (entState.trainer?.entities?.length)
|
||||||
for (let i = 0; i < entState.trainer.entities.length; ++i)
|
for (let i = 0; i < entState.trainer.entities.length; ++i)
|
||||||
if ((trader = GetTemplateData(entState.trainer.entities[i]).trader))
|
if ((trader = entState.trainer.entities[i].trader))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
const traderData = {
|
const traderData = {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue