Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/TreasureCollecter.js
- This file was added.
function TreasureCollecter() {} | |||||
TreasureCollecter.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>"; | |||||
TreasureCollecter.prototype.Init = function() | |||||
{ | |||||
}; | |||||
/** | |||||
* @return {Object} - Min/Max range at which this entity can claim a treasure. | |||||
*/ | |||||
TreasureCollecter.prototype.GetRange = function() | |||||
{ | |||||
return { "min": 0, "max": +this.template.MaxDistance }; | |||||
}; | |||||
Stan: Todo? | |||||
Done Inline ActionsCould be ditched I guess, I copied it from ResourceGatherer. Freagarach: Could be ditched I guess, I copied it from ResourceGatherer. | |||||
Done Inline Actionsreorder min, max ? wraitii: reorder min, max ? | |||||
/** | |||||
* @param {number} target - Entity ID of the target. | |||||
* @return {boolean} - Whether we can collect from the target. | |||||
*/ | |||||
TreasureCollecter.prototype.CanCollect = function(target) | |||||
{ | |||||
Done Inline ActionsCanCollectFrom wraitii: CanCollectFrom | |||||
Done Inline ActionsWell, the idea is that treasures are one-shot entities, and collecting from suggests that it is a prolonged activity. Freagarach: Well, the idea is that treasures are one-shot entities, and `collecting from` suggests that it… | |||||
let 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. | |||||
*/ | |||||
TreasureCollecter.prototype.StartCollecting = function(target, callerIID) | |||||
{ | |||||
if (this.target) | |||||
Done Inline ActionsFrom wraitii: From | |||||
this.StopCollecting(); | |||||
Done Inline ActionsDo you really need the *1 ? :P Stan: Do you really need the *1 ? :P | |||||
let cmpTreasure = Engine.QueryInterface(target, IID_Treasure); | |||||
if (!cmpTreasure || !cmpTreasure.IsAvailable()) | |||||
return false; | |||||
this.target = target; | |||||
this.callerIID = callerIID; | |||||
// ToDo: Implement rate modifiers. | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
this.timer = cmpTimer.SetTimeout(this.entity, IID_TreasureCollecter, "CollectTreasure", cmpTreasure.CollectionTime(), null); | |||||
return true; | |||||
}; | |||||
/** | |||||
* @param {string} reason - The reason why we stopped collecting, used to notify the caller. | |||||
*/ | |||||
TreasureCollecter.prototype.StopCollecting = function(reason) | |||||
{ | |||||
if (this.timer) | |||||
{ | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.CancelTimer(this.timer); | |||||
delete this.timer; | |||||
} | |||||
delete this.target; | |||||
// The callerIID component may start gathering again, | |||||
// replacing the callerIID, which gets deleted after | |||||
// the callerIID has finished. Hence save the data. | |||||
let callerIID = this.callerIID; | |||||
Done Inline Actionsno check for that one? Stan: no check for that one?
What happens if you delete a non existing property? If nothing above… | |||||
delete this.callerIID; | |||||
if (reason && callerIID) | |||||
{ | |||||
let component = Engine.QueryInterface(this.entity, callerIID); | |||||
if (component) | |||||
component.ProcessMessage(reason, null); | |||||
} | |||||
}; | |||||
/** | |||||
* @params - Data and lateness are unused. | |||||
*/ | |||||
TreasureCollecter.prototype.CollectTreasure = function(data, lateness) | |||||
{ | |||||
let 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"); | |||||
}; | |||||
Done Inline ActionsI guess the function call isn't so bad. Stan: I guess the function call isn't so bad. | |||||
Done Inline ActionsThis whole function should be in the position helper, IMHO. Especially if the course taken here is used more often. Freagarach: This whole function should be in the position helper, IMHO. Especially if the course taken here… | |||||
/** | |||||
* @param {number} - The entity ID of the target to check. | |||||
* @return {boolean} - Whether this entity is in range of its target. | |||||
*/ | |||||
TreasureCollecter.prototype.IsTargetInRange = function(target) | |||||
{ | |||||
let range = this.GetRange(); | |||||
let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); | |||||
return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, range.max, false); | |||||
}; | |||||
Engine.RegisterComponentType(IID_TreasureCollecter, "TreasureCollecter", TreasureCollecter); | |||||
Done Inline ActionsI would perhaps suggest moving this to a helper, but can be done later. wraitii: I would perhaps suggest moving this to a helper, but can be done later. | |||||
Done Inline ActionsMy idea ;) Freagarach: My idea ;)
But later indeed. |
Wildfire Games · Phabricator
Todo?