Added random dice throwing when creating instances

This commit is contained in:
Mitchell McCaffrey 2020-05-13 23:13:58 +10:00
parent e48e5481e1
commit 2e3a733d4c
3 changed files with 36 additions and 9 deletions

View File

@ -7,7 +7,9 @@ import environment from "../../../dice/environment.dds";
import Scene from "./DiceScene"; import Scene from "./DiceScene";
import DiceControls from "./DiceControls"; import DiceControls from "./DiceControls";
import createDiceTray from "../../../dice/diceTray/DiceTrayMesh"; import createDiceTray, {
diceTraySize,
} from "../../../dice/diceTray/DiceTrayMesh";
function DiceTray({ isOpen }) { function DiceTray({ isOpen }) {
const sceneRef = useRef(); const sceneRef = useRef();
@ -56,10 +58,12 @@ function DiceTray({ isOpen }) {
ground.isVisible = false; ground.isVisible = false;
ground.position.y = 0.2; ground.position.y = 0.2;
const wallSize = 50;
function createWall(name, x, z, yaw) { function createWall(name, x, z, yaw) {
let wall = BABYLON.Mesh.CreateBox( let wall = BABYLON.Mesh.CreateBox(
name, name,
50, wallSize,
scene, scene,
true, true,
BABYLON.Mesh.DOUBLESIDE BABYLON.Mesh.DOUBLESIDE
@ -76,10 +80,12 @@ function DiceTray({ isOpen }) {
wall.isVisible = false; wall.isVisible = false;
} }
createWall("wallTop", 0, -32.5, 0); const wallOffsetWidth = wallSize / 2 + diceTraySize.width / 2 - 0.5;
createWall("wallRight", -28.5, 0, Math.PI / 2); const wallOffsetHeight = wallSize / 2 + diceTraySize.height / 2 - 0.5;
createWall("wallBottom", 0, 32.5, Math.PI); createWall("wallTop", 0, -wallOffsetHeight, 0);
createWall("wallLeft", 28.5, 0, -Math.PI / 2); 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); var roof = BABYLON.Mesh.CreateGround("roof", 100, 100, 2, scene);
roof.physicsImpostor = new BABYLON.PhysicsImpostor( roof.physicsImpostor = new BABYLON.PhysicsImpostor(
@ -88,7 +94,7 @@ function DiceTray({ isOpen }) {
{ mass: 0, friction: 1.0 }, { mass: 0, friction: 1.0 },
scene scene
); );
roof.position.y = 5; roof.position.y = 10;
roof.isVisible = false; roof.isVisible = false;
scene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData( scene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(

View File

@ -8,6 +8,8 @@ import d12Source from "./meshes/d12.glb";
import d20Source from "./meshes/d20.glb"; import d20Source from "./meshes/d20.glb";
import d100Source from "./meshes/d100.glb"; import d100Source from "./meshes/d100.glb";
import { diceTraySize } from "./diceTray/DiceTrayMesh";
class Dice { class Dice {
static instanceCount = 0; static instanceCount = 0;
@ -69,8 +71,25 @@ class Dice {
scene scene
); );
// TODO: put in random start position const trayOffsetHeight = diceTraySize.height / 2 - 0.5;
instance.position.y = 2; 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; return instance;
} }

View File

@ -6,6 +6,8 @@ import albedo from "./albedo.jpg";
import metalRoughness from "./metalRoughness.jpg"; import metalRoughness from "./metalRoughness.jpg";
import normal from "./normal.jpg"; import normal from "./normal.jpg";
export const diceTraySize = { width: 8, height: 16 };
export default async function createDiceTray(scene, shadowGenerator) { export default async function createDiceTray(scene, shadowGenerator) {
let mesh = ( let mesh = (
await BABYLON.SceneLoader.ImportMeshAsync("", meshSource, "", scene) await BABYLON.SceneLoader.ImportMeshAsync("", meshSource, "", scene)