UNPKG

slanted-gamedev-toolz

Version:

A slanted mix of tools for your brilliant ideas in game design.

39 lines (38 loc) 1.26 kB
export const rollDice = (diceSides) => { if (!Number.isInteger(diceSides) || diceSides <= 0) { console.error("Invalid input: Please provide a positive integer for dice sides"); return 1; } // Perform the dice roll const thisRollValue = Math.floor(Math.random() * diceSides + 1); return thisRollValue; }; export const rollLowWeightedDice = (diceSides) => { if (!Number.isInteger(diceSides) || diceSides <= 0) { console.error("Invalid input: Please provide a positive integer for dice sides"); return 1; } if (percentageChance(50)) return 0; const thisRollValue = Math.floor(Math.pow(Math.random(), 2) * (diceSides - 1) + 1); return thisRollValue; }; export const rollDiceIsMaxRoll = (diceSides) => { const thisRollValue = Math.floor(Math.random() * diceSides + 1); const rolledMaxValue = thisRollValue === diceSides; return rolledMaxValue; }; export function percentageChance(chance) { if (!Number.isInteger(chance)) { console.error("Invalid input"); return false; } if (chance < 0) { return false; } if (chance > 100) { return true; } const randomValue = Math.random() * 100; return randomValue < chance; }