Ticket #1853: Foundation.patch

File Foundation.patch, 4.5 KB (added by Manuel Núñez, 11 years ago)
  • binaries/data/mods/public/simulation/components/Foundation.js

     
    1212    // its obstruction once there's nothing in the way.
    1313    this.committed = false;
    1414
    15     this.buildProgress = 0.0; // 0 <= progress <= 1
    16 
    1715    // Set up a timer so we can count the number of builders in a 1-second period.
    1816    // (We assume each builder only builds once per second, which is what UnitAI
    1917    // implements.)
     
    3735
    3836Foundation.prototype.InitialiseConstruction = function(owner, template)
    3937{
    40     var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    41     this.addedHitpoints = cmpHealth.GetHitpoints();
    42 
    4338    this.finalTemplateName = template;
    4439
    4540    // We need to know the owner in OnDestroy, but at that point the entity has already been
     
    5449    this.initialised = true;
    5550};
    5651
     52/**
     53 * Moving the revelation logic from Build to here makes the building sink if
     54 * it is attacked.
     55 */
     56Foundation.prototype.OnHealthChanged = function(msg)
     57{
     58    // Gradually reveal the final building preview
     59    var cmpPreviewVisual = Engine.QueryInterface(this.previewEntity, IID_Visual);
     60    if (cmpPreviewVisual)
     61        cmpPreviewVisual.SetConstructionProgress(this.GetBuildProgress());
     62       
     63    Engine.PostMessage(this.entity, MT_FoundationProgressChanged, { "to": this.GetBuildPercentage() });
     64};
     65
     66/**
     67 * Returns the current build progress in a [0,1] range.
     68 */
     69Foundation.prototype.GetBuildProgress = function()
     70{
     71    var cmpHealth = Engine.QueryInterface(this.entity, IID_Health)
     72    var hitpoints = cmpHealth.GetHitpoints();
     73    var maxHitpoints = cmpHealth.GetMaxHitpoints();
     74   
     75    return (hitpoints / maxHitpoints);
     76};
     77
    5778Foundation.prototype.GetBuildPercentage = function()
    5879{
    59     return Math.floor(this.buildProgress * 100);
     80    return Math.floor(this.GetBuildProgress() * 100);
    6081};
    6182
    6283Foundation.prototype.IsFinished = function()
    6384{
    64     return (this.buildProgress >= 1.0);
     85    return (this.GetBuildProgress() == 1.0);
    6586};
    6687
    6788Foundation.prototype.OnDestroy = function()
     
    7798        this.previewEntity = INVALID_ENTITY;
    7899    }
    79100
    80     if (this.buildProgress == 1.0)
     101    if (this.IsFinished())
    81102        return;
    82103
    83104    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     
    85106
    86107    for (var r in this.costs)
    87108    {
    88         var scaled = Math.floor(this.costs[r] * (1.0 - this.buildProgress));
     109        var scaled = Math.floor(this.costs[r] * (1.0 - this.GetBuildProgress()));
    89110        if (scaled)
    90111        {
    91112            cmpPlayer.AddResource(r, scaled);
     
    115136        this.numRecentBuilders = this.recentBuilders.length;
    116137        this.SetBuildMultiplier();
    117138    }
    118 }
     139};
    119140
    120141/**
    121142 * Sets the build rate multiplier, which is applied to all builders.
     
    124145{
    125146    // Yields a total rate of construction equal to numRecentBuilders^0.7
    126147    this.buildMultiplier = Math.pow(this.numRecentBuilders, 0.7) / this.numRecentBuilders;
    127 }
     148};
    128149
    129150/**
    130151 * Perform some number of seconds of construction work.
     
    135156    // Do nothing if we've already finished building
    136157    // (The entity will be destroyed soon after completion so
    137158    // this won't happen much)
    138     if (this.buildProgress == 1.0)
     159    if (this.GetBuildProgress() == 1.0)
    139160        return;
    140 
     161       
    141162    // Handle the initial 'committing' of the foundation
    142163    if (!this.committed)
    143164    {
     
    223244    // Record this builder so we can count the total number
    224245    this.AddBuilder(builderEnt);
    225246
    226     this.buildProgress += amount * this.buildMultiplier;
    227     if (this.buildProgress > 1.0)
    228         this.buildProgress = 1.0;
    229 
    230     Engine.PostMessage(this.entity, MT_FoundationProgressChanged, { "to": this.GetBuildPercentage() });
    231 
    232     // Gradually reveal the final building preview
    233     var cmpPreviewVisual = Engine.QueryInterface(this.previewEntity, IID_Visual);
    234     if (cmpPreviewVisual)
    235         cmpPreviewVisual.SetConstructionProgress(this.buildProgress);
    236 
    237247    // Add an appropriate proportion of hitpoints
    238248    var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    239249    var maxHealth = cmpHealth.GetMaxHitpoints();
    240     var targetHP = Math.max(0, Math.min(maxHealth, Math.floor(maxHealth * this.buildProgress)));
    241     var deltaHP = targetHP - this.addedHitpoints;
     250    var deltaHP = Math.max(0, Math.min(maxHealth, Math.floor(maxHealth * amount)));
    242251    if (deltaHP > 0)
    243252    {
    244253        cmpHealth.Increase(deltaHP);
    245         this.addedHitpoints += deltaHP;
    246254    }
    247255
    248     if (this.buildProgress >= 1.0)
     256    if (this.GetBuildProgress() >= 1.0)
    249257    {
    250258        // Finished construction
    251259