From 51ce7f76f94454a7fdf35a8da3d2b3dc6ba975de Mon Sep 17 00:00:00 2001 From: Rezmason Date: Fri, 8 Dec 2023 10:46:25 -0800 Subject: [PATCH] Exercise 4: detect if the player is near a bubble and remove it --- src/systems/BubbleSystem.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/systems/BubbleSystem.ts b/src/systems/BubbleSystem.ts index 087314d..9810598 100644 --- a/src/systems/BubbleSystem.ts +++ b/src/systems/BubbleSystem.ts @@ -1,13 +1,18 @@ import { defineQuery, getComponent, getMutableComponent } from "@etherealengine/engine/src/ecs/functions/ComponentFunctions"; import { defineSystem } from "@etherealengine/engine/src/ecs/functions/SystemFunctions"; import { BubbleEmitterComponent, removeBubble } from "../components/BubbleEmitterComponent"; -import { LocalTransformComponent } from "@etherealengine/engine/src/transform/components/TransformComponent"; +import { BubbleComponent } from "../components/BubbleComponent"; +import { LocalTransformComponent, TransformComponent } from "@etherealengine/engine/src/transform/components/TransformComponent"; import { NO_PROXY, getState } from "@etherealengine/hyperflux"; import { EngineState } from "@etherealengine/engine/src/ecs/classes/EngineState"; import { Vector3 } from "three"; import { SimulationSystemGroup } from "@etherealengine/engine/src/ecs/functions/EngineFunctions"; +import { AvatarComponent } from "@etherealengine/engine/src/avatar/components/AvatarComponent"; +import { EntityTreeComponent } from "@etherealengine/engine/src/ecs/functions/EntityTree"; const bubbleEmitterQuery = defineQuery([BubbleEmitterComponent]) +const bubbleQuery = defineQuery([BubbleComponent]) +const avatarQuery = defineQuery([AvatarComponent, TransformComponent]) const velocity = new Vector3(0,0,0) export const BubbleSystem = defineSystem({ @@ -28,5 +33,16 @@ export const BubbleSystem = defineSystem({ // [Exercise 4]: Utilizing an AvatarComponent Query, TransformComponent positions of bubble entities, and Vector3.distanceTo // Detect if the player is near a bubble and remove it } + + for (const bubbleEntity of bubbleQuery()) { + const bubbleWorldPosition = getComponent(bubbleEntity, TransformComponent).position + for (const avatarEntity of avatarQuery()) { + if (getComponent(avatarEntity, TransformComponent).position.distanceTo(bubbleWorldPosition) < 1) { + const emitter = getComponent(bubbleEntity, EntityTreeComponent).parentEntity! + removeBubble(emitter, bubbleEntity) + break + } + } + } } }) \ No newline at end of file