Ejemplo n.º 1
0
        public static TileNodeNetwork CreateFromNames(LayeredTileMap layeredTileMap, DirectionalType directionalType,
                                                      ICollection <string> names)
        {
            Func <List <TMXGlueLib.DataTypes.NamedValue>, bool> predicate = (list) =>
            {
                var toReturn = false;

                foreach (var namedValue in list)
                {
                    if (namedValue.Name == "Name")
                    {
                        var valueAsString = namedValue.Value as string;

                        if (!string.IsNullOrEmpty(valueAsString) && names.Contains(valueAsString))
                        {
                            toReturn = true;
                            break;
                        }
                    }
                }

                return(toReturn);
            };

            return(CreateFrom(layeredTileMap, directionalType, predicate));
        }
Ejemplo n.º 2
0
        public PositionedNode AddAndLinkTiledNode(int x, int y, DirectionalType directionalType)
        {
            PositionedNode node = null;

            if (mTiledNodes[x][y] != null)
            {
                node = mTiledNodes[x][y];
            }
            else
            {
                node = base.AddNode();
                mTiledNodes[x][y] = node;
            }

            node.Position.X = mXSeed + x * mGridSpacing;
            node.Position.Y = mYSeed + y * mGridSpacing;

            // Now attach to the adjacent tiles
            AttachNodeToNodeAtIndex(node, x, y + 1);
            AttachNodeToNodeAtIndex(node, x + 1, y);
            AttachNodeToNodeAtIndex(node, x, y - 1);
            AttachNodeToNodeAtIndex(node, x - 1, y);
            if (directionalType == DirectionalType.Eight)
            {
                AttachNodeToNodeAtIndex(node, x - 1, y + 1);
                AttachNodeToNodeAtIndex(node, x + 1, y + 1);
                AttachNodeToNodeAtIndex(node, x + 1, y - 1);
                AttachNodeToNodeAtIndex(node, x - 1, y - 1);
            }

            return(node);
        }
Ejemplo n.º 3
0
        public static TileNodeNetwork CreateFromTypes(LayeredTileMap layeredTileMap, DirectionalType directionalType, ICollection <string> types)
        {
            bool CreateFromTypesPredicate(List <NamedValue> list)
            {
                var toReturn = false;

                foreach (var namedValue in list)
                {
                    if (namedValue.Name == "Type")
                    {
                        var valueAsString = namedValue.Value as string;

                        if (!string.IsNullOrEmpty(valueAsString) && types.Contains(valueAsString))
                        {
                            toReturn = true;
                            break;
                        }
                    }
                }

                return(toReturn);
            }

            return(CreateFrom(layeredTileMap, directionalType, CreateFromTypesPredicate));
        }
Ejemplo n.º 4
0
        public static TileNodeNetwork CreateFrom(LayeredTileMap layeredTileMap, DirectionalType directionalType,
                                                 Func <List <TMXGlueLib.DataTypes.NamedValue>, bool> predicate)
        {
            var numberOfTilesWide =
                MathFunctions.RoundToInt(layeredTileMap.Width / layeredTileMap.WidthPerTile.Value);
            var numberOfTilesTall =
                MathFunctions.RoundToInt(layeredTileMap.Height / layeredTileMap.HeightPerTile.Value);

            var tileWidth = layeredTileMap.WidthPerTile.Value;

            var dimensionHalf = tileWidth / 2.0f;

            TileNodeNetwork nodeNetwork = new TileNodeNetwork(
                0 + dimensionHalf,
                -layeredTileMap.Height + tileWidth / 2.0f,
                tileWidth,
                numberOfTilesWide,
                numberOfTilesTall,
                directionalType);


            var properties = layeredTileMap.TileProperties;

            foreach (var kvp in properties)
            {
                string name        = kvp.Key;
                var    namedValues = kvp.Value;

                if (predicate(namedValues))
                {
                    foreach (var layer in layeredTileMap.MapLayers)
                    {
                        var dictionary = layer.NamedTileOrderedIndexes;

                        if (dictionary.ContainsKey(name))
                        {
                            var indexList = dictionary[name];

                            foreach (var index in indexList)
                            {
                                float left;
                                float bottom;
                                layer.GetBottomLeftWorldCoordinateForOrderedTile(index, out left, out bottom);

                                var centerX = left + dimensionHalf;
                                var centerY = bottom + dimensionHalf;

                                nodeNetwork.AddAndLinkTiledNodeWorld(centerX, centerY);
                            }
                        }
                    }
                }
            }

            nodeNetwork.Visible = true;

            return(nodeNetwork);
        }
