Triborg-333
@Triborg-333

Как правильно реализовать (Level System) для игры?

Здравствуйте, уважаемые программисты!) помогите правильно реализовать (Систему уровней для игры).
Нужно сделать так, чтобы на 100 lvl, max_xp был 500000-600000К
код ниже как пример:
let xp = 0;
let max_xp = 50;
let level = 0;

let timer = setInterval(()=>{
	xp += 15000;
	if(xp >= max_xp){
		level++;
		xp = 0;
		max_xp *= 2;
	}
	
	if(level >= 100){
		clearInterval(timer);
	}
	
	console.log('XP:' + xp, 'MAX_XP:' + Math.floor(max_xp), 'level:' + level);
	
}, 10);
  • Вопрос задан
  • 89 просмотров
Решения вопроса 1
Seasle
@Seasle Куратор тега JavaScript
\( ゚ヮ゚)/
Возможно как-то так:
Пример 1
const lerp = (min, max, value) => (1 - value) * min + value * max;

const LEVELS = 10;
const MAX_EXPERIENCE = 600000;

const multipliers = [];
for (let index = 0; index < LEVELS - 1; index++) {
  multipliers.push(
    lerp(1, 2, index / LEVELS)
  );
}

const total = multipliers.reduce((total, value) => total + value, 0);
const step = MAX_EXPERIENCE / total;
const { levels } = multipliers.reduce((store, multiplier, index) => {
  const experience = step * multiplier;
  store.levels.push({
    level: index + 1,
    experience: {
      current: store.total,
      next: store.total + experience,
      need: experience
    }
  });
  store.total += experience;
  
  if (index + 1 === LEVELS - 1) {
    store.levels.push({
      level: LEVELS,
      experience: {
        current: store.total
      }
    });
  }
  
  return store;
}, { levels: [], total: 0 });

console.log(levels);
/*
[
  {
    level: 1,
    experience: { current: 0, next: 47619.04761904762, need: 47619.04761904762 }
  },
  {
    level: 2,
    experience: { current: 47619.04761904762, next: 100000, need: 52380.95238095238 }
  },
  {
    level: 3,
    experience: { current: 100000, next: 157142.85714285716, need: 57142.85714285715 }
  },
  {
    level: 4,
    experience: { current: 157142.85714285716, next: 219047.61904761905, need: 61904.761904761894 }
  },
  {
    level: 5,
    experience: { current: 219047.61904761905, next: 285714.2857142857, need: 66666.66666666666 }
  },
  {
    level: 6,
    experience: { current: 285714.2857142857, next: 357142.8571428571, need: 71428.57142857142 }
  },
  {
    level: 7,
    experience: { current: 357142.8571428571, next: 433333.3333333333, need: 76190.4761904762 }
  },
  {
    level: 8,
    experience: { current: 433333.3333333333, next: 514285.71428571426, need: 80952.38095238095 }
  },
  {
    level: 9,
    experience: { current: 514285.71428571426, next: 600000, need: 85714.28571428571 }
  },
  {
    level: 10,
    experience: { current: 600000 }
  }
]
*/

В примере выше используется линейная интерполяция. На её основе можете изменить шаг между уровнями, например, используя easeInCubic в нормализованном значении:
Пример 2
const lerp = (min, max, value) => (1 - value) * min + value * max;
const easeInCubic = value => value * value * value;

const LEVELS = 10;
const MAX_EXPERIENCE = 600000;

const multipliers = [];
for (let index = 0; index < LEVELS - 1; index++) {
  multipliers.push(
    lerp(1, 2, easeInCubic(index / LEVELS))
  );
}

const total = multipliers.reduce((total, value) => total + value, 0);
const step = MAX_EXPERIENCE / total;
const { levels } = multipliers.reduce((store, multiplier, index) => {
  const experience = step * multiplier;
  store.levels.push({
    level: index + 1,
    experience: {
      current: store.total,
      next: store.total + experience,
      need: experience
    }
  });
  store.total += experience;
  
  if (index + 1 === LEVELS - 1) {
    store.levels.push({
      level: LEVELS,
      experience: {
        current: store.total
      }
    });
  }
  
  return store;
}, { levels: [], total: 0 });

console.log(levels);
/*
[
  {
    level: 1,
    experience: { current: 0, next: 58275.058275058276, need: 58275.058275058276 }
  },
  {
    level: 2,
    experience: { current: 58275.058275058276, next: 116608.3916083916, need: 58333.33333333333 }
  },
  {
    level: 3,
    experience: { current: 116608.3916083916, next: 175349.65034965036, need: 58741.258741258745 }
  },
  {
    level: 4,
    experience: { current: 175349.65034965036, next: 235198.13519813522, need: 59848.48484848485 }
  },
  {
    level: 5,
    experience: { current: 235198.13519813522, next: 297202.79720279726, need: 62004.66200466201 }
  },
  {
    level: 6,
    experience: { current: 297202.79720279726, next: 362762.2377622378, need: 65559.44055944055 }
  },
  {
    level: 7,
    experience: { current: 362762.2377622378, next: 433624.70862470864, need: 70862.47086247086 }
  },
  {
    level: 8,
    experience: { current: 433624.70862470864, next: 511888.1118881119, need: 78263.40326340326 }
  },
  {
    level: 9,
    experience: { current: 511888.1118881119, next: 600000, need: 88111.88811188811 }
  },
  {
    level: 10,
    experience: { current: 600000 }
  }
]
*/
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы