Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/Formation.js
Show First 20 Lines • Show All 304 Lines • ▼ Show 20 Lines | Formation.prototype.SetMembers = function(ents) | ||||
// Compute the speed etc. of the formation | // Compute the speed etc. of the formation | ||||
this.ComputeMotionParameters(); | this.ComputeMotionParameters(); | ||||
}; | }; | ||||
/** | /** | ||||
* Remove the given list of entities. | * Remove the given list of entities. | ||||
* The entities must already be members of this formation. | * The entities must already be members of this formation. | ||||
* @param {boolean} rename - Whether the removal was part of an entity rename | |||||
(prevents disbanding of the formation when under the member limit). | |||||
*/ | */ | ||||
Formation.prototype.RemoveMembers = function(ents) | Formation.prototype.RemoveMembers = function(ents, renamed = false) | ||||
{ | { | ||||
this.offsets = undefined; | this.offsets = undefined; | ||||
this.members = this.members.filter(function(e) { return ents.indexOf(e) == -1; }); | this.members = this.members.filter(function(e) { return ents.indexOf(e) == -1; }); | ||||
this.inPosition = this.inPosition.filter(function(e) { return ents.indexOf(e) == -1; }); | this.inPosition = this.inPosition.filter(function(e) { return ents.indexOf(e) == -1; }); | ||||
for (var ent of ents) | for (var ent of ents) | ||||
{ | { | ||||
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | ||||
Show All 9 Lines | for (var ent of this.formationMembersWithAura) | ||||
// the unit with the aura is also removed from the formation | // the unit with the aura is also removed from the formation | ||||
if (ents.indexOf(ent) !== -1) | if (ents.indexOf(ent) !== -1) | ||||
cmpAuras.RemoveFormationAura(this.members); | cmpAuras.RemoveFormationAura(this.members); | ||||
} | } | ||||
this.formationMembersWithAura = this.formationMembersWithAura.filter(function(e) { return ents.indexOf(e) == -1; }); | this.formationMembersWithAura = this.formationMembersWithAura.filter(function(e) { return ents.indexOf(e) == -1; }); | ||||
// If there's nobody left, destroy the formation | // If there's nobody left, destroy the formation | ||||
if (this.members.length == 0) | // unless this is a rename where we can have 0 members temporarily. | ||||
if (this.members.length == 0 && !renamed) | |||||
{ | { | ||||
Engine.DestroyEntity(this.entity); | Engine.DestroyEntity(this.entity); | ||||
return; | return; | ||||
} | } | ||||
this.ComputeMotionParameters(); | this.ComputeMotionParameters(); | ||||
if (!this.rearrange) | if (!this.rearrange) | ||||
Show All 15 Lines | Formation.prototype.AddMembers = function(ents) | ||||
} | } | ||||
this.members = this.members.concat(ents); | this.members = this.members.concat(ents); | ||||
for (let ent of ents) | for (let ent of ents) | ||||
{ | { | ||||
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | ||||
cmpUnitAI.SetFormationController(this.entity); | cmpUnitAI.SetFormationController(this.entity); | ||||
if (!cmpUnitAI.GetOrders().length) | |||||
cmpUnitAI.SetNextState("FORMATIONMEMBER.IDLE"); | |||||
let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | ||||
if (cmpAuras && cmpAuras.HasFormationAura()) | if (cmpAuras && cmpAuras.HasFormationAura()) | ||||
{ | { | ||||
this.formationMembersWithAura.push(ent); | this.formationMembersWithAura.push(ent); | ||||
cmpAuras.ApplyFormationAura(this.members); | cmpAuras.ApplyFormationAura(this.members); | ||||
} | } | ||||
} | } | ||||
this.ComputeMotionParameters(); | this.ComputeMotionParameters(); | ||||
if (!this.rearrange) | |||||
return; | |||||
this.MoveMembersIntoFormation(true, true, this.lastOrderVariant); | this.MoveMembersIntoFormation(true, true, this.lastOrderVariant); | ||||
}; | }; | ||||
/** | /** | ||||
* Called when the formation stops moving in order to detect | * Called when the formation stops moving in order to detect | ||||
* units that have already reached their final positions. | * units that have already reached their final positions. | ||||
*/ | */ | ||||
Formation.prototype.FindInPosition = function() | Formation.prototype.FindInPosition = function() | ||||
▲ Show 20 Lines • Show All 536 Lines • ▼ Show 20 Lines | Formation.prototype.OnGlobalOwnershipChanged = function(msg) | ||||
// controlled by this formation | // controlled by this formation | ||||
if (this.members.indexOf(msg.entity) != -1) | if (this.members.indexOf(msg.entity) != -1) | ||||
this.RemoveMembers([msg.entity]); | this.RemoveMembers([msg.entity]); | ||||
}; | }; | ||||
Formation.prototype.OnGlobalEntityRenamed = function(msg) | Formation.prototype.OnGlobalEntityRenamed = function(msg) | ||||
{ | { | ||||
if (this.members.indexOf(msg.entity) != -1) | if (this.members.indexOf(msg.entity) == -1) | ||||
{ | return; | ||||
this.offsets = undefined; | |||||
var cmpNewUnitAI = Engine.QueryInterface(msg.newentity, IID_UnitAI); | |||||
if (cmpNewUnitAI) | |||||
{ | |||||
this.members[this.members.indexOf(msg.entity)] = msg.newentity; | |||||
this.memberPositions[msg.newentity] = this.memberPositions[msg.entity]; | |||||
} | |||||
var cmpOldUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI); | |||||
cmpOldUnitAI.SetFormationController(INVALID_ENTITY); | |||||
if (cmpNewUnitAI) | // Save rearranging to temporarily set it to false. | ||||
{ | let temp = this.rearrange; | ||||
cmpNewUnitAI.SetFormationController(this.entity); | this.rearrange = false; | ||||
if (!cmpNewUnitAI.GetOrders().length) | |||||
cmpNewUnitAI.SetNextState("FORMATIONMEMBER.IDLE"); | // First remove the old member to be able to reuse its position. | ||||
} | this.RemoveMembers([msg.entity], true); | ||||
this.AddMembers([msg.newentity]); | |||||
this.memberPositions[msg.newentity] = this.memberPositions[msg.entity]; | |||||
// Because the renamed entity might have different characteristics, | this.rearrange = temp; | ||||
// (e.g. packed vs. unpacked siege), we need to recompute motion parameters | |||||
this.ComputeMotionParameters(); | |||||
} | |||||
}; | }; | ||||
Formation.prototype.RegisterTwinFormation = function(entity) | Formation.prototype.RegisterTwinFormation = function(entity) | ||||
{ | { | ||||
var cmpFormation = Engine.QueryInterface(entity, IID_Formation); | var cmpFormation = Engine.QueryInterface(entity, IID_Formation); | ||||
if (!cmpFormation) | if (!cmpFormation) | ||||
return; | return; | ||||
this.twinFormations.push(entity); | this.twinFormations.push(entity); | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator