<commit_before>﻿using $PROJECT_NAMESPACE$.DataTypes;
using FlatRedBall.TileGraphics;
using System;
using System.Collections.Generic;
using System.Linq;
using $PROJECT_NAMESPACE$.Performance;
using FlatRedBall.Graphics;
using System.Reflection;
using TMXGlueLib.DataTypes;


namespace FlatRedBall.TileEntities
{
    public static class TileEntityInstantiator
    {

        /// <summary>
        /// Creates entities from a single layer for any tile with the EntityToCreate property.
        /// </summary>
        /// <param name="mapLayer">The layer to create entities from.</param>
        /// <param name="layeredTileMap">The map which contains the mapLayer instance.</param>
        public static void CreateEntitiesFrom(MapDrawableBatch mapLayer, LayeredTileMap layeredTileMap)
        {
            var entitiesToRemove = new List<string>();

            CreateEntitiesFrom(entitiesToRemove, mapLayer, layeredTileMap.Properties);

            foreach (var entityToRemove in entitiesToRemove)
            {
                string remove = entityToRemove;
                mapLayer.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties);
            }

        }

        public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap)
        {
            var entitiesToRemove = new List<string>();

            foreach (var layer in layeredTileMap.MapLayers)
            {
                CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.Properties);
            }
            foreach (var entityToRemove in entitiesToRemove)
            {
                string remove = entityToRemove;
                layeredTileMap.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties);
            }
            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.Properties.ContainsKey(polygon.Name))
                    {
                        var properties = layeredTileMap.Properties[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;

                            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);<commit_msg>set entity name to polygon name<commit_after>﻿using $PROJECT_NAMESPACE$.DataTypes;
using FlatRedBall.TileGraphics;
using System;
using System.Collections.Generic;
using System.Linq;
using $PROJECT_NAMESPACE$.Performance;
using FlatRedBall.Graphics;
using System.Reflection;
using TMXGlueLib.DataTypes;


namespace FlatRedBall.TileEntities
{
    public static class TileEntityInstantiator
    {

        /// <summary>
        /// Creates entities from a single layer for any tile with the EntityToCreate property.
        /// </summary>
        /// <param name="mapLayer">The layer to create entities from.</param>
        /// <param name="layeredTileMap">The map which contains the mapLayer instance.</param>
        public static void CreateEntitiesFrom(MapDrawableBatch mapLayer, LayeredTileMap layeredTileMap)
        {
            var entitiesToRemove = new List<string>();

            CreateEntitiesFrom(entitiesToRemove, mapLayer, layeredTileMap.Properties);

            foreach (var entityToRemove in entitiesToRemove)
            {
                string remove = entityToRemove;
                mapLayer.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties);
            }

        }

        public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap)
        {
            var entitiesToRemove = new List<string>();

            foreach (var layer in layeredTileMap.MapLayers)
            {
                CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.Properties);
            }
            foreach (var entityToRemove in entitiesToRemove)
            {
                string remove = entityToRemove;
                layeredTileMap.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties);
            }
            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.Properties.ContainsKey(polygon.Name))
                    {
                        var properties = layeredTileMap.Properties[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);
{'fraction_non_alphanumeric': 0.08183453237410072, 'fraction_numerical': 0.0005995203836930455, 'mean_word_length': 1.8725785622040465, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2017472', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1552, 'n_words': 366}