import { Observable } from '../Util/Observable';
import { DynamicTreeCollisionProcessor } from './Index';
import { BodyComponent } from './BodyComponent';
import { watchDeep } from '../Util/Watch';
export class PhysicsWorld {
    get config() {
        return watchDeep(this._config, (change) => {
            this.$configUpdate.notifyAll(change);
        });
    }
    set config(newConfig) {
        this._config = newConfig;
        this.$configUpdate.notifyAll(newConfig);
    }
    /**
     * Spatial data structure for locating potential collision pairs and ray casts
     */
    get collisionProcessor() {
        if (this._configDirty) {
            this._configDirty = false;
            // preserve tracked colliders if config updates
            const colliders = this._collisionProcessor.getColliders();
            this._collisionProcessor = new DynamicTreeCollisionProcessor(this._config);
            for (const collider of colliders) {
                this._collisionProcessor.track(collider);
            }
        }
        return this._collisionProcessor;
    }
    constructor(config) {
        this.$configUpdate = new Observable();
        this._configDirty = false;
        this.config = config;
        this.$configUpdate.subscribe((config) => {
            this._configDirty = true;
            BodyComponent.updateDefaultPhysicsConfig(config.bodies);
        });
        this._collisionProcessor = new DynamicTreeCollisionProcessor(this.config);
    }
    /**
     * Raycast into the scene's physics world
     * @param ray
     * @param options
     */
    rayCast(ray, options) {
        return this.collisionProcessor.rayCast(ray, options);
    }
}
//# sourceMappingURL=PhysicsWorld.js.map