Пример #1
0
        // Try to connect grid_cell with this cell. Connection can be established only when any two cells of both grid cells are connected. This function should behave
        // properly when called multiple times for the same pair of grid cells!
        public void AddNeighbour(GridCell grid_cell)
        {
            if (GlobalId == grid_cell.GlobalId || !AABB.Overlaps2D(grid_cell.AABB, true))
            {
                return;
            }

            // this is removed to properly handle merging
            //if (Neighbours.Exists(x => x.cell.Equals(grid_cell)))
            //    return;

            bool         any_cells_connected = false;
            MovementFlag connection_flags    = MovementFlag.None;

            foreach (Cell our_cell in Cells)
            {
                foreach (Cell other_cell in grid_cell.Cells)
                {
                    Vec3 border_point    = default(Vec3);
                    bool cells_connected = our_cell.AddNeighbour(other_cell, ref border_point);

                    if (cells_connected)
                    {
                        any_cells_connected = true;
                        MovementFlag flags = our_cell.Flags & other_cell.Flags;

                        if (flags > connection_flags)
                        {
                            connection_flags = flags;
                        }
                    }
                }
            }

            if (any_cells_connected)
            {
                Neighbour n1 = Neighbours.FirstOrDefault(x => x.cell.GlobalId == grid_cell.GlobalId);

                // if they were not connected before, simply connect them
                if (n1 == null)
                {
                    Neighbours.Add(new Neighbour(grid_cell, Vec3.ZERO, connection_flags));
                    grid_cell.Neighbours.Add(new Neighbour(this, Vec3.ZERO, connection_flags));
                }
                // otherwise verify connection flags
                else if (n1.connection_flags < connection_flags)
                {
                    Neighbour n2 = grid_cell.Neighbours.FirstOrDefault(x => x.cell.GlobalId == GlobalId);

                    n1.connection_flags = connection_flags;
                    n2.connection_flags = connection_flags;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the previous level in a linear level sequence.
        /// Only valid for LinearHorizontal and LinearVertical layouts.
        /// Returns null if this is the first level.
        /// </summary>
        public Level GetPreviousNeighbour(WorldLayout worldLayout)
        {
            switch (worldLayout)
            {
            case WorldLayout.LinearHorizontal:
                return(Neighbours.FirstOrDefault(level => level.IsWest));

            case WorldLayout.LinearVertical:
                return(Neighbours.FirstOrDefault(level => level.IsNorth));
            }
            Debug.LogWarning($"LDtk: Tried getting previous neighbour with an invalid world layout: {worldLayout}");
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Retrieve node
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public Node <T> GetNode(T element)
        {
            var node = Neighbours.FirstOrDefault(x => x.NodeContent.Equals(element));

            return(node);
        }