2014-01-11 13:19:43 +01:00
|
|
|
function FormationAttack() {}
|
|
|
|
|
|
|
|
FormationAttack.prototype.Schema =
|
2014-01-21 10:10:17 +01:00
|
|
|
"<element name='CanAttackAsFormation'>" +
|
|
|
|
"<text/>" +
|
|
|
|
"</element>";
|
2014-01-11 13:19:43 +01:00
|
|
|
|
|
|
|
FormationAttack.prototype.Init = function()
|
|
|
|
{
|
2014-01-21 10:10:17 +01:00
|
|
|
this.canAttackAsFormation = this.template.CanAttackAsFormation == "true";
|
2014-01-11 13:19:43 +01:00
|
|
|
};
|
|
|
|
|
2014-01-21 17:50:58 +01:00
|
|
|
FormationAttack.prototype.CanAttackAsFormation = function()
|
|
|
|
{
|
|
|
|
return this.canAttackAsFormation;
|
|
|
|
};
|
|
|
|
|
2014-01-11 13:19:43 +01:00
|
|
|
FormationAttack.prototype.GetRange = function(target)
|
|
|
|
{
|
2014-01-21 10:10:17 +01:00
|
|
|
var result = {"min": 0, "max": this.canAttackAsFormation ? -1 : 0};
|
2014-01-11 13:19:43 +01:00
|
|
|
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
|
|
|
|
if (!cmpFormation)
|
|
|
|
{
|
|
|
|
warn("FormationAttack component used on a non-formation entity");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
var members = cmpFormation.GetMembers();
|
|
|
|
for each (var ent in members)
|
|
|
|
{
|
|
|
|
var cmpAttack = Engine.QueryInterface(ent, IID_Attack);
|
|
|
|
if (!cmpAttack)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var type = cmpAttack.GetBestAttackAgainst(target);
|
|
|
|
if (!type)
|
|
|
|
continue;
|
|
|
|
|
2014-01-21 10:10:17 +01:00
|
|
|
// if the formation can attack, take the minimum max range (so units are certainly in range),
|
|
|
|
// If the formation can't attack, take the maximum max range as the point where the formation will be disbanded
|
|
|
|
// Always take the minimum min range (to not get impossible situations)
|
2014-01-11 13:19:43 +01:00
|
|
|
var range = cmpAttack.GetRange(type);
|
2014-01-21 10:10:17 +01:00
|
|
|
|
|
|
|
if (this.canAttackAsFormation)
|
|
|
|
{
|
|
|
|
if (range.max < result.max || result.max < 0)
|
|
|
|
result.max = range.max;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (range.max > result.max || range.max < 0)
|
|
|
|
result.max = range.max;
|
|
|
|
}
|
2014-01-11 13:19:43 +01:00
|
|
|
if (range.min < result.min)
|
|
|
|
result.min = range.min;
|
|
|
|
}
|
|
|
|
// add half the formation size, so it counts as the range for the units on the first row
|
|
|
|
var extraRange = cmpFormation.GetSize().depth/2;
|
|
|
|
|
|
|
|
if (result.max >= 0)
|
|
|
|
result.max += extraRange;
|
|
|
|
result.min += extraRange;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.RegisterComponentType(IID_Attack, "FormationAttack", FormationAttack);
|