Ejemplo n.º 5
0
        public static TileNodeNetwork CreateFrom(LayeredTileMap layeredTileMap, DirectionalType directionalType,
                                                 Func <List <TMXGlueLib.DataTypes.NamedValue>, bool> predicate)
        {
            TileNodeNetwork nodeNetwork = CreateTileNodeNetwork(layeredTileMap, directionalType);

            FillFromPredicate(nodeNetwork, layeredTileMap, predicate);

            nodeNetwork.Visible = true;

            return(nodeNetwork);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new, empty TileNodeNetwork matching the arguments.
        /// </summary>
        /// <param name="xOrigin">The X position of the left-most nodes. This, along with the ySeed, define the bottom-left of the node network.
        /// For tile maps this should be the center X of the first tile column (typically TileWidth / 2).</param>
        /// <param name="yOrigin">The y position of the bottom-most nodes. This, along with xSeed, define the bottom-left of the node network.
        /// For tile maps this should be the center Y of the bottom tile row.
        /// If the top-left of the map is at 0,0, then this value would be (-EntireMapHeight + TileHeight/2)</param>
        /// <param name="gridSpacing">The X and Y distance between each node. That is, the X distance between two adjacent nodes (assumed to be equal to the Y distance). For a tile map this will equal the width of a tile.</param>
        /// <param name="numberOfXTiles">The number of nodes vertically.</param>
        /// <param name="numberOfYTiles">The number of nodes horizontally.</param>
        /// <param name="directionalType">Whether to create a Four-way or Eight-way node network. Eight creates diagonal links, enabling diagonal movement when following the node network.</param>
        public TileNodeNetwork(float xOrigin, float yOrigin, float gridSpacing, int numberOfXTiles,
                               int numberOfYTiles, DirectionalType directionalType)
        {
            mCosts = new float[PropertyIndexSize]; // Maybe expand this to 64 if we ever move to a long bit field?

            OccupiedCircleRadius = .5f;
            mTiledNodes          = new PositionedNode[numberOfXTiles][];
            mNumberOfXTiles      = numberOfXTiles;
            mNumberOfYTiles      = numberOfYTiles;
            mDirectionalType     = directionalType;
            mXSeed       = xOrigin;
            mYSeed       = yOrigin;
            mGridSpacing = gridSpacing;

            // Do an initial loop to create the arrays so that
            // linking works properly
            for (int x = 0; x < numberOfXTiles; x++)
            {
                mTiledNodes[x] = new PositionedNode[numberOfYTiles];
            }
        }
Ejemplo n.º 7
0
        public TileNodeNetwork(float xSeed, float ySeed, float gridSpacing, int numberOfXTiles,
            int numberOfYTiles, DirectionalType directionalType)
        {
            mCosts = new float[PropertyIndexSize]; // Maybe expand this to 64 if we ever move to a long bit field?

            OccupiedCircleRadius = .5f;
            mTiledNodes = new PositionedNode[numberOfXTiles][];
            mNumberOfXTiles = numberOfXTiles;
            mNumberOfYTiles = numberOfYTiles;
            mDirectionalType = directionalType;
            mXSeed = xSeed;
            mYSeed = ySeed;
            mGridSpacing = gridSpacing;

            // Do an initial loop to create the arrays so that
            // linking works properly
            for (int x = 0; x < numberOfXTiles; x++)
            {
                mTiledNodes[x] = new PositionedNode[numberOfYTiles];
            }

        }
Ejemplo n.º 8
0
        public static TileNodeNetwork CreateFromTilesWithProperties(LayeredTileMap layeredTileMap, DirectionalType directionalType,
                                                                    ICollection <string> types)
        {
            Func <List <TMXGlueLib.DataTypes.NamedValue>, bool> predicate = (list) =>
            {
                var toReturn = false;

                foreach (var namedValue in list)
                {
                    if (types.Contains(namedValue.Name))
                    {
                        toReturn = true;
                        break;
                    }
                }

                return(toReturn);
            };

            return(CreateFrom(layeredTileMap, directionalType, predicate));
        }
Ejemplo n.º 9
0
        private static TileNodeNetwork CreateTileNodeNetwork(LayeredTileMap layeredTileMap, DirectionalType directionalType)
        {
            var numberOfTilesWide =
                MathFunctions.RoundToInt(layeredTileMap.Width / layeredTileMap.WidthPerTile.Value);
            var numberOfTilesTall =
                MathFunctions.RoundToInt(layeredTileMap.Height / layeredTileMap.HeightPerTile.Value);

            var tileWidth = layeredTileMap.WidthPerTile.Value;

            var dimensionHalf = tileWidth / 2.0f;

            TileNodeNetwork nodeNetwork = new TileNodeNetwork(
                0 + dimensionHalf,
                -layeredTileMap.Height + tileWidth / 2.0f,
                tileWidth,
                numberOfTilesWide,
                numberOfTilesTall,
                directionalType);

            return(nodeNetwork);
        }
Ejemplo n.º 10
0
        public static TileNodeNetwork CreateFromEmptyTiles(MapDrawableBatch mapDrawableBatch, LayeredTileMap layeredTileMap, DirectionalType directionalType)
        {
            TileNodeNetwork toReturn = CreateTileNodeNetwork(layeredTileMap, directionalType);

            toReturn.FillCompletely();

            var offset = new Microsoft.Xna.Framework.Vector3(layeredTileMap.WidthPerTile.Value / 2, layeredTileMap.HeightPerTile.Value / 2, 0);

            for (int i = 0; i < mapDrawableBatch.Vertices.Length; i += 4)
            {
                var position = mapDrawableBatch.Vertices[i].Position + offset;

                var nodeToRemove = toReturn.TiledNodeAtWorld(position.X, position.Y);

                if (nodeToRemove != null)
                {
                    toReturn.Remove(nodeToRemove);
                }
            }

            return(toReturn);
        }
Ejemplo n.º 11
0
        public PositionedNode AddAndLinkTiledNode(int x, int y, DirectionalType directionalType)
        {
            PositionedNode node = null;

            if (mTiledNodes[x][y] != null)
            {            
                node = mTiledNodes[x][y];
            }
            else
            {
                node = AddNode();
                mTiledNodes[x][y] = node;

            }

            node.Position.X = mXSeed + x * mGridSpacing;
            node.Position.Y = mYSeed + y * mGridSpacing;

            // Now attach to the adjacent tiles
            AttachNodeToNodeAtIndex(node, x, y + 1);
            AttachNodeToNodeAtIndex(node, x + 1, y);
            AttachNodeToNodeAtIndex(node, x, y - 1);
            AttachNodeToNodeAtIndex(node, x - 1, y);
            if (directionalType == DirectionalType.Eight)
            {
                AttachNodeToNodeAtIndex(node, x - 1, y + 1);
                AttachNodeToNodeAtIndex(node, x + 1, y + 1);
                AttachNodeToNodeAtIndex(node, x + 1, y - 1);
                AttachNodeToNodeAtIndex(node, x - 1, y - 1);
            }

            return node;
        }