Exemple #1
0
        private static void CreateEntitiesFromPolygons(LayeredTileMap layeredTileMap, Math.Geometry.ShapeCollection shapeCollection)
        {
            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;
                    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);
                        }
                    }
                }
            }
        }
Exemple #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);
                            }
                        }
                    }
                }
            }
        }