/// <summary>
        /// Requests an entity from the entity manager.
        /// </summary>
        /// <typeparam name="T">The entity type.</typeparam>
        /// <param name="position">The starting position of the entity.</param>
        /// <returns>A reference to the entity.</returns>
        public IEntity RequestEntity <T>(Vector3 position) where T : AbstractEntity, new()
        {
            // Store the type of entity requested.
            Type entityType = typeof(T);

            // Variable to hold requested entity.
            AbstractEntity entity = null;

            // Check if there is an inactive entity.
            if (inactiveEntities.ContainsKey(entityType))
            {
                if (inactiveEntities[entityType].Count > 0)
                {
                    // Get entity.
                    entity = inactiveEntities[entityType].ElementAt(0);

                    // Set ID.
                    entity.Setup(++entitiesCreated);

                    // Remove entity from the map.
                    inactiveEntities[entityType].RemoveAt(0);
                }
            }

            // Else, create new entity.
            else
            {
                // Create new entity.
                entity = new T();

                // Set ID.
                entity.Setup(++entitiesCreated);

                // Check if entity is loadable.
                // If loadable, add to load system.
                if (entity is ILoadable)
                {
                    loadSystem.AddEntity(entity);
                }
            }

            // Initialise entity.
            entity.Initialise(position, this, sceneStateManager);

            // Add entity to active pool.
            activeEntities.Add(entity.Id, entity);

            // Return entity as IEntity.
            return(entity);
        }
Beispiel #2
0
        /// <summary>
        /// Adds an entity to a scene.
        /// </summary>
        /// <param name="sceneId">The ID of the scene.</param>
        /// <param name="entity">The entity to add.</param>
        public void AddEntity(int sceneId, IEntity entity)
        {
            // Check if scene exists.
            if (scenes.ContainsKey(sceneId))
            {
                // Get scene.
                IScene scene = scenes[sceneId];

                // Add entity.
                scenes[sceneId].AddEntity(entity);

                // If scene is running, add to systems.
                if (((ISceneState)scene).State == SceneState.Running)
                {
                    collisionSystem.AddEntity(entity);
                    inputSystem.AddEntity(entity);
                    if (entity is IRenderable)
                    {
                        renderSystem.AddEntity(entity);
                    }
                    if (entity is IUpdatable)
                    {
                        updateSystem.AddEntity(entity);
                    }
                }

                // Else if, scene is paused, add to render system only.
                else if (((ISceneState)scene).State == SceneState.Paused)
                {
                    if (entity is IRenderable)
                    {
                        renderSystem.AddEntity(entity);
                    }
                }
            }
        }