mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-25 15:33:03 +00:00
e7f1406c37
eslint --no-config-lookup --fix --rule '"prefer-const": 1' \
binaries/data/mods/public/simulation/components/[T-V]*
Ref: #7812
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
128 lines
3.5 KiB
JavaScript
128 lines
3.5 KiB
JavaScript
function TreasureCollector() {}
|
|
|
|
TreasureCollector.prototype.Schema =
|
|
"<a:help>Defines the treasure collecting abilities.</a:help>" +
|
|
"<a:example>" +
|
|
"<MaxDistance>2.0</MaxDistance>" +
|
|
"</a:example>" +
|
|
"<element name='MaxDistance' a:help='The maximum treasure taking distance in m.'>" +
|
|
"<ref name='positiveDecimal'/>" +
|
|
"</element>";
|
|
|
|
TreasureCollector.prototype.Init = function()
|
|
{
|
|
};
|
|
|
|
/**
|
|
* @return {Object} - Min/Max range at which this entity can claim a treasure.
|
|
*/
|
|
TreasureCollector.prototype.GetRange = function()
|
|
{
|
|
return { "min": 0, "max": +this.template.MaxDistance };
|
|
};
|
|
|
|
/**
|
|
* @param {number} target - Entity ID of the target.
|
|
* @return {boolean} - Whether we can collect from the target.
|
|
*/
|
|
TreasureCollector.prototype.CanCollect = function(target)
|
|
{
|
|
const cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
|
|
return cmpTreasure && cmpTreasure.IsAvailable();
|
|
};
|
|
|
|
/**
|
|
* @param {number} target - The target to collect.
|
|
* @param {number} callerIID - The IID to notify on specific events.
|
|
*
|
|
* @return {boolean} - Whether we started collecting.
|
|
*/
|
|
TreasureCollector.prototype.StartCollecting = function(target, callerIID)
|
|
{
|
|
if (this.target)
|
|
this.StopCollecting();
|
|
|
|
const cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
|
|
if (!cmpTreasure || !cmpTreasure.IsAvailable())
|
|
return false;
|
|
|
|
const cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
|
|
if (cmpVisual)
|
|
cmpVisual.SelectAnimation("collecting_treasure", false, 1.0);
|
|
|
|
this.target = target;
|
|
this.callerIID = callerIID;
|
|
|
|
// ToDo: Implement rate modifiers.
|
|
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
this.timer = cmpTimer.SetTimeout(this.entity, IID_TreasureCollector, "CollectTreasure", cmpTreasure.CollectionTime(), null);
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {string} reason - The reason why we stopped collecting, used to notify the caller.
|
|
*/
|
|
TreasureCollector.prototype.StopCollecting = function(reason)
|
|
{
|
|
if (!this.target)
|
|
return;
|
|
|
|
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
cmpTimer.CancelTimer(this.timer);
|
|
delete this.timer;
|
|
|
|
delete this.target;
|
|
|
|
const cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
|
|
if (cmpVisual)
|
|
cmpVisual.SelectAnimation("idle", false, 1.0);
|
|
|
|
// The callerIID component may start again,
|
|
// replacing the callerIID, hence save that.
|
|
const callerIID = this.callerIID;
|
|
delete this.callerIID;
|
|
|
|
if (reason && callerIID)
|
|
{
|
|
const component = Engine.QueryInterface(this.entity, callerIID);
|
|
if (component)
|
|
component.ProcessMessage(reason, null);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @params - Data and lateness are unused.
|
|
*/
|
|
TreasureCollector.prototype.CollectTreasure = function(data, lateness)
|
|
{
|
|
const cmpTreasure = Engine.QueryInterface(this.target, IID_Treasure);
|
|
if (!cmpTreasure || !cmpTreasure.IsAvailable())
|
|
{
|
|
this.StopCollecting("TargetInvalidated");
|
|
return;
|
|
}
|
|
|
|
if (!this.IsTargetInRange(this.target))
|
|
{
|
|
this.StopCollecting("OutOfRange");
|
|
return;
|
|
}
|
|
|
|
cmpTreasure.Reward(this.entity);
|
|
this.StopCollecting("TargetInvalidated");
|
|
};
|
|
|
|
/**
|
|
* @param {number} - The entity ID of the target to check.
|
|
* @return {boolean} - Whether this entity is in range of its target.
|
|
*/
|
|
TreasureCollector.prototype.IsTargetInRange = function(target)
|
|
{
|
|
const range = this.GetRange();
|
|
const cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager);
|
|
return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, range.max, false);
|
|
};
|
|
|
|
Engine.RegisterComponentType(IID_TreasureCollector, "TreasureCollector", TreasureCollector);
|