javascript - JS 公式为游戏中的不同分支创建体验目标

标签 javascript math formula game-development

我正在用 Javascript 创建一个小游戏,我有一些树可以容纳一定数量的最大经验,每棵树都有不同数量的分支,这些分支也需要随着经验升级,它们应该总计达到树最大经验。

如果这么简单,我会把它们平分,就像max/Branches,但由于它是一个游戏,我需要制定一个公式,其中第一个分支不需要经验,并且然后每个分支的经验需要稳步增加(同时仍然总计为树最大经验)。

Branch 1              0 XP
Branch 2              100 XP
Branch 3              200 XP
Branch 4              300 XP

Total Tree Experience Capacity: 600 XP

分支的数量可以是从 2 到 10、到 20 等等的任意数量,因此我相信它必须根据树的最大经验容量进行扩展。我希望每个级别增加多少还必须取决于有多少分支,但我认为所有树都应该有某种模式(如果这是一个坏主意,请纠正我)。

我们知道的变量是:

  • 每棵树的分支数量
  • 每棵树的最大经验容量

其余的未知。

如何用公式解决这个问题?我也不介意任何专门针对 JS 的函数公式。

最佳答案

您似乎想要的是 arithmetic progression 。这是一个数字序列,每个数字之间存在共同差异,例如 1, 4, 7, 10将是一个算术级数,其差为 3每个后续成员之间。

算术级数的更普遍的观点是

a, a + d, a + 2d, a + 3d, ..., a + nd`

哪里a是您的初始成员d差异,并且 n是系列的长度

算术级数之和的公式如下:

S = (n/2) * (2*a + (n - 1)*d)

这看起来很复杂,让我们看看它的实际效果。假设我们想要对系列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 求和。或者1到10之间的整数。代入公式,我们有 a = 1 , d = 1 , n = 10我们得到

S = (10/2) * (2*1 + (10 - 1)*1)
  = 5 * (2 + 9*1)
  = 5 * (2 + 9)
  = 5 * 11
  = 55

我们可以编写代码来验证:

const sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;

console.log(sum);

所以,这个公式是有效的。

现在,我们想要重新洗牌 - 我们有 S ,对于总树经验容量,我们还有n 分支数量,我们有 a对于初始分支来说为零。我们只需要找到d为了得到我们需要的所有东西。所以,事情就是这样:

S = (n/2) * (2*a + (n - 1)*d)
S / (n/2) = 2*a + (n - 1)*d
(S / (n/2)) - 2*a = (n - 1)*d
((S / (n/2)) - 2*a) / (n - 1) = d

我们实际上可以消除2*a因为我们知道a = 0 ,所以我们可以稍微简化一下公式:

(S / (n/2)) / (n - 1) = d

完成了。我们现在可以将其编码到函数中:

// S = totalExperience
// n = branches

function findArithmeticStep(totalExperience, branches) {
  return (totalExperience / (branches/2)) / (branches - 1);
}

console.log(findArithmeticStep(600, 4));

您甚至可以使用生成器函数来为您提供下一个 XP 值:

// S = totalExperience
// n = branches

function findArithmeticStep(totalExperience, branches) {
  return (totalExperience / (branches/2)) / (branches - 1);
}

function* nextXPCost(totalExperience, branches) {
  const step = findArithmeticStep(totalExperience, branches);
  
  let currentXPCost = 0;
  for(let i = 0; i < branches; i++) {
    yield currentXPCost;
    currentXPCost += step;
  }
}

const gen = nextXPCost(600, 4);

//1 - 0
let next = gen.next();
console.log(next.value, next.done);
//2 - 100
next = gen.next();
console.log(next.value, next.done);
//3 - 200
next = gen.next();
console.log(next.value, next.done);
//4 - 300
next = gen.next();
console.log(next.value, next.done);
//5 - getting after the end
next = gen.next();
console.log(next.value, next.done);

//generate an array of all costs
const allCostsTree1 = [...nextXPCost(600, 4)];
console.log(allCostsTree1);


//generate a different tree with higher total cost
const allCostsTree2 = [...nextXPCost(2000, 5)];
console.log(allCostsTree2)

关于javascript - JS 公式为游戏中的不同分支创建体验目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60904585/

相关文章:

javascript - svg 中的图像映射选择

javascript - AJAX 调用 [object%20Object] 而不是 URL (Symfony)

javascript - PHP 中使用多个复选框进行过滤

python - 计算矩阵的零空间

excel - 逻辑解析 Excel 中的字符串以修剪附近的重复项

javascript - 在 console.log 中出现 "Undefined"错误

math - 哪种度量表明数据平滑变化?

.net - 在椭圆圆周上找到一个点,该点在具有中心点,高度和宽度的矩形内?

python - 莱布尼茨行列式公式复杂度

excel - VBA 半正弦公式