Beispiel #1
0
        private static void CreateEntitiesFromCircles(LayeredTileMap layeredTileMap, ShapeCollection shapeCollection)
        {
            var circles = shapeCollection.Circles;

            for (int i = circles.Count - 1; i > -1; i--)
            {
                var circle = circles[i];
                if (!string.IsNullOrEmpty(circle.Name) && layeredTileMap.ShapeProperties.ContainsKey(circle.Name))
                {
                    var properties           = layeredTileMap.ShapeProperties[circle.Name];
                    var entityAddingProperty = properties.FirstOrDefault(item => item.Name == "EntityToCreate" || item.Name == "Type");

                    var entityType = entityAddingProperty.Value as string;

                    if (!string.IsNullOrEmpty(entityType))
                    {
                        IEntityFactory factory = GetFactory(entityType);

                        var entity = factory.CreateNew(null) as PositionedObject;

                        entity.Name = circle.Name;
                        ApplyPropertiesTo(entity, properties, circle.Position);
                        shapeCollection.Circles.Remove(circle);

                        if (entity is Math.Geometry.ICollidable)
                        {
                            var entityCollision = (entity as Math.Geometry.ICollidable).Collision;
                            entityCollision.Circles.Add(circle);
                            circle.AttachTo(entity, false);
                        }
                    }
                }
            }
        }
        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);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private static PositionedObject CreateEntity(string entityType)
        {
            PositionedObject entity  = null;
            IEntityFactory   factory = GetFactory(entityType);

            if (factory != null)
            {
                entity = factory.CreateNew(null) as PositionedObject;
            }
            else if (CreationFunction != null)
            {
                entity = CreationFunction(entityType);
            }

            return(entity);
        }
Beispiel #4
0
        public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap)
        {
            var entitiesToRemove = new List<string>();

            foreach (var layer in layeredTileMap.MapLayers)
            {
                CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.TileProperties);
            }
            foreach (var entityToRemove in entitiesToRemove)
            {
                string remove = entityToRemove;
                layeredTileMap.RemoveTiles(t => t.Any(item => (item.Name == "EntityToCreate" || item.Name == "Type") && item.Value as string == remove), layeredTileMap.TileProperties);
            }
            foreach (var shapeCollection in layeredTileMap.ShapeCollections)
            {
                var polygons = shapeCollection.Polygons;
                for (int i = polygons.Count - 1; i > -1; i--)
                {
                    var polygon = polygons[i];
                    if (!string.IsNullOrEmpty(polygon.Name) && layeredTileMap.ShapeProperties.ContainsKey(polygon.Name))
                    {
                        var properties = layeredTileMap.ShapeProperties[polygon.Name];
                        var entityAddingProperty = properties.FirstOrDefault(item => item.Name == "EntityToCreate");

                        var entityType = entityAddingProperty.Value as string;
                        if (!string.IsNullOrEmpty(entityType))
                        {
                            IEntityFactory factory = GetFactory(entityType);

                            var entity = factory.CreateNew(null) as PositionedObject;

                            entity.Name = polygon.Name;
                            ApplyPropertiesTo(entity, properties, polygon.Position);
                            shapeCollection.Polygons.Remove(polygon);

                            if (entity is Math.Geometry.ICollidable)
                            {
                                var entityCollision = (entity as Math.Geometry.ICollidable).Collision;
                                entityCollision.Polygons.Add(polygon);
                                polygon.AttachTo(entity, false);
                            }

                        }
                    }
                }
            }
        }
Beispiel #5
0
        private static void CreateEntitiesFromPolygons(LayeredTileMap layeredTileMap, Math.Geometry.ShapeCollection shapeCollection, InstantiationRestrictions restrictions)
        {
            var polygons = shapeCollection.Polygons;

            for (int i = polygons.Count - 1; i > -1; i--)
            {
                var polygon = polygons[i];
                if (!string.IsNullOrEmpty(polygon.Name) && layeredTileMap.ShapeProperties.ContainsKey(polygon.Name))
                {
                    var properties           = layeredTileMap.ShapeProperties[polygon.Name];
                    var entityAddingProperty = properties.FirstOrDefault(item => item.Name == "EntityToCreate" || item.Name == "Type");

                    var entityType   = entityAddingProperty.Value as string;
                    var shouldCreate = !string.IsNullOrEmpty(entityType);
                    if (restrictions?.InclusiveList != null)
                    {
                        shouldCreate = restrictions.InclusiveList.Contains(entityType);
                    }
                    if (shouldCreate)
                    {
                        IEntityFactory factory = GetFactory(entityType);
                        if (factory != null)
                        {
                            var entity = factory.CreateNew(null) as PositionedObject;

                            entity.Name = polygon.Name;
                            ApplyPropertiesTo(entity, properties, polygon.Position);

                            if (CurrentSettings.RemoveTileObjectsAfterEntityCreation)
                            {
                                shapeCollection.Polygons.Remove(polygon);
                            }

                            if (entity is Math.Geometry.ICollidable)
                            {
                                var entityCollision = (entity as Math.Geometry.ICollidable).Collision;
                                entityCollision.Polygons.Add(polygon);
                                polygon.AttachTo(entity, false);
                            }
                        }
                    }
                }
            }
        }
Beispiel #6
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);
                            }
                        }
                    }
                }
            }
        }