2010-02-12 23:46:53 +01:00
function ResourceGatherer ( ) { }
2010-04-09 21:02:39 +02:00
ResourceGatherer . prototype . Schema =
2010-04-23 18:09:03 +02:00
"<a:help>Lets the unit gather resources from entities that have the ResourceSupply component.</a:help>" +
"<a:example>" +
"<BaseSpeed>1.0</BaseSpeed>" +
"<Rates>" +
"<food.fish>1</food.fish>" +
2010-07-21 20:51:27 +02:00
"<metal.ore>3</metal.ore>" +
"<stone.rock>3</stone.rock>" +
"<wood.tree>2</wood.tree>" +
2010-04-23 18:09:03 +02:00
"</Rates>" +
"</a:example>" +
"<element name='BaseSpeed' a:help='Base resource-gathering rate (in resource units per second)'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='Rates' a:help='Per-resource-type gather rate multipliers. If a resource type is not specified then it cannot be gathered by this unit'>" +
2010-04-09 21:02:39 +02:00
"<interleave>" +
2010-04-23 18:09:03 +02:00
"<optional><element name='food' a:help='Food gather rate (may be overridden by \"food.*\" subtypes)'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='wood' a:help='Wood gather rate'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='stone' a:help='Stone gather rate'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='metal' a:help='Metal gather rate'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='food.fish' a:help='Fish gather rate (overrides \"food\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='food.fruit' a:help='Fruit gather rate (overrides \"food\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='food.grain' a:help='Grain gather rate (overrides \"food\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='food.meat' a:help='Meat gather rate (overrides \"food\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='food.milk' a:help='Milk gather rate (overrides \"food\")'><ref name='positiveDecimal'/></element></optional>" +
2010-07-21 20:51:27 +02:00
"<optional><element name='wood.tree' a:help='Tree gather rate (overrides \"wood\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='stone.rock' a:help='Rock gather rate (overrides \"stone\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='metal.ore' a:help='Ore gather rate(overrides \"metal\")'><ref name='positiveDecimal'/></element></optional>" +
"<optional><element name='metal.treasure' a:help='Treasure gather rate(overrides \"metal\")'><ref name='positiveDecimal'/></element></optional>" +
2010-04-09 21:02:39 +02:00
"</interleave>" +
"</element>" ;
2010-02-12 23:46:53 +01:00
ResourceGatherer . prototype . Init = function ( )
{
} ;
ResourceGatherer . prototype . GetGatherRates = function ( )
{
var ret = { } ;
for ( var r in this . template . Rates )
ret [ r ] = this . template . Rates [ r ] * this . template . BaseSpeed ;
return ret ;
} ;
ResourceGatherer . prototype . GetRange = function ( )
{
2010-07-21 18:09:58 +02:00
return { "max" : 2 , "min" : 0 } ;
2010-02-12 23:46:53 +01:00
// maybe this should depend on the unit or target or something?
}
/ * *
* Gather from the target entity . This should only be called after a successful range check ,
* and if the target has a compatible ResourceSupply .
* It should be called at a rate of once per second .
* /
ResourceGatherer . prototype . PerformGather = function ( target )
{
2010-07-08 22:08:08 +02:00
var rate = this . GetTargetGatherRate ( target ) ;
if ( ! rate )
return { "exhausted" : true } ;
2010-02-12 23:46:53 +01:00
var cmpResourceSupply = Engine . QueryInterface ( target , IID _ResourceSupply ) ;
var type = cmpResourceSupply . GetType ( ) ;
var status = cmpResourceSupply . TakeResources ( rate ) ;
// Give the gathered resources to the player
var cmpPlayerManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _PlayerManager ) ;
var cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
var cmpPlayer = Engine . QueryInterface ( cmpPlayerManager . GetPlayerByID ( cmpOwnership . GetOwner ( ) ) , IID _Player ) ;
2010-03-12 22:41:40 +01:00
cmpPlayer . AddResource ( type . generic , status . amount ) ;
2010-07-21 18:09:58 +02:00
// Tell the target we're gathering from it
Engine . PostMessage ( target , MT _ResourceGather ,
{ "entity" : target , "gatherer" : this . entity } ) ;
2010-02-12 23:46:53 +01:00
return status ;
} ;
2010-07-08 22:08:08 +02:00
/ * *
* Compute the amount of resources collected per second from the target .
* Returns 0 if resources cannot be collected ( e . g . the target doesn ' t
* exist , or is the wrong type ) .
* /
ResourceGatherer . prototype . GetTargetGatherRate = function ( target )
{
var cmpResourceSupply = Engine . QueryInterface ( target , IID _ResourceSupply ) ;
if ( ! cmpResourceSupply )
return 0 ;
var type = cmpResourceSupply . GetType ( ) ;
var rate ;
if ( type . specific && this . template . Rates [ type . generic + "." + type . specific ] )
rate = this . template . Rates [ type . generic + "." + type . specific ] ;
else
rate = this . template . Rates [ type . generic ] ;
return ( rate || 0 ) * this . template . BaseSpeed ;
}
2010-02-12 23:46:53 +01:00
Engine . RegisterComponentType ( IID _ResourceGatherer , "ResourceGatherer" , ResourceGatherer ) ;