コード例 #1
0
        /// <summary>
        /// Gets the neighbour grid component in the given direction, if one exists.
        /// The predefined directions on the <see cref="DirectionVector"/> can be combined for more combinations.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <param name="direction">The direction.</param>
        /// <returns>The neighbouring grid, or null if no neighbour exists</returns>
        public static GridComponent GetNeighbourGrid(this GridComponent grid, DirectionVector direction)
        {
            var bounds = grid.bounds;
            var pos    = bounds.center + ((bounds.extents + Vector3.one) * direction);

            return(GridManager.instance.GetGridComponent(pos));
        }
コード例 #2
0
        internal bool Connects(GridComponent gridOne, GridComponent gridTwo)
        {
            var b1 = gridOne.bounds;
            var b2 = gridTwo.bounds;

            return((b1.Contains(this.actualPortalOne.center) || b1.Contains(this.actualPortalTwo.center)) &&
                   (b2.Contains(this.actualPortalOne.center) || b2.Contains(this.actualPortalTwo.center)));
        }
コード例 #3
0
        /// <summary>
        /// Gets the portal components that connects a grid. This includes both portal connecting the grid to other grids and portals forming internal connections within the grid.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <returns>The portals</returns>
        public IEnumerable <GridPortalComponent> GetAssociatedPortals(GridComponent grid)
        {
            var count = _portalComponents.count;

            for (int i = 0; i < count; i++)
            {
                var p = _portalComponents[i];
                if (p.Connects(grid))
                {
                    yield return(_portalComponents[i]);
                }
            }
        }
コード例 #4
0
ファイル: GridVisualizer.cs プロジェクト: andrewstarnes/wwtd2
        private bool DrawGrid(GridComponent gridComp, Bounds drawArea, bool outlineOnly)
        {
            if (gridComp.sizeX == 0 || gridComp.sizeZ == 0 || gridComp.cellSize == 0f)
            {
                return true;
            }

            Gizmos.color = this.boundsColor;
            Gizmos.DrawWireCube(gridComp.bounds.center, gridComp.bounds.size);

            IGrid grid = gridComp.grid;
            if (outlineOnly || grid == null)
            {
                return true;
            }

            var start = grid.GetCell(drawArea.min);
            var end = grid.GetCell(drawArea.max);
            if (start == null || end == null)
            {
                return false;
            }

            var lineColor = this.gridLinesColor;
            if (!gridComp.automaticInitialization && !Application.isPlaying)
            {
                lineColor.a = lineColor.a * 0.5f;
            }

            if (this.drawMode == GridMode.Layout)
            {
                DrawLayout(grid, start, end, lineColor);
            }
            else if (this.drawMode == GridMode.HeightOverlay)
            {
                grid.cellMatrix.RenderHeightOverlay(this.gridLinesColor);
            }
            else
            {
                DrawAccessibility(grid, start, end, lineColor);
            }

            if (this.drawSubSections)
            {
                var y = grid.origin.y + 0.05f;

                Gizmos.color = this.subSectionsColor;
                foreach (var section in grid.gridSections)
                {
                    var subCenter = section.bounds.center;
                    subCenter.y = y;
                    Gizmos.DrawWireCube(subCenter, section.bounds.size);
                }
            }

            return true;
        }
コード例 #5
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);
        }
コード例 #6
0
        private static void BakeGrid(GridComponent g)
        {
            var builder = g.GetBuilder();

            var matrix = CellMatrix.Create(builder);

            var data = g.bakedData;
            if (data == null)
            {
                data = CellMatrixData.Create(matrix);

                g.bakedData = data;
            }
            else
            {
                data.Refresh(matrix);
            }

            if (g.storeBakedDataAsAsset)
            {
                EditorUtilities.CreateOrUpdateAsset(data, g.friendlyName.Trim());
            }
            else
            {
                EditorUtility.SetDirty(data);
            }

            g.ResetGrid();
            EditorUtility.SetDirty(g);

            Debug.Log(string.Format("The grid {0} was successfully baked.", g.friendlyName));
        }
コード例 #7
0
 /// <summary>
 /// Unregisters the grid component with this manager.
 /// </summary>
 /// <param name="grid">The grid component.</param>
 public void UnregisterGridComponent(GridComponent grid)
 {
     _gridComponents.Remove(grid);
 }
コード例 #8
0
 /// <summary>
 /// Registers the grid component with this manager.
 /// </summary>
 /// <param name="grid">The grid component.</param>
 public void RegisterGridComponent(GridComponent grid)
 {
     _gridComponents.AddUnique(grid);
 }
コード例 #9
0
        internal bool Connects(GridComponent grid)
        {
            var b = grid.bounds;

            return(b.Contains(this.actualPortalOne.center) || b.Contains(this.actualPortalTwo.center));
        }
コード例 #10
0
 /// <summary>
 /// Gets the portal components that connects a grid. This includes both portal connecting the grid to other grids and portals forming internal connections within the grid.
 /// </summary>
 /// <param name="grid">The grid.</param>
 /// <returns>The portals</returns>
 public IEnumerable<GridPortalComponent> GetAssociatedPortals(GridComponent grid)
 {
     var count = _portalComponents.count;
     for (int i = 0; i < count; i++)
     {
         var p = _portalComponents[i];
         if (p.Connects(grid))
         {
             yield return _portalComponents[i];
         }
     }
 }
コード例 #11
0
 /// <summary>
 /// Unregisters the grid component with this manager.
 /// </summary>
 /// <param name="grid">The grid component.</param>
 public void UnregisterGridComponent(GridComponent grid)
 {
     _gridComponents.Remove(grid);
 }
コード例 #12
0
 /// <summary>
 /// Registers the grid component with this manager.
 /// </summary>
 /// <param name="grid">The grid component.</param>
 public void RegisterGridComponent(GridComponent grid)
 {
     _gridComponents.AddUnique(grid);
 }