0ad/binaries/data/mods/public/simulation/components/RangeVisualization.js
elexis ee5bb1fd61 RangeVisualization component and use it to visualize Aura ranges.
Differential Revision: https://code.wildfiregames.com/D238
Fixes #4349
Patch By: Sandarac
This was SVN commit r19519.
2017-05-06 00:47:21 +00:00

78 lines
2 KiB
JavaScript

function RangeVisualization() {}
RangeVisualization.prototype.Schema = "<empty/>";
RangeVisualization.prototype.Init = function()
{
this.enabled = false;
this.enabledRangeTypes = {
"Aura": false
};
this.rangeVisualizations = new Map();
for (let type in this.enabledRangeTypes)
this["GetVisual" + type + "Ranges"](type);
};
// The GUI enables visualizations
RangeVisualization.prototype.Serialize = null;
RangeVisualization.prototype.Deserialize = function(data)
{
this.Init();
};
RangeVisualization.prototype.GetVisualAuraRanges = function(type)
{
let cmpAuras = Engine.QueryInterface(this.entity, IID_Auras);
if (!cmpAuras)
return;
this.rangeVisualizations.set(type, []);
for (let auraName of cmpAuras.GetVisualAuraRangeNames())
this.rangeVisualizations.get(type).push({
"radius": cmpAuras.GetRange(auraName),
"texture": cmpAuras.GetLineTexture(auraName),
"textureMask": cmpAuras.GetLineTextureMask(auraName),
"thickness": cmpAuras.GetLineThickness(auraName),
});
};
RangeVisualization.prototype.SetEnabled = function(enabled, enabledRangeTypes)
{
this.enabled = enabled;
this.enabledRangeTypes = enabledRangeTypes;
this.RegenerateRangeVisualizations();
};
RangeVisualization.prototype.RegenerateRangeVisualizations = function()
{
let cmpSelectable = Engine.QueryInterface(this.entity, IID_Selectable);
if (!cmpSelectable)
return;
cmpSelectable.ResetRangeOverlays();
if (!this.enabled)
return;
// Only render individual range types that have been enabled
for (let rangeOverlayType of this.rangeVisualizations.keys())
if (this.enabledRangeTypes[rangeOverlayType])
for (let rangeOverlay of this.rangeVisualizations.get(rangeOverlayType))
cmpSelectable.AddRangeOverlay(
rangeOverlay.radius,
rangeOverlay.texture,
rangeOverlay.textureMask,
rangeOverlay.thickness);
};
RangeVisualization.prototype.OnOwnershipChanged = function(msg)
{
if (this.enabled && msg.to != -1)
this.RegenerateRangeVisualizations();
};
Engine.RegisterComponentType(IID_RangeVisualization, "RangeVisualization", RangeVisualization);