Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(REVIEW ONLY) Jeremy bubble exercises #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/components/BubbleEmitterComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { NO_PROXY, getState } from "@etherealengine/hyperflux"
import { useExecute } from "@etherealengine/engine/src/ecs/functions/SystemFunctions"
import { SimulationSystemGroup } from "@etherealengine/engine/src/ecs/functions/EngineFunctions"
import { EngineState } from "@etherealengine/engine/src/ecs/classes/EngineState"
import { LocalTransformComponent } from "@etherealengine/engine/src/transform/components/TransformComponent"

export const BubbleEmitterComponent = defineComponent({
//name: The human-readable label for the component. This will be displayed in the editor and debugging tools.
Expand Down Expand Up @@ -92,21 +93,31 @@ export const BubbleEmitterComponent = defineComponent({
// Spawning a single bubble as an example
// [Exercise 1]: Using this system. Spawn multiple bubbles with varying x,z Localtransform positons
// [Exercise 3]: Remove them if they are too old(bubble.age > N seconds)[This can be done in a couple ways(reactively and within this sytem synchronosly)]
if(emitterComponent.bubbleEntities.value!.length < 1) { //For example ensuring there is only one bubble being added
const bubbles: Array<Entity> = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using emitterComponent.bubbleEntities was enough to track the bubbles.

const numExistingBubbles = emitterComponent.bubbleEntities.value!.length
const numBubbles = 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this constant to actually be at the top of the file or configured withing the emitter component

for (let i = numExistingBubbles; i < numBubbles; i++) {
const bubbleEntity = createEntity()
setComponent(bubbleEntity, BubbleComponent)
setComponent(bubbleEntity, LocalTransformComponent, {
position: new Vector3((Math.random() - 0.5) * 100, 0, (Math.random() - 0.5) * 100)
})
setComponent(bubbleEntity, EntityTreeComponent, {
parentEntity: entity,
uuid: MathUtils.generateUUID() as EntityUUID
})
emitterComponent.bubbleEntities.merge([bubbleEntity])
bubbles.push(bubbleEntity)
}
emitterComponent.bubbleEntities.merge(bubbles)

const bubble = getComponent(emitterComponent.bubbleEntities.value![0], BubbleComponent)

if(bubble.age >= 5) { // Delete one bubble after its age is greater than 5 seconds
removeBubble(entity,emitterComponent.bubbleEntities.value![0])
const bubbleEntities = emitterComponent.bubbleEntities.value!.slice()
for (const bubbleEntity of bubbleEntities) {
const bubble = getComponent(bubbleEntity, BubbleComponent)
if (bubble.age >= 5) {
removeBubble(entity, bubbleEntity)
}
}

}, { after: SimulationSystemGroup })

return null
Expand Down
31 changes: 24 additions & 7 deletions src/systems/BubbleSystem.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -18,14 +23,26 @@ export const BubbleSystem = defineSystem({
// [Exercise 2]: Using the below basic setup. Move every bubble not just the first one
const tempvector = new Vector3(0,0,0)
const emitterComponent = getComponent(entity, BubbleEmitterComponent)
const localTransform = getComponent(emitterComponent.bubbleEntities![0], LocalTransformComponent)
if(!localTransform) continue;
velocity.copy(emitterComponent.direction).multiplyScalar(emitterComponent.speed)
tempvector.addVectors(localTransform.position, velocity)
localTransform.position.copy(tempvector)

for (const bubbleEntity of emitterComponent.bubbleEntities!) {
const localTransform = getComponent(bubbleEntity, LocalTransformComponent)
if(!localTransform) continue;
velocity.copy(emitterComponent.direction).multiplyScalar(emitterComponent.speed)
tempvector.addVectors(localTransform.position, velocity)
localTransform.position.copy(tempvector)
}
// [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
}
}
}
}
})