Skip to content

Commit

Permalink
feat: add global events, pointer events and nft shape (decentraland#2)
Browse files Browse the repository at this point in the history
* feat: add global events, pointer events and nft shape

* fix

* move system to a global one

* fix

* better test

* apply @lean feedback

* feat: add animation component
fix: fix gltf visible collision layer
  • Loading branch information
kuruk-mm authored Sep 1, 2023
1 parent d7f6936 commit ce574c7
Show file tree
Hide file tree
Showing 22 changed files with 347 additions and 110 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"trailingComma": "none"
},
"dependencies": {
"@dcl-sdk/utils": "^1.1.2",
"@dcl/posix": "^1.0.4",
"@types/buffers": "^0.1.31",
"@types/text-encoding": "^0.0.36",
Expand Down
8 changes: 7 additions & 1 deletion sdk6-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@
"node": ">=14.0.0",
"npm": ">=6.0.0",
"yarn": "please use npm"
}
},
"dependencies": {
"@dcl/ecs-scene-utils": "^1.7.5"
},
"bundleDependencies": [
"@dcl/ecs-scene-utils"
]
}
51 changes: 36 additions & 15 deletions sdk6-tests/src/game.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/// --- Set up a system ---

import { getCurrentRealm } from "@decentraland/EnvironmentAPI"
import { getParcel } from "@decentraland/ParcelIdentity"
import { getProvider } from "@decentraland/web3-provider"
import * as utils from '@dcl/ecs-scene-utils'

class RotatorSystem {
// this group will contain every entity that has a Transform component
Expand Down Expand Up @@ -35,33 +32,57 @@ function spawnCube(x: number, y: number, z: number) {
// add a shape to the entity
cube.addComponent(new BoxShape())

const material = new Material()
material.albedoColor = Color3.Red()
cube.addComponent(material)

// add the entity to the engine
engine.addEntity(cube)

cube.addComponent(
new utils.TriggerComponent(
new utils.TriggerBoxShape(new Vector3(5,5,5)), //shape
{
onCameraEnter: () => {
cube.getComponent(Material).albedoColor = Color3.Blue()
},
onCameraExit: () => {
cube.getComponent(Material).albedoColor = Color3.Red()
}
}
)
)

cube.addComponent(
new OnPointerUp(() => {
log('OnPointerUp', cube)
})
)
return cube
}

/// --- Spawn a cube ---

const cube = spawnCube(8, 1, 8)

log('Hello world')
cube.addComponent(
new OnPointerDown(() => {
log('OnPointerDown')
cube.getComponent(Transform).scale.z *= 1.1
cube.getComponent(Transform).scale.x *= 0.9

spawnCube(Math.random() * 8 + 1, Math.random() * 8, Math.random() * 8 + 1)
})
)

getParcel().then((e) => {
log('getParcel:', JSON.stringify(e))
})

getProvider().then((e) => {
log('getProvider:', JSON.stringify(e))
})

getCurrentRealm().then((e) => {
log('getCurrentRealm:', JSON.stringify(e))
})
cube.addComponent(
new OnPointerHoverEnter(() => {
log('OnPointerHoverEnter')
})
)
cube.addComponent(
new OnPointerHoverExit(() => {
log('OnPointerHoverExit')
})
)
17 changes: 13 additions & 4 deletions sdk6-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
"compilerOptions": {
"outFile": "./bin/game.js",
"allowJs": true,
"strict": true
"strict": true,
"paths": {
"@dcl/ecs-scene-utils": [
"node_modules/@dcl/ecs-scene-utils/dist/index.d.ts"
]
},
"baseUrl": "."
},
"include": ["src/**/*.ts"],
"extends": "./node_modules/decentraland-ecs/types/tsconfig.json"
}
"include": [
"src/**/*.ts"
],
"extends": "./node_modules/decentraland-ecs/types/tsconfig.json",
"compileOnSave": false
}
35 changes: 35 additions & 0 deletions src/components-bridge/Animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { Animator, PBAnimationState } from '@dcl/sdk/ecs'

function convertAnimationState(states: any): PBAnimationState[] {
const animationStates: PBAnimationState[] = []

for (const state of states) {
animationStates.push({
name: state.clip, // TODO: Review if this is right
clip: state.clip,
loop: state.looping,
weight: state.weight,
playing: state.playing,
shouldReset: state.shouldReset,
speed: state.speed,
})
}

return animationStates
}

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

