Example #1
0
        /// <summary>
        /// Verifies that all of created templates in the template context have an associated
        /// definition in the list of template definitions.
        /// </summary>
        private void VerifyReferenceDefinitions(GameEngineContext engineContext, TemplateConversionContext templateContext, List<ContentTemplateSerializationFormat> templateDefinitions) {
            // We only verify template definitions if we're restoring an for an engine; it does not
            // matter if the templates are not fully instantiated if we're only in content editing
            // mode, as no game code will be executed.
            // TODO: do we really want to do this?
            if (engineContext.GameEngine.IsEmpty) {
                return;
            }

            // Get all of the ids for templates that we can restore
            HashSet<int> restoredTemplates = new HashSet<int>();
            foreach (var restoredTemplate in templateDefinitions) {
                restoredTemplates.Add(restoredTemplate.TemplateId);
            }

            // For every template that we have already created a reference for, verify that we have
            // an associated definition
            foreach (var pair in templateContext.CreatedTemplates) {
                ITemplate template = pair.Value;

                if (restoredTemplates.Contains(template.TemplateId) == false) {
                    throw new InvalidOperationException("Found template reference with id=" +
                        template.TemplateId + ", but the ITemplateGroup had no corresponding " +
                        "template definition");
                }
            }
        }
        /// <summary>
        /// Returns an entity instance for the given entity UniqueId. If an instance for the given
        /// id already exists, then it is returned. Otherwise, either a RuntimeEntity or
        /// ContentEntity is created.
        /// </summary>
        /// <param name="entityId">The id of the entity to get an instance for.</param>
        /// <param name="context">The GameEngineContext, used to determine if we should create a
        /// ContentTemplate or RuntimeTemplate instance.</param>
        public IEntity GetEntityInstance(int entityId, GameEngineContext context) {
            if (CreatedEntities.ContainsKey(entityId)) {
                return CreatedEntities[entityId];
            }

            IEntity entity;
            if (context.GameEngine.IsEmpty) {
                entity = new ContentEntity();
            }
            else {
                entity = new RuntimeEntity();
            }

            CreatedEntities[entityId] = entity;
            return entity;
        }
        /// <summary>
        /// Returns a template instance for the given TemplateId. If an instance for the given id
        /// already exists, then it is returned. Otherwise, either a RuntimeTemplate or
        /// ContentTemplate is created with an associated id based on the GameEngineContext.
        /// </summary>
        /// <param name="templateId">The id of the template to get an instance for.</param>
        /// <param name="context">The GameEngineContext, used to determine if we should create a
        /// ContentTemplate or RuntimeTemplate instance.</param>
        public ITemplate GetTemplateInstance(int templateId, GameEngineContext context) {
            if (CreatedTemplates.ContainsKey(templateId)) {
                return CreatedTemplates[templateId];
            }

            ITemplate template;
            if (context.GameEngine.IsEmpty) {
                template = new ContentTemplate(templateId);
            }
            else {
                template = new RuntimeTemplate(templateId, context.GameEngine.Value);
            }

            CreatedTemplates[templateId] = template;
            return template;
        }
Example #4
0
        /// <summary>
        /// Returns an entity instance for the given entity UniqueId. If an instance for the given
        /// id already exists, then it is returned. Otherwise, either a RuntimeEntity or
        /// ContentEntity is created.
        /// </summary>
        /// <param name="entityId">The id of the entity to get an instance for.</param>
        /// <param name="context">The GameEngineContext, used to determine if we should create a
        /// ContentTemplate or RuntimeTemplate instance.</param>
        public IEntity GetEntityInstance(int entityId, GameEngineContext context)
        {
            if (CreatedEntities.ContainsKey(entityId))
            {
                return(CreatedEntities[entityId]);
            }

            IEntity entity;

            if (context.GameEngine.IsEmpty)
            {
                entity = new ContentEntity();
            }
            else
            {
                entity = new RuntimeEntity();
            }

            CreatedEntities[entityId] = entity;
            return(entity);
        }
        /// <summary>
        /// Returns a template instance for the given TemplateId. If an instance for the given id
        /// already exists, then it is returned. Otherwise, either a RuntimeTemplate or
        /// ContentTemplate is created with an associated id based on the GameEngineContext.
        /// </summary>
        /// <param name="templateId">The id of the template to get an instance for.</param>
        /// <param name="context">The GameEngineContext, used to determine if we should create a
        /// ContentTemplate or RuntimeTemplate instance.</param>
        public ITemplate GetTemplateInstance(int templateId, GameEngineContext context)
        {
            if (CreatedTemplates.ContainsKey(templateId))
            {
                return(CreatedTemplates[templateId]);
            }

            ITemplate template;

            if (context.GameEngine.IsEmpty)
            {
                template = new ContentTemplate(templateId);
            }
            else
            {
                template = new RuntimeTemplate(templateId, context.GameEngine.Value);
            }

            CreatedTemplates[templateId] = template;
            return(template);
        }