From f7fb0a8c160304330e78caafcded9efa0a70a809 Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Tue, 11 Apr 2023 17:59:33 +0200 Subject: [PATCH] inventory example --- index.html | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index b854871..472a595 100644 --- a/index.html +++ b/index.html @@ -3074,6 +3074,51 @@ function shoot(){ }, 10) } +var collectibles = [] +var inventory = [] +AFRAME.registerComponent('collectible', { + schema: { + required: {type: 'string'} + }, + init: function () { + collectibles.push({el:this.el, position:this.el.getAttribute("position"), required:this.data.required}) + } +}) +AFRAME.registerComponent('collect-to-inventory', { + schema: { + threshold: {type: 'number', default: 1}, + }, + init: function () { + this.tick = AFRAME.utils.throttleTick(this.tick, 500, this); + // could also rely on https://aframe.io/docs/1.4.0/core/entity.html#listening-for-component-changes + }, + tick: function(){ + let pos = this.el.getAttribute("position") + // does not scale well yet probably not too costly. Should use octree for numerous ones + collectibles.map( (t,i) => { + if ( pos.distanceTo(t.position) < this.data.threshold ){ + if (!t.required){ + inventory.push(t.el.id) + t.el.parentNode.removeChild(t.el) + collectibles.splice(i,1) + setFeedbackHUD("collected "+t.el.id) + } + if (t.required && inventory.includes(t.required)) { + console.log(t.required, 'needed and found') + inventory.push(t.el.id) + t.el.parentNode.removeChild(t.el) + collectibles.splice(i,1) + setFeedbackHUD("collected "+t.el.id) + } + if (t.required && !inventory.includes(t.required)) { + setFeedbackHUD("missing "+t.required) + } + console.log(inventory) + } + }) + } +}) + // used for testing AFRAME.registerComponent('startfunctions', { init: function () { @@ -3112,7 +3157,7 @@ AFRAME.registerComponent('startfunctions', { - @@ -3144,6 +3189,9 @@ AFRAME.registerComponent('startfunctions', { + + +