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} - Range at which this entity can claim a treasure. | |||||
*/ | |||||
TreasureCollecter.prototype.GetRange = function() | |||||
{ | |||||
// Maybe this should depend on the unit or target or something? | |||||
return { "max": +this.template.MaxDistance, "min": 0 }; | |||||
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 - The target to collect. | |||||
* @param {Object} callback - Optionally an object with the functions to call on specific events. | |||||
* Must at least have the IID of the component to call. | |||||
* | |||||
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… | |||||
* @return {boolean} - Whether we started collecting. | |||||
*/ | |||||
TreasureCollecter.prototype.StartCollecting = function(target, callback) | |||||
{ | |||||
if (this.target) | |||||
this.StopCollecting(); | |||||
let cmpTreasure = Engine.QueryInterface(target, IID_Treasure); | |||||
if (!cmpTreasure) | |||||
return false; | |||||
// ToDo: Implement rate modifiers. | |||||
let time = cmpTreasure.Time() * 1; | |||||
Done Inline ActionsFrom wraitii: From | |||||
Done Inline ActionsDo you really need the *1 ? :P Stan: Do you really need the *1 ? :P | |||||
this.target = target; | |||||
this.callback = callback; | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
this.timer = cmpTimer.SetTimeout(this.entity, IID_TreasureCollecter, "CollectTreasure", time, null); | |||||
return true; | |||||
}; | |||||
/** | |||||
* @param {String} reason - The reason why we stopped collecting. Currently implemented are: | |||||
* "outOfRange" and "targetInvalidated". | |||||
*/ | |||||
TreasureCollecter.prototype.StopCollecting = function(reason) | |||||
{ | |||||
if (this.timer) | |||||
{ | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.CancelTimer(this.timer); | |||||
delete this.timer; | |||||
} | |||||
if (this.target) | |||||
delete this.target; | |||||
if (reason && this.callback && this.callback[reason]) | |||||
{ | |||||
let component = Engine.QueryInterface(this.entity, this.callback.iid); | |||||
if (component) | |||||
component[this.callback[reason]].apply(component); | |||||
} | |||||
delete this.callback; | |||||
}; | |||||
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… | |||||
/** | |||||
* @params - Data and lateness are unused. | |||||
*/ | |||||
TreasureCollecter.prototype.CollectTreasure = function(data, lateness) | |||||
{ | |||||
let cmpTreasure = Engine.QueryInterface(this.target, IID_Treasure); | |||||
if (!cmpTreasure) | |||||
{ | |||||
this.StopCollecting("targetInvalidated"); | |||||
return; | |||||
} | |||||
if (!this.IsTargetInRange(this.target)) | |||||
{ | |||||
this.StopCollecting("outOfRange"); | |||||
return; | |||||
} | |||||
cmpTreasure.Collect(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. | |||||
*/ | |||||
TreasureCollecter.prototype.IsTargetInRange = function(target) | |||||
{ | |||||
let range = this.GetRange(); | |||||
let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); | |||||
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… | |||||
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?