<commit_before>// ---------------------------------------------------------------------------------------------------------------------
// Brief Description of entityman.js.
//
// @module entityman.js
// ---------------------------------------------------------------------------------------------------------------------

var Promise = require('bluebird');

// ---------------------------------------------------------------------------------------------------------------------

function EntityManagerFactory(socket, avatar, sceneMan)
{
    function EntityManager()
    {
        this.entities = {};

        // Because browserify can't support dynamic requires (https://github.com/substack/node-browserify/issues/377), we
        // are forced to use literal strings, and build a map of the server's behavior string, and ours.
        this.behaviors = {
            './behaviors/ship': require('./behaviors/ship')
        };

        // Listen for incoming messages
        socket.on('create entity', this._handleCreate.bind(this));
        socket.on('inhabit entity', this._handleInhabit.bind(this));
        socket.on('update entity', this._handleUpdate.bind(this));
        socket.on('destroy entity', this._handleDestroy.bind(this));
    } // end EntityManager

    /**
     * Creates a new entity from the entity definition.
     *
     * @param {object} entityDef - A definition for the entity.
     */
    EntityManager.prototype.create = function(entityDef)
    {
        if(entityDef.id in this.entities)
        {
            console.error('Attempting to add an entity("%s") that already exists. Ignoring message.', entityDef.id);
            return Promise.resolve(this.entities[entityDef.id]);
        }
        else
        {
            // We instantiate the behavior class as the entity. This way, internally, behaviors can simply use `this` to
            // refer to the entity, as opposed to having to pass the entity into the behaviors.
            var BehaviorClass = this.behaviors[entityDef.behavior];
            var entity = new BehaviorClass(entityDef, socket);

            // Add the newly created entity to our list of entities.
            this.entities[entity.id] = entity;

            if(entityDef.model)
            {
                console.debug('loading model:', entityDef.model);
                return sceneMan.loadMesh(entityDef.model)
                    .then(function(mesh)
                    {
                        entity.mesh = mesh;
                        sceneMan.playerCamera.target = mesh;

                        console.debug('Entity added successfully!', entity);
                        return entity;
                    });
            }
            else
            {
                return Promise.resolve(entity);
            }// end if
        } // end if
    }; // end EntityManager

    /**
     * Removed an entity from the simulation.
     *
     *  @param {String} entityID - The id of the entity to remove.
     */
    EntityManager.prototype.remove = function(entityID)
    {
        //TODO: We will want to unload any models we've loaded for this entity.
        delete this.entities[entityID];

        return Promise.resolve();
    }; // end EntityManager

    // ---------------------------------------------------------------------------------------------------------------------
    // Event handlers
    // ---------------------------------------------------------------------------------------------------------------------

    EntityManager.prototype._handleCreate = function(entityDef)
    {
        this.create(entityDef);
    }; // end _handleCreate

    EntityManager.prototype._handleInhabit = function(entityDef)
    {
        this.create(entityDef).then(function(entity)
        {
            avatar.inhabitEntity(entity);
        });
    }; // end _handleInhabit

    EntityManager.prototype._handleUpdate = function(message)
    {
        console.error('Update Not Implemented! Message:', message);
    }; // end _handleUpdate

    EntityManager.prototype._handleDestroy = function(message)
    {
        console.error('Destroy Not Implemented! Message:', message);
    }; // end _handleDestroy

    return new EntityManager();
} // end EntityManagerFactory

// ---------------------------------------------------------------------------------------------------------------------

angular.module('rfi-client.services').service('EntityManager', [
        'SocketService',
        'AvatarService',
        'SceneManager',
        EntityManagerFactory
    ]);

// ---------------------------------------------------------------------------------------------------------------------<commit_msg>Use Angular factories for entity creation<commit_after>// ---------------------------------------------------------------------------------------------------------------------
// Brief Description of entityman.js.
//
// @module entityman.js
// ---------------------------------------------------------------------------------------------------------------------

var Promise = require('bluebird');

// ---------------------------------------------------------------------------------------------------------------------

function EntityManagerFactory($injector, socket, avatar, sceneMan)
{
    function EntityManager()
    {
        this.entities = {};

        // Mapping between server name and our factory name
        this.behaviors = {
            './behaviors/ship': 'ShipEntity'
        };

        // Listen for incoming messages
        socket.on('create entity', this._handleCreate.bind(this));
        socket.on('inhabit entity', this._handleInhabit.bind(this));
        socket.on('update entity', this._handleUpdate.bind(this));
        socket.on('destroy entity', this._handleDestroy.bind(this));
    } // end EntityManager

    /**
     * Creates a new entity from the entity definition.
     *
     * @param {object} entityDef - A definition for the entity.
     */
    EntityManager.prototype.create = function(entityDef)
    {
        if(entityDef.id in this.entities)
        {
            console.error('Attempting to add an entity("%s") that already exists. Ignoring message.', entityDef.id);
            return Promise.resolve(this.entities[entityDef.id]);
        }
        else
        {
            // We instantiate the behavior class as the entity. This way, internally, behaviors can simply use `this` to
            // refer to the entity, as opposed to having to pass the entity into the behaviors.
            var BehaviorFactoryName = this.behaviors[entityDef.behavior];
            var entity = $injector.get(BehaviorFactoryName)(entityDef, socket);

            // Add the newly created entity to our list of entities.
            this.entities[entity.id] = entity;

            if(entityDef.model)
            {
                console.debug('loading model:', entityDef.model);
                return sceneMan.loadMesh(entityDef.model)
                    .then(function(mesh)
                    {
                        entity.mesh = mesh;

                        console.debug('Entity added successfully!', entity);
                        return entity;
                    });
            }
            else
            {
                return Promise.resolve(entity);
            }// end if
        } // end if
    }; // end EntityManager

    /**
     * Removed an entity from the simulation.
     *
     *  @param {String} entityID - The id of the entity to remove.
     */
    EntityManager.prototype.remove = function(entityID)
    {
        //TODO: We will want to unload any models we've loaded for this entity.
        delete this.entities[entityID];

        return Promise.resolve();
    }; // end EntityManager

    // ---------------------------------------------------------------------------------------------------------------------
    // Event handlers
    // ---------------------------------------------------------------------------------------------------------------------

    EntityManager.prototype._handleCreate = function(entityDef)
    {
        this.create(entityDef);
    }; // end _handleCreate

    EntityManager.prototype._handleInhabit = function(entityDef)
    {
        this.create(entityDef).then(function(entity)
        {
            avatar.inhabitEntity(entity);
        });
    }; // end _handleInhabit

    EntityManager.prototype._handleUpdate = function(message)
    {
        console.error('Update Not Implemented! Message:', message);
    }; // end _handleUpdate

    EntityManager.prototype._handleDestroy = function(message)
    {
        console.error('Destroy Not Implemented! Message:', message);
    }; // end _handleDestroy

    return new EntityManager();
} // end EntityManagerFactory

// ---------------------------------------------------------------------------------------------------------------------

angular.module('rfi-client.services').service('EntityManager', [
        '$injector',
        'SocketService',
        'AvatarService',
        'SceneManager',
        EntityManagerFactory
    ]);

// ---------------------------------------------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.26699978275038017, 'fraction_numerical': 0.000325874429719748, 'mean_word_length': 1.2052694610778443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493105', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1924, 'n_words': 495}