Beispiel #1
0
        public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap, InstantiationRestrictions restrictions = null)
        {
            if (layeredTileMap != null)
            {
                var entitiesToRemove = new List <string>();

                foreach (var layer in layeredTileMap.MapLayers)
                {
                    CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.TileProperties, layeredTileMap.WidthPerTile ?? 16, restrictions);
                }
                if (CurrentSettings.RemoveTileObjectsAfterEntityCreation)
                {
                    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)
                {
                    CreateEntitiesFromCircles(layeredTileMap, shapeCollection, restrictions);

                    CreateEntitiesFromRectangles(layeredTileMap, shapeCollection, restrictions);

                    CreateEntitiesFromPolygons(layeredTileMap, shapeCollection, restrictions);
                }
            }
        }
Beispiel #2
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)
                    {
                        PositionedObject entity = CreateEntity(entityType);
                        if (entity != null)
                        {
                            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 #3
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);
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
        private static void CreateEntitiesFromRectangles(LayeredTileMap layeredTileMap, ShapeCollection shapeCollection, InstantiationRestrictions restrictions)
        {
            var rectangles = shapeCollection.AxisAlignedRectangles;

            for (int i = rectangles.Count - 1; i > -1; i--)
            {
                var rectangle = rectangles[i];
                if (!string.IsNullOrEmpty(rectangle.Name) && layeredTileMap.ShapeProperties.ContainsKey(rectangle.Name))
                {
                    var properties           = layeredTileMap.ShapeProperties[rectangle.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 = rectangle.Name;
                            ApplyPropertiesTo(entity, properties, rectangle.Position);

                            if (CurrentSettings.RemoveTileObjectsAfterEntityCreation)
                            {
                                shapeCollection.AxisAlignedRectangles.Remove(rectangle);
                            }

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