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
|
|
|
|
|
|
|
|
Node {
|
|
|
|
id: shapeSpawner
|
|
|
|
property var dices: []
|
|
|
|
property var dieComponent: Qt.createComponent("PhysicalDie.qml")
|
|
|
|
|
|
|
|
function randomInRange(min, max) {
|
2023-06-19 10:54:04 +00:00
|
|
|
return Math.random() * (max - min) + min;
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDie(position, physicsMaterial) {
|
2023-06-19 10:54:04 +00:00
|
|
|
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-06-19 10:54:04 +00:00
|
|
|
};
|
|
|
|
let die = dieComponent.createObject(shapeSpawner, settings);
|
|
|
|
dices.push(die);
|
2023-06-19 13:29:46 +00:00
|
|
|
if (die === null) {
|
2023-06-19 10:54:04 +00:00
|
|
|
console.log("Error creating object");
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnDice(numberOfDice, physicsMaterial) {
|
2023-06-19 10:54:04 +00:00
|
|
|
reset();
|
2023-06-22 11:37:28 +00:00
|
|
|
let degrees45 = Math.PI / 4;
|
2023-06-19 13:29:46 +00:00
|
|
|
for (var index = 0; index < numberOfDice; index++) {
|
2023-06-19 10:54:04 +00:00
|
|
|
let initialPosition
|
2023-06-22 11:37:28 +00:00
|
|
|
= Qt.vector3d(0.11 * Math.cos(index / degrees45),
|
2023-06-19 10:54:04 +00:00
|
|
|
index * 2.1, 0);
|
|
|
|
createDie(initialPosition, physicsMaterial);
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
dices.forEach(die => {
|
2023-06-19 10:54:04 +00:00
|
|
|
die.destroy();
|
|
|
|
});
|
|
|
|
dices = [];
|
2023-06-19 13:29:46 +00:00
|
|
|
}
|
|
|
|
}
|