private static void CreateEntitiesFrom(List <string> entitiesToRemove, MapDrawableBatch layer, Dictionary <string, List <NamedValue> > propertiesDictionary)
        {
            var flatRedBallLayer = SpriteManager.Layers.FirstOrDefault(item => item.Batches.Contains(layer));

            var dictionary = layer.NamedTileOrderedIndexes;

            // layer needs its position updated:
            layer.ForceUpdateDependencies();

            foreach (var propertyList in propertiesDictionary.Values)
            {
                var property =
                    propertyList.FirstOrDefault(item2 => item2.Name == "EntityToCreate" || item2.Name == "Type");

                if (!string.IsNullOrEmpty(property.Name))
                {
                    var tileName = propertyList.FirstOrDefault(item => item.Name.ToLowerInvariant() == "name").Value as string;

                    var entityType = property.Value as string;

                    if (!string.IsNullOrEmpty(entityType) && dictionary.ContainsKey(tileName))
                    {
                        IEntityFactory factory = GetFactory(entityType);

                        if (factory == null)
                        {
                            bool isEntity = typesInThisAssembly.Any(item => item.Name.Contains($".Entities.") && item.Name.EndsWith(entityType));

                            if (isEntity)
                            {
                                string message =
                                    $"The factory for entity {entityType} could not be found. To create instances of this entity, " +
                                    "set its 'CreatedByOtherEntities' property to true in Glue.";
                                throw new Exception(message);
                            }
                        }
                        else
                        {
                            entitiesToRemove.Add(entityType);
                            var indexList = dictionary[tileName];

                            foreach (var tileIndex in indexList)
                            {
                                var entity = factory.CreateNew(flatRedBallLayer) as PositionedObject;

                                ApplyPropertiesTo(entity, layer, tileIndex, propertyList);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void CreateEntitiesFrom(List <string> entitiesToRemove, MapDrawableBatch layer, Dictionary <string, List <NamedValue> > propertiesDictionary,
                                               float tileSize,
                                               InstantiationRestrictions restrictions = null)
        {
            var flatRedBallLayer = SpriteManager.Layers.FirstOrDefault(item => item.Batches.Contains(layer));

            var dictionary = layer.NamedTileOrderedIndexes;

            // layer needs its position updated:
            layer.ForceUpdateDependencies();

            foreach (var propertyList in propertiesDictionary.Values)
            {
                var property =
                    propertyList.FirstOrDefault(item2 => item2.Name == "EntityToCreate" || item2.Name == "Type");

                if (!string.IsNullOrEmpty(property.Name))
                {
                    var tileName = propertyList.FirstOrDefault(item => item.Name.ToLowerInvariant() == "name").Value as string;

                    var entityType = property.Value as string;

                    var shouldCreateEntityType =
                        !string.IsNullOrEmpty(entityType) && dictionary.ContainsKey(tileName);

                    if (shouldCreateEntityType && restrictions?.InclusiveList != null)
                    {
                        shouldCreateEntityType = restrictions.InclusiveList.Contains(entityType);
                    }

                    if (shouldCreateEntityType)
                    {
                        IEntityFactory factory = GetFactory(entityType);

                        if (factory == null && CreationFunction == null)
                        {
                            bool isEntity = typesInThisAssembly.Any(item => item.Name.Contains($".Entities.") && item.Name.EndsWith(entityType));

                            if (isEntity)
                            {
                                string message =
                                    $"The factory for entity {entityType} could not be found. To create instances of this entity, " +
                                    "set its 'CreatedByOtherEntities' property to true in Glue.";
                                throw new Exception(message);
                            }
                        }
                        else
                        {
                            var createdEntityOfThisType = false;

                            var indexList = dictionary[tileName];

                            foreach (var tileIndex in indexList)
                            {
                                var shouldCreate = true;
                                var bounds       = restrictions?.Bounds;
                                if (bounds != null)
                                {
                                    layer.GetBottomLeftWorldCoordinateForOrderedTile(tileIndex, out float x, out float y);
                                    x           += tileSize / 2.0f;
                                    y           += tileSize / 2.0f;
                                    shouldCreate = bounds.IsPointInside(x, y);
                                }

                                if (shouldCreate)
                                {
                                    PositionedObject entity = null;
                                    if (factory != null)
                                    {
                                        entity = factory.CreateNew(flatRedBallLayer) as PositionedObject;
                                    }
                                    else if (CreationFunction != null)
                                    {
                                        entity = CreationFunction(entityType);
                                        // todo - need to support moving to layer
                                    }

                                    if (entity != null)
                                    {
                                        ApplyPropertiesTo(entity, layer, tileIndex, propertyList);
                                        createdEntityOfThisType = true;
                                    }
                                }
                            }
                            if (createdEntityOfThisType)
                            {
                                entitiesToRemove.Add(entityType);
                            }
                        }
                    }
                }
            }
        }