From 2e3a733d4c98adf2054bc978f89f15787472527c Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Wed, 13 May 2020 23:13:58 +1000 Subject: [PATCH] Added random dice throwing when creating instances --- src/components/map/dice/DiceTray.js | 20 +++++++++++++------- src/dice/Dice.js | 23 +++++++++++++++++++++-- src/dice/diceTray/DiceTrayMesh.js | 2 ++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/components/map/dice/DiceTray.js b/src/components/map/dice/DiceTray.js index 02602aa..786f70e 100644 --- a/src/components/map/dice/DiceTray.js +++ b/src/components/map/dice/DiceTray.js @@ -7,7 +7,9 @@ import environment from "../../../dice/environment.dds"; import Scene from "./DiceScene"; import DiceControls from "./DiceControls"; -import createDiceTray from "../../../dice/diceTray/DiceTrayMesh"; +import createDiceTray, { + diceTraySize, +} from "../../../dice/diceTray/DiceTrayMesh"; function DiceTray({ isOpen }) { const sceneRef = useRef(); @@ -56,10 +58,12 @@ function DiceTray({ isOpen }) { ground.isVisible = false; ground.position.y = 0.2; + const wallSize = 50; + function createWall(name, x, z, yaw) { let wall = BABYLON.Mesh.CreateBox( name, - 50, + wallSize, scene, true, BABYLON.Mesh.DOUBLESIDE @@ -76,10 +80,12 @@ function DiceTray({ isOpen }) { wall.isVisible = false; } - createWall("wallTop", 0, -32.5, 0); - createWall("wallRight", -28.5, 0, Math.PI / 2); - createWall("wallBottom", 0, 32.5, Math.PI); - createWall("wallLeft", 28.5, 0, -Math.PI / 2); + const wallOffsetWidth = wallSize / 2 + diceTraySize.width / 2 - 0.5; + const wallOffsetHeight = wallSize / 2 + diceTraySize.height / 2 - 0.5; + createWall("wallTop", 0, -wallOffsetHeight, 0); + createWall("wallRight", -wallOffsetWidth, 0, Math.PI / 2); + createWall("wallBottom", 0, wallOffsetHeight, Math.PI); + createWall("wallLeft", wallOffsetWidth, 0, -Math.PI / 2); var roof = BABYLON.Mesh.CreateGround("roof", 100, 100, 2, scene); roof.physicsImpostor = new BABYLON.PhysicsImpostor( @@ -88,7 +94,7 @@ function DiceTray({ isOpen }) { { mass: 0, friction: 1.0 }, scene ); - roof.position.y = 5; + roof.position.y = 10; roof.isVisible = false; scene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData( diff --git a/src/dice/Dice.js b/src/dice/Dice.js index f8ad2a1..e5d0285 100644 --- a/src/dice/Dice.js +++ b/src/dice/Dice.js @@ -8,6 +8,8 @@ import d12Source from "./meshes/d12.glb"; import d20Source from "./meshes/d20.glb"; import d100Source from "./meshes/d100.glb"; +import { diceTraySize } from "./diceTray/DiceTrayMesh"; + class Dice { static instanceCount = 0; @@ -69,8 +71,25 @@ class Dice { scene ); - // TODO: put in random start position - instance.position.y = 2; + const trayOffsetHeight = diceTraySize.height / 2 - 0.5; + const initialPosition = new BABYLON.Vector3( + ((Math.random() * 2 - 1) * diceTraySize.width) / 2, + 5, + this.instanceCount % 2 === 0 ? trayOffsetHeight : -trayOffsetHeight + ); + instance.position = initialPosition; + instance.addRotation( + Math.random() * Math.PI * 2, + Math.random() * Math.PI * 2, + Math.random() * Math.PI * 2 + ); + + const impulse = BABYLON.Vector3.Zero() + .subtract(initialPosition) + .normalizeToNew() + .scale(10); + + instance.physicsImpostor.applyImpulse(impulse, initialPosition); return instance; } diff --git a/src/dice/diceTray/DiceTrayMesh.js b/src/dice/diceTray/DiceTrayMesh.js index 64f3d86..9f11de5 100644 --- a/src/dice/diceTray/DiceTrayMesh.js +++ b/src/dice/diceTray/DiceTrayMesh.js @@ -6,6 +6,8 @@ import albedo from "./albedo.jpg"; import metalRoughness from "./metalRoughness.jpg"; import normal from "./normal.jpg"; +export const diceTraySize = { width: 8, height: 16 }; + export default async function createDiceTray(scene, shadowGenerator) { let mesh = ( await BABYLON.SceneLoader.ImportMeshAsync("", meshSource, "", scene)