Beispiel #1
0
 /// <summary>
 /// Unregisters the portal component with this manager.
 /// </summary>
 /// <param name="portal">The portal component.</param>
 public void UnregisterPortalComponent(GridPortalComponent portal)
 {
     _portalComponents.Remove(portal);
 }
Beispiel #2
0
        /// <summary>
        /// Connects the specified grid to its neighbour grid(s) using Connector Portals. Will only connect to initialized and enabled grids.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <param name="pos">The position to connect with neighbours. Valid options are Top, Bottom, Left and Right</param>
        /// <returns>A list of portal components that represent the connections. If none are found the list will be empty.</returns>
        /// <exception cref="System.ArgumentException">
        /// Can only connect a grid component that has been initialized.
        /// or
        /// Only direct neighbours, i.e. Left, Right, Top or Bottom are supported.
        /// </exception>
        public static IList <GridPortalComponent> Connect(this GridComponent grid, NeighbourPosition pos)
        {
            if (grid.grid == null)
            {
                throw new ArgumentException("Can only connect a grid component that has been initialized.");
            }

            NeighbourPosition neighboursPos;
            Vector3           translation;
            Bounds            explorerBounds;

            //First we establish an origin based bounds on the appropriate side of the grid to check for neighbour grids
            switch (pos)
            {
            case NeighbourPosition.Bottom:
            {
                neighboursPos  = NeighbourPosition.Top;
                translation    = new Vector3(0f, 0f, grid.cellSize);
                explorerBounds = GetEdge(grid.grid, NeighbourPosition.Bottom).Translate(-translation);
                break;
            }

            case NeighbourPosition.Top:
            {
                neighboursPos  = NeighbourPosition.Bottom;
                translation    = new Vector3(0f, 0f, -grid.cellSize);
                explorerBounds = GetEdge(grid.grid, NeighbourPosition.Top).Translate(-translation);
                break;
            }

            case NeighbourPosition.Left:
            {
                neighboursPos  = NeighbourPosition.Right;
                translation    = new Vector3(grid.cellSize, 0f, 0f);
                explorerBounds = GetEdge(grid.grid, NeighbourPosition.Left).Translate(-translation);
                break;
            }

            case NeighbourPosition.Right:
            {
                neighboursPos  = NeighbourPosition.Left;
                translation    = new Vector3(-grid.cellSize, 0f, 0f);
                explorerBounds = GetEdge(grid.grid, NeighbourPosition.Right).Translate(-translation);
                break;
            }

            default:
            {
                throw new ArgumentException("Only direct neighbours, i.e. Left, Right, Top or Bottom are supported.");
            }
            }

            //Resize so the bounds are contained in the grid
            explorerBounds = explorerBounds.DeltaSize(-0.05f, 0f, -0.05f);

            var existingConnectors = GridManager.instance.GetAssociatedPortals(grid).Where(p => p.type == PortalType.Connector).ToArray();

            var portals    = new List <GridPortalComponent>();
            var neighbours = GridManager.instance.GetGrids(explorerBounds);

            foreach (var n in neighbours)
            {
                //Make sure a connection is not already in place
                bool connect = true;
                for (int i = 0; i < existingConnectors.Length; i++)
                {
                    if (existingConnectors[i].Connects(grid.grid, n))
                    {
                        connect = false;
                        break;
                    }
                }

                if (connect)
                {
                    //Get the edge on the neighbour grid and find the intersection between it and this grid
                    var p1 = GetEdge(n, neighboursPos).DeltaSize(-0.05f, 0f, -0.05f);
                    p1 = p1.Intersection(explorerBounds);
                    var p2 = p1.Translate(translation);

                    //TODO: revise this once the PortalActionNoneComponent is gone or made a default for connectors
                    var portal = GridPortalComponent.Create <PortalActionNoneComponent>(grid.gameObject, PortalType.Connector, p1, p2);
                    portals.Add(portal);
                }
            }

            return(portals);
        }
Beispiel #3
0
 /// <summary>
 /// Registers the portal component with this manager.
 /// </summary>
 /// <param name="portal">The portal component.</param>
 public void RegisterPortalComponent(GridPortalComponent portal)
 {
     _portalComponents.Add(portal);
 }