2013-06-09 3 views
-2

У меня есть корпус и щит, и я хочу, чтобы он был поврежден, чтобы получить урон и вычесть его из щита сначала, а затем, когда он пуст, чтобы взять с корпуса. Но если у щита было 100 левых и урон был 400, то корпус, если он начинался с 1000, составлял 700.Вычитание из здоровья

Здесь, что мне удалось сделать, часть щита работает, но алгоритм корпуса слишком сложно понять.

Player.prototype.deductHealth = function(damage) 
{ 

    var shield = (this.PlayerShield - damage); 
    var remainder = (damage - this.PlayerShield); 
    var hull = this.PlayerHull; 

    if(this.PlayerShield < 0) 
    { 

    hull = (this.PlayerHull - remainder); 
    } 

    if(hull <=0) 
    { 
    hull = 0; 
    shield = 0; 
    } 

    this.PlayerHull = hull; 
    this.PlayerShield = shield; 

} 
+1

Я думаю, что этот вопрос лучше подходит для http://codereview.stackexchange.com –

+0

Если вы моделируя EVE, затем обратите внимание, что любой ущерб, нанесенный повреждениям на экранах, игнорируется, за исключением того, что истекает кровянистая способность, которая зависит от навыков и оборудования. –

ответ

0

Я не могу проверить это сейчас, но что-то вроде этого, я думаю, может работать.

Player.prototype.deductHealth = function(damage) 
    { 

     var shield = (this.PlayerShield - damage); 
     var remainder = 0; 
     if (shield<0) { 
      remainder=-shield; 
      shield=0; 
     } 

     var hull = this.PlayerHull;   
     if(remainder > 0) 
     {   
     hull = (this.PlayerHull - remainder); 
     } 

     if(hull <=0) 
     { 
      hull = 0; 
      shield = 0; 
     } 

     this.PlayerHull = hull; 
     this.PlayerShield = shield; 

    } 
0

Хм ... попробовать это, я думаю, что вы быть_наст проблемы, потому что вы смешение ссылки локального ВАРА, которые представляют собой корпус и щит силу, и ваши ВАРОВ-членов, которые представляют собой корпус и экран. Мое предложение заключается в использовании только вары члена так:

Player.prototype.deductHealth = function(damage) 
{ 

    var shieldOverkill = (damage - this.PlayerShield); 
    this.PlayerShield = this.PlayerShield - damage; 

    if (shieldOverkill > 0) 
    { 
     this.PlayerHull = this.PlayerHull - shieldOverkill; 
    } 

    if(this.PlayerHull < 0) 
    { 
     this.PlayerHull= 0; 
    } 
    if (this.PlayerShield < 0) 
    { 
     this.PlayerShield = 0; 
    } 

}

0

Так давайте пройдем через этот шаг за шагом:

Player.protoype.deductHealth = function (damage) { 
    this.PlayerShield -= damage; // First deduct the damage from the shields: 
    if (this.PlayerShield < 0) { // If shields are negative 
    // Take the damage not absorbed by shields from hull; 
    this.PlayerHull += this.PlayerShield; // note we use + since shields are negative 
    this.PlayerShield = 0; // Set shields to 0 
    if (this.PlayerHull < 0) { // If hull now negative 
     this.PlayerHull = 0; // set to zero. 
    } 
    } 
} 

более заблаговременной версией, используя более подходящие названия:

Player.prototype.takeHit (damage) { 
    if ((this.shields -= damage) < 0) { // Shields exhausted ? 
    if ((this.hull += this.shields) < 0) { // Hull exhausted? 
     this.hull = 0; 
    } 
    this.shields = 0; 
    } 
} 
Смежные вопросы