2010-01-24 18:24:35 +01:00
function Builder ( ) { }
2010-04-09 21:02:39 +02:00
Builder . prototype . Schema =
2010-04-23 18:09:03 +02:00
"<a:help>Allows the unit to construct and repair buildings.</a:help>" +
"<a:example>" +
"<Rate>1.0</Rate>" +
2010-07-03 03:23:23 +02:00
"<Entities datatype='tokens'>" +
2010-04-23 18:09:03 +02:00
"\n structures/{civ}_barracks\n structures/{civ}_civil_centre\n structures/celt_sb1\n " +
"</Entities>" +
"</a:example>" +
"<element name='Rate' a:help='Construction speed multiplier (1.0 is normal speed, higher values are faster)'>" +
2010-04-09 21:02:39 +02:00
"<ref name='positiveDecimal'/>" +
2010-04-23 18:09:03 +02:00
"</element>" +
"<element name='Entities' a:help='Space-separated list of entity template names that this unit can build. The special string \"{civ}\" will be automatically replaced by the unit's four-character civ code'>" +
2010-07-03 03:23:23 +02:00
"<attribute name='datatype'>" +
"<value>tokens</value>" +
"</attribute>" +
2010-04-23 18:09:03 +02:00
"<text/>" +
2010-04-09 21:02:39 +02:00
"</element>" ;
2010-01-24 18:24:35 +01:00
Builder . prototype . Init = function ( )
{
} ;
2011-03-05 23:30:32 +01:00
Builder . prototype . Serialize = null ; // we have no dynamic state to save
2010-01-24 18:24:35 +01:00
Builder . prototype . GetEntitiesList = function ( )
{
2010-07-03 03:23:23 +02:00
var string = this . template . Entities . _string ;
2010-01-24 18:24:35 +01:00
// Replace the "{civ}" codes with this entity's civ ID
var cmpIdentity = Engine . QueryInterface ( this . entity , IID _Identity ) ;
if ( cmpIdentity )
string = string . replace ( /\{civ\}/g , cmpIdentity . GetCiv ( ) ) ;
return string . split ( /\s+/ ) ;
} ;
2010-03-12 22:41:40 +01:00
Builder . prototype . GetRange = function ( )
{
2010-04-30 01:36:05 +02:00
return { "max" : 2 , "min" : 0 } ;
2010-03-12 22:41:40 +01:00
// maybe this should depend on the unit or target or something?
}
/ * *
* Build / repair the target entity . This should only be called after a successful range check .
* It should be called at a rate of once per second .
2010-07-21 18:09:58 +02:00
* Returns obj with obj . finished == true if this is a repair and it ' s fully repaired .
2010-03-12 22:41:40 +01:00
* /
Builder . prototype . PerformBuilding = function ( target )
{
2010-03-12 23:28:51 +01:00
var rate = + this . template . Rate ;
2010-03-12 22:41:40 +01:00
// If it's a foundation, then build it
var cmpFoundation = Engine . QueryInterface ( target , IID _Foundation ) ;
if ( cmpFoundation )
{
2010-07-21 18:09:58 +02:00
cmpFoundation . Build ( this . entity , rate ) ;
2010-08-21 23:53:51 +02:00
return ;
2010-03-12 22:41:40 +01:00
}
2010-08-21 23:53:51 +02:00
// Otherwise try to repair it
var cmpHealth = Engine . QueryInterface ( target , IID _Health ) ;
if ( cmpHealth )
{
cmpHealth . Repair ( this . entity , rate ) ;
return ;
2010-03-12 22:41:40 +01:00
}
} ;
2010-01-24 18:24:35 +01:00
Engine . RegisterComponentType ( IID _Builder , "Builder" , Builder ) ;