Animator.createOrReplace(ecs7Entity, {
states: convertAnimationState(payload.states)
})
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
Animator.deleteFrom(ecs7Entity)
}
6 changes: 3 additions & 3 deletions src/components-bridge/BasicMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { Material } from '@dcl/ecs'
import { convertTexture } from './commons/utils'

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
Material.setBasicMaterial(ecs7Entity, {
texture: convertTexture(state, payload.albedoTexture),
alphaTest: payload.alphaTest,
Expand All @@ -14,7 +14,7 @@ export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
if (Material.getOrNull(ecs7Entity)) {
Material.deleteFrom(ecs7Entity)
}
Expand Down
8 changes: 4 additions & 4 deletions src/components-bridge/BoxShape.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { MeshRenderer, MeshCollider, ColliderLayer } from '@dcl/ecs'
import { MeshRenderer, MeshCollider } from '@dcl/ecs'
import { getColliderLayer } from './commons/utils'

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

if (payload.visible) {
const uvs: number[] = payload.uvs || []
Expand All @@ -24,7 +24,7 @@ export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
if (MeshRenderer.getOrNull(ecs7Entity)) {
MeshRenderer.deleteFrom(ecs7Entity)
}
Expand Down
6 changes: 3 additions & 3 deletions src/components-bridge/CylinderShape.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { MeshRenderer, MeshCollider } from '@dcl/ecs'
import { getColliderLayer } from './commons/utils'

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

if (payload.visible) {
MeshRenderer.setCylinder(ecs7Entity, payload.radiusTop || 0, payload.radiusBottom || 1)
Expand All @@ -22,7 +22,7 @@ export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
if (MeshRenderer.getOrNull(ecs7Entity)) {
MeshRenderer.deleteFrom(ecs7Entity)
}
Expand Down
23 changes: 13 additions & 10 deletions src/components-bridge/GltfShape.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { GltfContainer } from '@dcl/ecs'
import { getColliderLayer } from './commons/utils'

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

GltfContainer.createOrReplace(ecs7Entity, {
src: payload.src,
invisibleMeshesCollisionMask: getColliderLayer(payload)
})
if (payload.visible) {
GltfContainer.createOrReplace(ecs7Entity, {
src: payload.src,
invisibleMeshesCollisionMask: getColliderLayer(payload),
visibleMeshesCollisionMask: getColliderLayer(payload)
})
} else {
GltfContainer.deleteFrom(ecs7Entity)
}
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
if (GltfContainer.getOrNull(ecs7Entity)) {
GltfContainer.deleteFrom(ecs7Entity)
}
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
GltfContainer.deleteFrom(ecs7Entity)
}
6 changes: 3 additions & 3 deletions src/components-bridge/Material.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State, ECS6_CLASS_ID } from '../types'

import { Material, TextureUnion, TextureWrapMode } from '@dcl/ecs'
Expand Down Expand Up @@ -43,7 +43,7 @@ function convertTexture(state: ECS6State, textureEntityId: any): TextureUnion |
}

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
Material.setPbrMaterial(ecs7Entity, {
texture: convertTexture(state, payload.albedoTexture),
alphaTest: payload.alphaTest,
Expand All @@ -65,7 +65,7 @@ export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
if (Material.getOrNull(ecs7Entity)) {
Material.deleteFrom(ecs7Entity)
}
Expand Down
37 changes: 37 additions & 0 deletions src/components-bridge/NftShape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { NftShape, MeshCollider } from '@dcl/ecs'
import { getColliderLayer } from './commons/utils'

function convertNftSrcToUrn(src: string): string {
src = src.substring('ethereum://'.length)
src = src.replace('/', ':')
return `urn:decentraland:ethereum:erc721:${src}`
}

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

if (payload.visible) {
NftShape.createOrReplace(ecs7Entity, {
urn: convertNftSrcToUrn(payload.src),
color: payload.color,
style: payload.style
})
} else {
NftShape.deleteFrom(ecs7Entity)
}

const colliderLayer = getColliderLayer(payload)
if (colliderLayer) {
MeshCollider.setPlane(ecs7Entity, colliderLayer)
} else {
MeshCollider.deleteFrom(ecs7Entity)
}
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
NftShape.deleteFrom(ecs7Entity)
}
29 changes: 0 additions & 29 deletions src/components-bridge/OnPointerDown.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/components-bridge/PlaneShape.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ecs7EnsureEntity } from '../ecs7/ECS7'
import { sdk7EnsureEntity } from '../ecs7/ECS7'
import { ECS6State } from '../types'

import { MeshRenderer, MeshCollider } from '@dcl/ecs'
import { getColliderLayer } from './commons/utils'

export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)

if (payload.visible) {
const uvs: number[] = payload.uvs || []
Expand All @@ -17,14 +17,14 @@ export function update(state: ECS6State, ecs6EntityId: EntityID, payload: any) {

const colliderLayer = getColliderLayer(payload)
if (colliderLayer) {
MeshCollider.setBox(ecs7Entity, colliderLayer)
MeshCollider.setPlane(ecs7Entity, colliderLayer)
} else if (MeshCollider.getOrNull(ecs7Entity)) {
MeshCollider.deleteFrom(ecs7Entity)
}
}

export function remove(state: ECS6State, ecs6EntityId: EntityID) {
const ecs7Entity = ecs7EnsureEntity(state, ecs6EntityId)
const ecs7Entity = sdk7EnsureEntity(state, ecs6EntityId)
if (MeshRenderer.getOrNull(ecs7Entity)) {
MeshRenderer.deleteFrom(ecs7Entity)
}
Expand Down
Loading

0 comments on commit ce574c7

Please sign in to comment.