这很难解释我想要什么,所以我会尽我所能。
我有一个游戏,游戏中的玩家有健康计数器(hp)和保镖。每个保镖都有100马力,一旦所有球员保镖都消失了,伤害就会从他们的马力上消失。
所以,这些是我的价值观:
$dmg // the damage
$bgs // the amount of bodyguards the player has
$bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at
$hp // the players health
当他们被枪杀时,需要检查他们是否有$ bgs。这是一个基本的例子。
if ($bgs > 0) {
$bgs_hp = $bgs_hp - $dmg;
if ($bg_hp <= 0) { $bg = $bg - 1; $bg_hp = 100; }
} else {
$hp = $hp - $dmg;
}
无论如何,我需要帮助的部分就是这个。我希望伤害重叠。 所以说$ dmg是200,会杀死2个保镖(每个都有100马力)。或者有人可以射击所有他们的保镖,剩下的$ dmg也从$ hp中脱落。
我需要帮助的原因是因为我在数学方面很糟糕,而且我需要使用一些MOD函数或者某些东西。
实例;
1)球员有3名保镖。有人向他射击150 dmg。它会杀死1名保镖,并对下一次造成50点伤害。
2)玩家有一个半保镖(50)的保镖。有人向他射击160,它会杀死保镖(50hp),其余的伤害(110)也会杀死玩家。
答案 0 :(得分:1)
我只是保留一个带有每个保镖的hps的数组,如下所示:
$bgs = array(100); // one bg with 100 hp
或
$bgs = array(100, 300, 50); // one bg with 100 hp, then one with 300, etc
如果你想知道有多少保镖,一个简单的count($bgs)
会告诉你。
这将使您将来更容易扩展游戏,并且还可以更轻松地解决这些类型的计算。你现在可以写:
$hp = 100;
$bgs = array(100, 100);
$damage = 101; // for example
// As long as there's still damage to take and bgs to take it:
while($damage && !empty($bgs)) {
$soak = min($bgs[0], $damage); // not more than the first bg can take
$bgs[0] -= $soak; // remove hps from the first bg
$damage -= $soak; // deduct from the amount of damage to tage
if ($bgs[0] == 0) {
// bodyguard dead, remove him from the array
array_shift($bgs);
}
}
// If there's any damage left over, it goes to hp
$hp -= $damage;
<强> See it in action 强>
更新:这是适用于您当前方案的代码。它并不简单,而且对你能做的事情有更多的限制:
$max_bg_health = 100;
$hp = 100;
$bgs = 2;
$bg_hp = $max_bg_health; // so that makes 2 "full health" bgs
$damage = 101; // for example
if($bgs) {
$bg_capacity = ($bgs - 1) * $max_bg_health + $bg_hp;
$soak = min($bg_capacity, $damage);
$bg_capacity -= $soak;
$damage -= $soak;
// update bodyguard status; this is extra tricky because $bgs is not
// "the amount of full hp bgs" (which would make it a lot simpler)
// but "the amount of bgs including the one which may not have full hp"
$bgs = $bg_capacity ? intval($bg_capacity - 1 / $max_bg_health) + 1 : 0;
$bg_hp = $bg_capacity % $max_bg_health;
}
// If there's any damage left over, it goes to hp
$hp -= $damage;
答案 1 :(得分:0)
一种可能性是,仅存储总健康点(即您和保镖的健康点)。然后,您可以在每次需要时计算您的健康点数和保镖数量(因此您无需保存)
$totalhp = 499; // you have 100 health and 4 bodyguards, the last one has 99 health
$bgs = $totalhp > 100 ? (int)(($totalhp-1)/100) : 0; // Number of active bodyguards
$hp = min($totalhp, 100); // Your health
$bg_hp = $bgs > 0 ? ((($totalhp-1) % 100) + 1) : 0; // the health of your "current" bodyguard
然后,当你受到一些伤害时,只需从$totalhp
:
$totalhp = $totalhp - $dmg;
答案 2 :(得分:0)
<?php
$bg = 2;
$health_max = 100;
$health_total = $bg * $health_max + $health_max;
$damage = 100;
if($damage >= $health_total)
{
die('You are caput.');
}
else
{
$health_total = $health_total - $damage;
$bg = floor($health_total / $health_max);
}
if($bg > 0)
{
$tmp = ($health_total - $health_max) % $health_max;
for($i = 1; $i < $bg; $i++)
$bg_health[] = $health_max;
$bg_health[] = $tmp;
$bg_health = implode(', ', $bg_health);
}
if(empty($bg_health)) $tmp = 0;
$health = $health_total - $tmp;
echo "You have {$health} health and {$bg} bodyguards left with health of {$bg_health}.";
?>
这会产生......
你有100个健康和1个保镖,健康状况为77.