Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Promotion.js
Show All 25 Lines | |||||
Promotion.prototype.GetPromotedTemplateName = function() | Promotion.prototype.GetPromotedTemplateName = function() | ||||
{ | { | ||||
return this.template.Entity; | return this.template.Entity; | ||||
}; | }; | ||||
Promotion.prototype.Promote = function(promotedTemplateName) | Promotion.prototype.Promote = function(promotedTemplateName) | ||||
{ | { | ||||
// If the unit is dead, don't promote it | // If the unit is dead, don't promote it | ||||
var cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health); | let cmpCurrentUnitHealth = Engine.QueryInterface(this.entity, IID_Health); | ||||
if (cmpCurrentUnitHealth.GetHitpoints() == 0) | if (cmpCurrentUnitHealth.GetHitpoints() == 0) | ||||
return; | return; | ||||
// Create promoted unit entity | // Create promoted unit entity | ||||
var promotedUnitEntity = Engine.AddEntity(promotedTemplateName); | let promotedUnitEntity = Engine.AddEntity(promotedTemplateName); | ||||
// Copy parameters from current entity to promoted one | // Copy parameters from current entity to promoted one | ||||
var cmpCurrentUnitPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpCurrentUnitPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
var cmpPromotedUnitPosition = Engine.QueryInterface(promotedUnitEntity, IID_Position); | let cmpPromotedUnitPosition = Engine.QueryInterface(promotedUnitEntity, IID_Position); | ||||
if (cmpCurrentUnitPosition.IsInWorld()) | if (cmpCurrentUnitPosition.IsInWorld()) | ||||
{ | { | ||||
var pos = cmpCurrentUnitPosition.GetPosition2D(); | let pos = cmpCurrentUnitPosition.GetPosition2D(); | ||||
cmpPromotedUnitPosition.JumpTo(pos.x, pos.y); | cmpPromotedUnitPosition.JumpTo(pos.x, pos.y); | ||||
} | } | ||||
var rot = cmpCurrentUnitPosition.GetRotation(); | let rot = cmpCurrentUnitPosition.GetRotation(); | ||||
cmpPromotedUnitPosition.SetYRotation(rot.y); | cmpPromotedUnitPosition.SetYRotation(rot.y); | ||||
cmpPromotedUnitPosition.SetXZRotation(rot.x, rot.z); | cmpPromotedUnitPosition.SetXZRotation(rot.x, rot.z); | ||||
var heightOffset = cmpCurrentUnitPosition.GetHeightOffset(); | let heightOffset = cmpCurrentUnitPosition.GetHeightOffset(); | ||||
cmpPromotedUnitPosition.SetHeightOffset(heightOffset); | cmpPromotedUnitPosition.SetHeightOffset(heightOffset); | ||||
var cmpCurrentUnitOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | let cmpCurrentUnitOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
var cmpPromotedUnitOwnership = Engine.QueryInterface(promotedUnitEntity, IID_Ownership); | let cmpPromotedUnitOwnership = Engine.QueryInterface(promotedUnitEntity, IID_Ownership); | ||||
cmpPromotedUnitOwnership.SetOwner(cmpCurrentUnitOwnership.GetOwner()); | cmpPromotedUnitOwnership.SetOwner(cmpCurrentUnitOwnership.GetOwner()); | ||||
// change promoted unit health to the same percent of hitpoints as unit had before promotion | // change promoted unit health to the same percent of hitpoints as unit had before promotion | ||||
var cmpPromotedUnitHealth = Engine.QueryInterface(promotedUnitEntity, IID_Health); | let cmpPromotedUnitHealth = Engine.QueryInterface(promotedUnitEntity, IID_Health); | ||||
var healthFraction = Math.max(0, Math.min(1, cmpCurrentUnitHealth.GetHitpoints() / cmpCurrentUnitHealth.GetMaxHitpoints())); | let healthFraction = Math.max(0, Math.min(1, cmpCurrentUnitHealth.GetHitpoints() / cmpCurrentUnitHealth.GetMaxHitpoints())); | ||||
var promotedUnitHitpoints = cmpPromotedUnitHealth.GetMaxHitpoints() * healthFraction; | let promotedUnitHitpoints = cmpPromotedUnitHealth.GetMaxHitpoints() * healthFraction; | ||||
cmpPromotedUnitHealth.SetHitpoints(promotedUnitHitpoints); | cmpPromotedUnitHealth.SetHitpoints(promotedUnitHitpoints); | ||||
var cmpPromotedUnitPromotion = Engine.QueryInterface(promotedUnitEntity, IID_Promotion); | let cmpPromotedUnitPromotion = Engine.QueryInterface(promotedUnitEntity, IID_Promotion); | ||||
if (cmpPromotedUnitPromotion) | if (cmpPromotedUnitPromotion) | ||||
cmpPromotedUnitPromotion.IncreaseXp(this.currentXp); | cmpPromotedUnitPromotion.IncreaseXp(this.currentXp); | ||||
var cmpCurrentUnitResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | let cmpCurrentUnitResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | ||||
var cmpPromotedUnitResourceGatherer = Engine.QueryInterface(promotedUnitEntity, IID_ResourceGatherer); | let cmpPromotedUnitResourceGatherer = Engine.QueryInterface(promotedUnitEntity, IID_ResourceGatherer); | ||||
if (cmpCurrentUnitResourceGatherer && cmpPromotedUnitResourceGatherer) | if (cmpCurrentUnitResourceGatherer && cmpPromotedUnitResourceGatherer) | ||||
{ | { | ||||
var carriedResorces = cmpCurrentUnitResourceGatherer.GetCarryingStatus(); | let carriedResorces = cmpCurrentUnitResourceGatherer.GetCarryingStatus(); | ||||
cmpPromotedUnitResourceGatherer.GiveResources(carriedResorces); | cmpPromotedUnitResourceGatherer.GiveResources(carriedResorces); | ||||
} | } | ||||
var cmpCurrentUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | let cmpCurrentUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | ||||
var cmpPromotedUnitAI = Engine.QueryInterface(promotedUnitEntity, IID_UnitAI); | let cmpPromotedUnitAI = Engine.QueryInterface(promotedUnitEntity, IID_UnitAI); | ||||
var heldPos = cmpCurrentUnitAI.GetHeldPosition(); | let heldPos = cmpCurrentUnitAI.GetHeldPosition(); | ||||
if (heldPos) | if (heldPos) | ||||
cmpPromotedUnitAI.SetHeldPosition(heldPos.x, heldPos.z); | cmpPromotedUnitAI.SetHeldPosition(heldPos.x, heldPos.z); | ||||
if (cmpCurrentUnitAI.GetStanceName()) | if (cmpCurrentUnitAI.GetStanceName()) | ||||
cmpPromotedUnitAI.SwitchToStance(cmpCurrentUnitAI.GetStanceName()); | cmpPromotedUnitAI.SwitchToStance(cmpCurrentUnitAI.GetStanceName()); | ||||
var orders = cmpCurrentUnitAI.GetOrders(); | let orders = cmpCurrentUnitAI.GetOrders(); | ||||
if (cmpCurrentUnitPosition.IsInWorld()) // do not cheer if not visibly garrisoned | |||||
cmpPromotedUnitAI.Cheer(); | |||||
if (cmpCurrentUnitAI.IsGarrisoned()) | if (cmpCurrentUnitAI.IsGarrisoned()) | ||||
cmpPromotedUnitAI.SetGarrisoned(); | cmpPromotedUnitAI.SetGarrisoned(); | ||||
cmpPromotedUnitAI.AddOrders(orders); | cmpPromotedUnitAI.AddOrders(orders); | ||||
var workOrders = cmpCurrentUnitAI.GetWorkOrders(); | let workOrders = cmpCurrentUnitAI.GetWorkOrders(); | ||||
cmpPromotedUnitAI.SetWorkOrders(workOrders); | cmpPromotedUnitAI.SetWorkOrders(workOrders); | ||||
if (cmpCurrentUnitAI.IsGuardOf()) | if (cmpCurrentUnitAI.IsGuardOf()) | ||||
{ | { | ||||
let guarded = cmpCurrentUnitAI.IsGuardOf(); | let guarded = cmpCurrentUnitAI.IsGuardOf(); | ||||
let cmpGuard = Engine.QueryInterface(guarded, IID_Guard); | let cmpGuard = Engine.QueryInterface(guarded, IID_Guard); | ||||
if (cmpGuard) | if (cmpGuard) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator