Added d100

This commit is contained in:
Mitchell McCaffrey 2020-05-14 13:26:21 +10:00
parent 2e3a733d4c
commit 975cc24f4c
3 changed files with 113 additions and 41 deletions

View File

@ -9,6 +9,7 @@ import D10Icon from "../../../icons/D10Icon";
import D8Icon from "../../../icons/D8Icon";
import D6Icon from "../../../icons/D6Icon";
import D4Icon from "../../../icons/D4Icon";
import D100Icon from "../../../icons/D100Icon";
function DiceControls({ onDiceAdd }) {
return (
@ -55,6 +56,13 @@ function DiceControls({ onDiceAdd }) {
>
<D4Icon />
</IconButton>
<IconButton
title="Add D100"
aria-label="Add D100"
onClick={() => onDiceAdd(SunsetDice, "d100")}
>
<D100Icon />
</IconButton>
</Flex>
);
}

View File

@ -115,34 +115,21 @@ function DiceTray({ isOpen }) {
for (let i = 0; i < die.length; i++) {
const dice = die[i];
const diceAsleep = dieSleepRef.current[i];
const speed = dice.physicsImpostor.getLinearVelocity().length();
if (speed < 0.01 && !diceAsleep) {
let highestDot = -1;
let highestLocator;
for (let locator of dice.getChildTransformNodes()) {
let dif = locator
.getAbsolutePosition()
.subtract(dice.getAbsolutePosition());
let direction = dif.normalize();
const dot = BABYLON.Vector3.Dot(direction, BABYLON.Vector3.Up());
if (dot > highestDot) {
highestDot = dot;
highestLocator = locator;
}
}
const diceIsAsleep = dieSleepRef.current[i];
const speed = getDiceSpeed(dice);
if (speed < 0.01 && !diceIsAsleep) {
dieSleepRef.current[i] = true;
const newNumber = parseInt(highestLocator.name.slice(12));
let newNumber = getDiceNumber(dice);
setDieNumbers((prevNumbers) => {
let newNumbers = [...prevNumbers];
newNumbers[i] = newNumber;
return newNumbers;
});
} else if (speed > 0.5 && diceAsleep) {
} else if (speed > 0.5 && diceIsAsleep) {
dieSleepRef.current[i] = false;
setDieNumbers((prevNumbers) => {
let newNumbers = [...prevNumbers];
newNumbers[i] = null;
newNumbers[i] = "unknown";
return newNumbers;
});
}
@ -152,15 +139,73 @@ function DiceTray({ isOpen }) {
}
}
function getDiceSpeed(dice) {
const diceSpeed = dice.instance.physicsImpostor
.getLinearVelocity()
.length();
// If the dice is a d100 check the d10 as well
if (dice.type === "d100") {
const d10Speed = dice.d10Instance.physicsImpostor
.getLinearVelocity()
.length();
return Math.max(diceSpeed, d10Speed);
} else {
return diceSpeed;
}
}
// Find the number facing up on a dice object
function getDiceNumber(dice) {
let number = getDiceInstanceNumber(dice.instance);
// If the dice is a d100 add the d10
if (dice.type === "d100") {
const d10Number = getDiceInstanceNumber(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 number;
}
// Find the number facing up on a mesh instance of a dice
function getDiceInstanceNumber(instance) {
let highestDot = -1;
let highestLocator;
for (let locator of instance.getChildTransformNodes()) {
let dif = locator
.getAbsolutePosition()
.subtract(instance.getAbsolutePosition());
let direction = dif.normalize();
const dot = BABYLON.Vector3.Dot(direction, BABYLON.Vector3.Up());
if (dot > highestDot) {
highestDot = dot;
highestLocator = locator;
}
}
return parseInt(highestLocator.name.slice(12));
}
async function handleDiceAdd(style, type) {
const scene = sceneRef.current;
const shadowGenerator = shadowGeneratorRef.current;
if (scene && shadowGenerator) {
const instance = await style.createInstance(type, scene);
shadowGenerator.addShadowCaster(instance);
dieRef.current.push(instance);
let dice = { type, instance };
// If we have a d100 add a d10 as well
if (type === "d100") {
const d10Instance = await style.createInstance("d10", scene);
shadowGenerator.addShadowCaster(d10Instance);
dice.d10Instance = d10Instance;
}
dieRef.current.push(dice);
dieSleepRef.current.push(false);
setDieNumbers((prevNumbers) => [...prevNumbers, null]);
setDieNumbers((prevNumbers) => [...prevNumbers, "unknown"]);
}
}
@ -177,27 +222,28 @@ function DiceTray({ isOpen }) {
bg="background"
>
<Scene onSceneMount={handleSceneMount} />
<div
style={{
position: "absolute",
bottom: "8px",
left: "50%",
transform: "translateX(-50%)",
display: "flex",
color: "white",
}}
>
{dieNumbers.map((num, index) => (
<h3 key={index}>
{num || "?"}
{index === dieNumbers.length - 1 ? "" : "+"}
{!dieNumbers.includes("unknown") && (
<div
style={{
position: "absolute",
bottom: "8px",
left: "50%",
transform: "translateX(-50%)",
display: "flex",
color: "white",
}}
>
{dieNumbers.map((num, index) => (
<h3 key={index}>
{num}
{index === dieNumbers.length - 1 ? "" : "+"}
</h3>
))}
<h3>
{dieNumbers.length > 0 && `= ${dieNumbers.reduce((a, b) => a + b)}`}
</h3>
))}
<h3>
{dieNumbers.length > 0 &&
`= ${dieNumbers.reduce((a, b) => (a || 0) + (b || 0))}`}
</h3>
</div>
</div>
)}
<div
style={{
position: "absolute",

18
src/icons/D100Icon.js Normal file
View File

@ -0,0 +1,18 @@
import React from "react";
function D100Icon() {
return (
<svg
width="24"
height="24"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentcolor"
>
<path d="M0 0h24v24H0z" fill="none" />
<path d="M11.148 1.157l.104.076 1.134.949 1.067-.914a1 1 0 011.214-.068l.097.075L23.66 9.09a1 1 0 01.333.63L24 9.84v4.789a1 1 0 01-.255.668l-.088.086-8.895 7.752a1 1 0 01-1.208.08l-.098-.073-1.069-.91-1.138.945a1 1 0 01-1.164.081l-.105-.074-9.61-7.8a1 1 0 01-.362-.648L0 14.609V9.862a1 1 0 01.273-.687l.094-.087 9.61-7.862a1 1 0 011.17-.07zm-.546 2.545L2 10.48v3.5l8.603 6.722L19 13.993v-3.526l-8.398-6.765zm-4.002 5v6H5.325v-3.931l-.154.145c-.152.139-.3.255-.445.35a4.096 4.096 0 01-.726.362v-1.344l.207-.093c.33-.16.595-.345.793-.555.237-.252.423-.564.557-.934H6.6zm3.07 0c.356 0 .65.048.878.145.23.096.416.221.561.376.145.154.259.316.342.486.083.17.15.37.2.596.1.433.149.884.149 1.354 0 1.052-.163 1.823-.488 2.311-.326.488-.887.732-1.682.732-.446 0-.807-.078-1.082-.233a1.823 1.823 0 01-.676-.685c-.128-.214-.227-.506-.299-.877a6.538 6.538 0 01-.106-1.229c0-1.102.181-1.874.544-2.315.363-.44.916-.66 1.659-.66zm5.2 0c.356 0 .65.048.878.145.23.096.416.221.561.376.145.154.259.316.342.486.083.17.15.37.2.596.1.433.149.884.149 1.354 0 1.052-.163 1.823-.488 2.311-.326.488-.887.732-1.682.732-.446 0-.807-.078-1.082-.233a1.823 1.823 0 01-.676-.685c-.128-.214-.227-.506-.299-.877a6.538 6.538 0 01-.106-1.229c0-1.102.181-1.874.544-2.315.363-.44.916-.66 1.659-.66zm-5.252 1c-.3 0-.517.142-.65.424-.134.283-.201.806-.201 1.57 0 .772.073 1.3.22 1.582.147.283.36.424.64.424.184 0 .343-.06.479-.18.135-.12.234-.31.298-.569.064-.259.096-.663.096-1.212 0-.805-.074-1.346-.22-1.623-.148-.277-.368-.416-.662-.416zm5.2 0c-.3 0-.517.142-.65.424-.134.283-.201.806-.201 1.57 0 .772.073 1.3.22 1.582.147.283.36.424.64.424.184 0 .343-.06.479-.18.135-.12.234-.31.298-.569.064-.259.096-.663.096-1.212 0-.805-.074-1.346-.22-1.623-.148-.277-.368-.416-.662-.416z" />
</svg>
);
}
export default D100Icon;