JS中的公式可为游戏中的不同分支创建体验目标 [英] Formula in JS to create experience goals for different branches in a game

查看:60
本文介绍了JS中的公式可为游戏中的不同分支创建体验目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Javascript创建一个小游戏,我的树可以容纳一定量的最大经验,每棵树上都有数量不等的分支,也需要根据经验进行升级,这些分支应该合计达到树的最大体验.

I'm creating a little game in Javascript, and I have trees that can hold a certain amount of max experience, and each tree have a varied amount of branches that also needs to be leveled up with experience, and they should total to the tree max experience.

如果这很容易,我可以像max / branches一样将它们平均分配,但是由于是游戏,因此我需要制定一个公式,其中第一个分支不需要经验,然后每个分支的经验都需要稳定增加(同时仍可达到树的最大体验).

If it was that easy, I would just divide them equally, like max / branches, but since it's for a game, I need to make a formula where the first branch needs no experience, and then each branch experience needed steadily increase (while still totalling to the tree max experience).

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等,因此我相信它必须与树的最大体验能力成比例.我希望每个级别增加多少,还必须取决于有多少分支,但是我认为所有树上都应该有某种模式(如果这是个坏主意,请纠正我).

The amount of branches can be anything from 2 to 10, to 20 and so on, so it must therefor scale with the tree max experience capacity, I believe. How much I want each level to increase with must also depend on how many branches there are, but there should be some kind of pattern across all trees, I think (correct me if it's a bad idea).

我们知道的变量是:

  • 每棵树的分支数量
  • 每棵树的最大体验能力

其余的未知.

公式如何解决此问题?我也不介意专门针对JS的任何函数式.

How can a formula solve this issue? I also do not mind any functional formulas for specifically JS.

推荐答案

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

What you seem to want is arithmetic progression. It's a sequence of numbers where there is a common difference between each, for example 1, 4, 7, 10 would be an arithmetic progression where the difference is 3 between each subsequent members.

对算术级数更广义的看法是

The more generalised view of arithmetic progression is

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

其中a是您的初始成员d差异,并且n是该系列的长度 .

Where a is your initial member, d is the difference, and n is the length of the series.

算术级数和的公式如下:

The formula for a sum of arithmetic series is as follows:

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

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

Which looks complex let's just see it in action. Let's say we want to sum the series 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Or integers from 1 to 10. Substituting in the formula, we have a = 1, d = 1, n = 10 and we get

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

我们可以编写代码进行验证:

And we can just write code to verify:

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

console.log(sum);

因此,该公式有效.

现在,我们想洗牌-我们有S,对于总树体验能力,我们还有n 分支数,并且我们有a,它的初始分支为零.我们只需要找到d即可获得我们需要的所有东西.所以,就这样:

Now, we want to shuffle things - we have S, for the total tree experience capacity, we also have n the number of branches, and we have a which would be zero for the initial branch. We just need to find d in order to get all the things we need. So, here it goes:

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

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

We can actually eliminate 2*a since we know a = 0, so we can slightly simplify the formula:

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

完成.我们现在可以将其编码为一个函数:

And done. We can just encode it in a function now:

// S = totalExperience
// n = branches

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

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

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

You can even use a generator function that will supply you the next XP value:

// 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)

这篇关于JS中的公式可为游戏中的不同分支创建体验目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