Source: actor/actor.js

import { Object3D, Group, Vector3, Quaternion, Layers } from '../../node_modules/three/build/three.module.js';
import { Reporter } from '../reporter/reporter.js';
import { InformationSource } from '../reporter/information_source.js';
import { BuildableComponent } from '../package/component/buildable_component.js';
import { StateTracker } from '../state_tracker.js';
import { Project } from '../project.js';

/**
 * Actor
 * @event Actor#bodychange emitted whenever the body changes, i.e. when a configuration is changed (but not when an element is dragged or rotated)
 */

class Actor extends StateTracker {

    /**
     * @param {Reporter} reporter 
     * @param {Object} settings 
     */

    constructor(reporter, settings) {
        
        super(reporter, settings);

        this.addEvent('bodychange');

        this.hiddenBody = new Group();
        this.body = new Group();
        this.body.name = "Actor"
        this.body.userData.origin = this;
        this.body.castShadow = true;
        this.body.receiveShadow = true;

        

        this.visible = true;
    }

    /** @type {Boolean} */
    visible;



    show() {

        //console.log('show', this.label)

        const hiddenActorLayer = new Layers()
        hiddenActorLayer.set( Project.layerMap.hiddenActors );

        this.body.traverse( function( child ) {
            
            //isEnabled method is not yet available in the threejs version we are using! Resorting to using test
            if( child.layers.test( hiddenActorLayer ) ){
                
                child.layers.set( Project.layerMap.visibleActors ) 
            }

        })

        this.visible = true;
    }


    hide() {

        console.log('hide', this.label)

        const visibleActorLayer = new Layers()
        visibleActorLayer.set( Project.layerMap.visibleActors );
       

        this.body.traverse( function( child ) { 

            //isEnabled method is not yet available in the threejs version we are using! Resorting to using test
            if( child.layers.test( visibleActorLayer ) ){
                child.layers.set( Project.layerMap.hiddenActors ) 
            }

        })

        this.visible = false;
    }


    clearBody() {
        while (this.body.children.length > 0) {
            this.body.remove(this.body.children[0]);
        }
    }


    /**
     * @param {Object3D} newContent 
     */

    async updateBody(newContent, quaternion, position, modTransform) {
        
        this.clearBody()

        this.body.add(newContent);

        if ( quaternion ) {
            this.body.quaternion.copy( quaternion );
        }

        if ( position) {
            if (modTransform && modTransform.translation) {
                this.body.position.copy( new Vector3().addVectors( position, modTransform.translation ));
            }
            else {
                this.body.position.copy( position );
            }
        }

        await this.emit('bodychange', this.body);
    }


    /** @type {Group} */

    body = new Group();
}

export { Actor };