2023-06-19 13:29:46 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
import QtQuick
|
|
|
|
import QtQuick3D
|
|
|
|
import QtQuick3D.Physics
|
|
|
|
import QtQuick3D.Helpers
|
2023-08-17 03:49:16 +00:00
|
|
|
import QtMultimedia
|
2023-06-19 13:29:46 +00:00
|
|
|
|
|
|
|
Node {
|
|
|
|
id: shapeSpawner
|
|
|
|
property var dices: []
|
|
|
|
property var dieComponent: Qt.createComponent("PhysicalDie.qml")
|
|
|
|
|
|
|
|
function randomInRange(min, max) {
|
2023-08-17 03:49:16 +00:00
|
|
|
return Math.random() * (max - min) + min
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 03:49:16 +00:00
|
|
|
function createDie(position, physicsMaterial, rollForce) {
|
|
|
|
let rotation = Qt.vector3d(randomInRange(0, 360),
|
|
|
|
randomInRange(0, 360), randomInRange(0, 360))
|
2023-06-19 13:29:46 +00:00
|
|
|
let settings = {
|
|
|
|
"position": position,
|
|
|
|
"eulerRotation": rotation,
|
|
|
|
"physicsMaterial": physicsMaterial
|
2023-08-17 03:49:16 +00:00
|
|
|
}
|
|
|
|
let die = dieComponent.createObject(shapeSpawner, settings)
|
2023-06-19 13:29:46 +00:00
|
|
|
if (die === null) {
|
2023-08-17 03:49:16 +00:00
|
|
|
console.log("Error creating object")
|
|
|
|
} else {
|
|
|
|
die.applyCentralForce(rollForce)
|
|
|
|
dices.push(die)
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 03:49:16 +00:00
|
|
|
SoundEffect {
|
|
|
|
id: rollSound
|
|
|
|
loops: 0
|
|
|
|
source: "sounds/rolling.wav"
|
|
|
|
}
|
|
|
|
function spawnDice(numberOfDice, physicsMaterial, rollForce) {
|
|
|
|
if (allAtRest()) {
|
|
|
|
reset()
|
|
|
|
rollSound.play()
|
2023-06-19 13:29:46 +00:00
|
|
|
|
2023-08-17 03:49:16 +00:00
|
|
|
let degrees45 = Math.PI / 4
|
|
|
|
for (var index = 0; index < numberOfDice; index++) {
|
|
|
|
let initialPosition = Qt.vector3d(0.11 * Math.cos(
|
|
|
|
index / degrees45),
|
|
|
|
index * 2.1, 0)
|
|
|
|
createDie(initialPosition, physicsMaterial, rollForce)
|
|
|
|
}
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
dices.forEach(die => {
|
2023-08-17 03:49:16 +00:00
|
|
|
die.destroy()
|
|
|
|
})
|
|
|
|
dices = []
|
|
|
|
}
|
|
|
|
|
|
|
|
function allAtRest() {
|
|
|
|
let atRest = true
|
|
|
|
dices.forEach(die => {
|
|
|
|
atRest &= die.atRest
|
|
|
|
})
|
|
|
|
return atRest
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|