Handle updates for foundation progress, resource carrying and resource supply amount for AIProxy. Fixes #1327

This was SVN commit r11547.
This commit is contained in:
Jonathan Waller 2012-04-18 11:30:28 +00:00
parent 4b4411116f
commit 13281e230d
5 changed files with 42 additions and 2 deletions

View File

@ -125,7 +125,7 @@ AIProxy.prototype.OnTrainingQueueChanged = function(msg)
var cmpTrainingQueue = Engine.QueryInterface(this.entity, IID_TrainingQueue);
this.changes.trainingQueue = cmpTrainingQueue.GetQueue();
}
};
AIProxy.prototype.OnGarrisonedUnitsChanged = function(msg)
{
@ -133,7 +133,25 @@ AIProxy.prototype.OnGarrisonedUnitsChanged = function(msg)
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
this.changes.garrisoned = cmpGarrisonHolder.GetEntities();
}
};
AIProxy.prototype.OnResourceSupplyChanged = function(msg)
{
this.NotifyChange();
this.changes.resourceSupplyAmount = msg.to;
};
AIProxy.prototype.OnResourceCarryingChanged = function(msg)
{
this.NotifyChange();
this.changes.resourceCarrying = msg.to;
};
AIProxy.prototype.OnFoundationProgressChanged = function(msg)
{
this.NotifyChange();
this.changes.foundationProgress = msg.to;
};
// TODO: event handlers for all the other things
@ -198,24 +216,28 @@ AIProxy.prototype.GetFullRepresentation = function()
var cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
if (cmpFoundation)
{
// Updated by OnFoundationProgressChanged
ret.foundationProgress = cmpFoundation.GetBuildPercentage();
}
var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
if (cmpResourceSupply)
{
// Updated by OnResourceSupplyChanged
ret.resourceSupplyAmount = cmpResourceSupply.GetCurrentAmount();
}
var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
if (cmpResourceGatherer)
{
// Updated by OnResourceCarryingChanged
ret.resourceCarrying = cmpResourceGatherer.GetCarryingStatus();
}
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
if (cmpGarrisonHolder)
{
// Updated by OnGarrisonedUnitsChanged
ret.garrisoned = cmpGarrisonHolder.GetEntities();
}

View File

@ -158,6 +158,8 @@ Foundation.prototype.Build = function(builderEnt, work)
this.buildProgress += amount;
if (this.buildProgress > 1.0)
this.buildProgress = 1.0;
Engine.PostMessage(this.entity, MT_FoundationProgressChanged, { "to": this.GetBuildPercentage() });
// Add an appropriate proportion of hitpoints
var targetHP = Math.max(0, Math.min(this.maxHitpoints, Math.floor(this.maxHitpoints * this.buildProgress)));

View File

@ -95,6 +95,8 @@ ResourceGatherer.prototype.GiveResources = function(resources)
{
this.carrying[resource.type] = +(resource.amount);
}
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() });
};
/**
@ -193,6 +195,8 @@ ResourceGatherer.prototype.PerformGather = function(target)
var cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
if (cmpStatisticsTracker)
cmpStatisticsTracker.IncreaseResourceGatheredCounter(type.generic, status.amount, type.specific);
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() });
// Tell the target we're gathering from it
Engine.PostMessage(target, MT_ResourceGather,
@ -269,6 +273,8 @@ ResourceGatherer.prototype.CommitResources = function(types)
delete this.carrying[type];
}
}
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() });
};
/**
@ -279,6 +285,8 @@ ResourceGatherer.prototype.CommitResources = function(types)
ResourceGatherer.prototype.DropResources = function()
{
this.carrying = {};
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() });
};
Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer);

View File

@ -5,3 +5,7 @@ Engine.RegisterInterface("Foundation");
// Units can watch for this and change task once it's complete.
// Data: { entity: 123, newentity: 234 }
Engine.RegisterMessageType("ConstructionFinished");
// Message of the form { "to", 59 }, as the percentage complete
// sent whenever the foundations progress changes.
Engine.RegisterMessageType("FoundationProgressChanged");

View File

@ -3,3 +3,7 @@ Engine.RegisterInterface("ResourceGatherer");
// Message sent from ResourceGatherers to a ResourceSupply entity
// each time they gather resources from it
Engine.RegisterMessageType("ResourceGather");
// Message of the form { "to", [ {"type":"wood", "amount":7, "max":10} ] },
// sent whenever carrying amount changes.
Engine.RegisterMessageType("ResourceCarryingChanged");