grungnet/src/helpers/dice.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Vector3 } from "@babylonjs/core/Maths/math";
2021-07-09 02:22:35 -04:00
import { DiceRoll } from "../types/Dice";
/**
* Find the number facing up on a mesh instance of a dice
* @param {Object} instance The dice instance
*/
2021-05-25 03:35:26 -04:00
export function getDiceInstanceRoll(instance: any) {
let highestDot = -1;
let highestLocator;
for (let locator of instance.getChildTransformNodes()) {
let dif = locator
.getAbsolutePosition()
.subtract(instance.getAbsolutePosition());
let direction = dif.normalize();
const dot = Vector3.Dot(direction, Vector3.Up());
if (dot > highestDot) {
highestDot = dot;
highestLocator = locator;
}
}
return parseInt(highestLocator.name.slice(12));
}
/**
* Find the number facing up on a dice object
* @param {Object} dice The Dice object
*/
2021-05-25 03:35:26 -04:00
export function getDiceRoll(dice: any) {
let number = getDiceInstanceRoll(dice.instance);
// If the dice is a d100 add the d10
if (dice.type === "d100") {
const d10Number = getDiceInstanceRoll(dice.d10Instance);
// Both zero set to 100
if (d10Number === 0 && number === 0) {
number = 100;
} else {
number += d10Number;
}
} else if (dice.type === "d10" && number === 0) {
number = 10;
}
return { type: dice.type, roll: number };
}
2020-08-05 03:05:04 -04:00
2021-07-09 02:22:35 -04:00
export function getDiceRollTotal(diceRolls: DiceRoll[]) {
2021-05-25 03:35:26 -04:00
return diceRolls.reduce((accumulator: number, dice: any) => {
2020-08-05 03:05:04 -04:00
if (dice.roll === "unknown") {
return accumulator;
} else {
return accumulator + dice.roll;
}
}, 0);
}