mini games
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { Raycaster, Vector3 } from "three"
|
||||
|
||||
class Clickable {
|
||||
constructor(defaultDistance) {
|
||||
const objects = [];
|
||||
const raycaster = new Raycaster();
|
||||
let v = new Vector3;
|
||||
|
||||
this.add = function (object, fn, distance) {
|
||||
objects.push(object);
|
||||
object._clickable = { fn, distance: distance || defaultDistance };
|
||||
};
|
||||
|
||||
this.remove = function (object) {
|
||||
objects.splice(objects.indexOf(object), 1);
|
||||
delete object._clickable;
|
||||
};
|
||||
|
||||
this.update = function (mouse, camera, event) {
|
||||
raycaster.setFromCamera(mouse, camera);
|
||||
let forExecute = [];
|
||||
objects.forEach(o => {
|
||||
console.log(1)
|
||||
o.getWorldPosition(v);
|
||||
if (camera.position.distanceTo(v) <= o._clickable.distance && o.visible) {
|
||||
console.log(2)
|
||||
const intersects = raycaster.intersectObject(o);
|
||||
// intersects.forEach(i=>{
|
||||
// forExecute.push({o, i})
|
||||
// })
|
||||
if (intersects[0]) forExecute.push({ o, i: intersects[0] });
|
||||
}
|
||||
});
|
||||
if (forExecute[0]) {
|
||||
let sorted = forExecute.sort((a, b) => a.i.distance - b.i.distance);
|
||||
sorted[0].o._clickable.fn(sorted[0].i);
|
||||
if (this.onclick) {
|
||||
this.onclick(sorted[0].o, sorted[0].i, event);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export {Clickable}
|
||||
Reference in New Issue
Block a user