How to use the decentraland-ecs.engine.addEntity function in decentraland-ecs

To help you get started, we’ve selected a few decentraland-ecs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github decentraland / builder / src / ecsScene / scene.ts View on Github external
if (scriptGroup.hasEntity(entity)) {
            // ...remove the placeholder
            entity.removeComponent(GLTFShape)
            // ...create the host entity
            const transform = entity.getComponent(Transform)
            const hostTransform = new Transform()
            hostTransform.position.copyFrom(transform.position)
            hostTransform.rotation.copyFrom(transform.rotation)
            hostTransform.scale.copyFrom(transform.scale)
            const name = (entity as any).name // TODO fix this on the kernel's side
            const placeholder = Object.values(engine.entities).find(entity => (entity as Entity).name === name)
            if (placeholder) {
              engine.removeEntity(placeholder)
            }
            const host = new Entity(name)
            engine.addEntity(host)
            host.addComponent(hostTransform)
            // ...and execute the script on the host entity
            const { assetId, values } = entity.getComponent(Script)
            const script = scriptInstances.get(assetId)!
            const channel = createChannel('channel-id', host as any, new MockMessageBus())
            script.spawn(host, values, channel)
          }
        }
      } else {
        for (const entityId in engine.entities) {
          const entity = engine.entities[entityId]
          const staticEntities = engine.getComponentGroup(StaticEntity)
          if (!staticEntities.hasEntity(entity)) {
            entity.addComponentOrReplace(gizmo)
          }
        }
github decentraland / builder / src / ecsScene / scene.ts View on Github external
function createEntities(entities: Record) {
  for (let id in entities) {
    const builderEntity = entities[id]
    let entity: IEntity = engine.entities[id]

    if (!entity) {
      entity = new Entity(builderEntity.name)
      ;(entity as any).uuid = id

      if (!builderEntity.disableGizmos) {
        entity.addComponentOrReplace(gizmo)
      } else {
        entity.addComponentOrReplace(staticEntity)
      }

      engine.addEntity(entity)
    }

    for (let componentId of builderEntity.components) {
      const component = getComponentById(componentId)
      if (component) {
        entity.addComponentOrReplace(component)
        if (component instanceof Script) {
          entity.addComponentOrReplace(smartItemComponent)
        }
      }
    }
  }
}