Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FullGridVectorField"/> class.
        /// </summary>
        /// <param name="group">The transient unit group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The vector field options.</param>
        public FullGridVectorField(TransientGroup <IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;

            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _obstacleStrengthFactor = options.obstacleStrengthFactor;
            _allowCornerCutting     = pathOptions.allowCornerCutting;
            _allowDiagonals         = !pathOptions.preventDiagonalMoves;
            _announceAllNodes       = modelUnit.pathNavigationOptions.announceAllNodes;

            _builtInContainment = options.builtInContainment;
            _updateInterval     = options.updateInterval;

            // pre-allocate lists
            _openSet = new SimpleQueue <Cell>(31);
            _tempWalkableNeighbours      = new DynamicArray <Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray <Cell>(8);

            _grid = GridManager.instance.GetGrid(group.modelUnit.position);
            if (_grid != null)
            {
                _fastMarchedCells = new PlaneVector[_grid.sizeX, _grid.sizeZ];
                _cellDirs         = new VectorFieldCell[_grid.sizeX, _grid.sizeZ];
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CrossGridVectorField"/> class.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The options.</param>
        public CrossGridVectorField(TransientGroup <IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;

            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _allowCornerCutting = pathOptions.allowCornerCutting;
            _allowDiagonals     = !pathOptions.preventDiagonalMoves;
            _announceAllNodes   = modelUnit.pathNavigationOptions.announceAllNodes;

            _obstacleStrengthFactor     = options.obstacleStrengthFactor;
            _builtInContainment         = options.builtInContainment;
            _updateInterval             = options.updateInterval;
            _paddingIncrease            = options.paddingIncrease;
            _maxExtraPadding            = options.maxExtraPadding;
            _boundsPadding              = options.boundsPadding;
            _boundsRecalculateThreshold = options.boundsRecalculateThreshold;
            _expectedGroupGrowthFactor  = 1f + options.expectedGroupGrowthFactor;

            // pre-allocate lists
            _openSet = new SimpleQueue <Cell>(31);
            _tempWalkableNeighbours      = new DynamicArray <Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray <Cell>(8);
            _groupBounds = new RectangleXZ(group.centerOfGravity, _boundsPadding, _boundsPadding);

            _grids = new List <IGrid>(4);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FullGridVectorField"/> class.
        /// </summary>
        /// <param name="group">The transient unit group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The vector field options.</param>
        public FullGridVectorField(TransientGroup<IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;
            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _obstacleStrengthFactor = options.obstacleStrengthFactor;
            _allowCornerCutting = pathOptions.allowCornerCutting;
            _allowDiagonals = !pathOptions.preventDiagonalMoves;
            _announceAllNodes = modelUnit.pathNavigationOptions.announceAllNodes;

            _builtInContainment = options.builtInContainment;
            _updateInterval = options.updateInterval;

            // pre-allocate lists
            _openSet = new SimpleQueue<Cell>(31);
            _tempWalkableNeighbours = new DynamicArray<Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray<Cell>(8);

            _grid = GridManager.instance.GetGrid(group.modelUnit.position);
            if (_grid != null)
            {
                _fastMarchedCells = new PlaneVector[_grid.sizeX, _grid.sizeZ];
                _cellDirs = new VectorFieldCell[_grid.sizeX, _grid.sizeZ];
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProgressiveVectorField"/> class.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The options.</param>
        public ProgressiveVectorField(TransientGroup<IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;
            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _allowCornerCutting = pathOptions.allowCornerCutting;
            _allowDiagonals = !pathOptions.preventDiagonalMoves;
            _announceAllNodes = modelUnit.pathNavigationOptions.announceAllNodes;

            _obstacleStrengthFactor = options.obstacleStrengthFactor;
            _builtInContainment = options.builtInContainment;
            _updateInterval = options.updateInterval;
            _paddingIncrease = options.paddingIncrease;
            _maxExtraPadding = options.maxExtraPadding;
            _boundsPadding = options.boundsPadding;
            _boundsRecalculateThreshold = options.boundsRecalculateThreshold;
            _expectedGroupGrowthFactor = 1f + options.expectedGroupGrowthFactor;

            // pre-allocate lists memory
            _openSet = new SimpleQueue<Cell>(31);
            _tempWalkableNeighbours = new DynamicArray<Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray<Cell>(8);
            _groupBounds = new RectangleXZ(group.centerOfGravity, _boundsPadding, _boundsPadding);
        }
Exemplo n.º 5
0
        private void Start()
        {
            //Create the AI group. In this case we just explicitly create one group
            //but the group strategy could also be used to create a grouping
            _aiGroup = new DefaultTransientUnitGroup(4);
            var aiStartPos = new Vector3(-20f, 0f, 0f);

            for (int i = 0; i < 4; i++)
            {
                var unitGO = Instantiate(this.unitMold, aiStartPos, Quaternion.identity) as GameObject;
                unitGO.SetActive(true);
                _aiGroup.Add(unitGO.GetUnitFacade());
            }
        }
        /// <summary>
        /// Creates a vector field using the VectorFieldOptions exposed on this component.
        /// </summary>
        /// <param name="group">The transient unit group.</param>
        /// <param name="path">The path.</param>
        /// <returns>A new vector field, or null on error</returns>
        public IVectorField CreateVectorField(TransientGroup <IUnitFacade> group, Path path)
        {
            IVectorField newField = null;

            switch (vectorFieldOptions.vectorFieldType)
            {
            case VectorFieldType.FullGridField:
            {
                newField = new FullGridVectorField(group, path, vectorFieldOptions);
                break;
            }

            case VectorFieldType.ProgressiveField:
            {
                newField = new ProgressiveVectorField(group, path, vectorFieldOptions);
                break;
            }

            case VectorFieldType.CrossGridField:
            {
                newField = new CrossGridVectorField(group, path, vectorFieldOptions);
                break;
            }

            default:
            case VectorFieldType.FunnelField:
            {
                newField = new FunnelVectorField(group, path, vectorFieldOptions);
                break;
            }
            }

            if (newField == null)
            {
                return(null);
            }

            newField.Initialize();

            return(newField);
        }
        /// <summary>
        /// Creates a vector field using the VectorFieldOptions exposed on this component.
        /// </summary>
        /// <param name="group">The transient unit group.</param>
        /// <param name="path">The path.</param>
        /// <returns>A new vector field, or null on error</returns>
        public IVectorField CreateVectorField(TransientGroup<IUnitFacade> group, Path path)
        {
            IVectorField newField = null;
            switch (vectorFieldOptions.vectorFieldType)
            {
                case VectorFieldType.FullGridField:
                {
                    newField = new FullGridVectorField(group, path, vectorFieldOptions);
                    break;
                }

                case VectorFieldType.ProgressiveField:
                {
                    newField = new ProgressiveVectorField(group, path, vectorFieldOptions);
                    break;
                }

                case VectorFieldType.CrossGridField:
                {
                    newField = new CrossGridVectorField(group, path, vectorFieldOptions);
                    break;
                }

                default:
                case VectorFieldType.FunnelField:
                {
                    newField = new FunnelVectorField(group, path, vectorFieldOptions);
                    break;
                }
            }

            if (newField == null)
            {
                return null;
            }

            newField.Initialize();

            return newField;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FunnelVectorField"/> class.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The options.</param>
        public FunnelVectorField(TransientGroup <IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;

            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _funnelWidth            = options.funnelWidth;
            _obstacleStrengthFactor = options.obstacleStrengthFactor;
            _allowCornerCutting     = pathOptions.allowCornerCutting;
            _allowDiagonals         = !pathOptions.preventDiagonalMoves;
            _announceAllNodes       = modelUnit.pathNavigationOptions.announceAllNodes;

            _builtInContainment = options.builtInContainment;
            _updateInterval     = options.updateInterval;

            // pre-allocate lists memory
            _openSet = new SimpleQueue <Cell>(31);
            _tempWalkableNeighbours      = new DynamicArray <Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray <Cell>(8);

            _grid = GridManager.instance.GetGrid(group.modelUnit.position);
            if (_grid != null)
            {
                // we allocate half of the grid's size in order to have a bit more allocated memory than we expect to actually use
                int size = Mathf.CeilToInt((_grid.sizeX * _grid.sizeZ) / 2f);

                float minF = 9.99999944E-11f;
                _cellDirsSet         = new Dictionary <Vector3, VectorFieldCell>(size, new Vector3EqualityComparer(minF));
                _fastMarchedCellsSet = new Dictionary <Vector3, PlaneVector>(size, new Vector3EqualityComparer(minF));
            }

            _funnelWidthSqr = _funnelWidth * _funnelWidth;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FunnelVectorField"/> class.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="path">The path.</param>
        /// <param name="options">The options.</param>
        public FunnelVectorField(TransientGroup<IUnitFacade> group, Path path, VectorFieldOptions options)
        {
            Ensure.ArgumentNotNull(group, "group");
            this.group = group;

            _currentPath = path;

            var modelUnit = group.modelUnit;
            _unitProperties = modelUnit;
            var pathOptions = modelUnit.pathFinderOptions;

            // cache options locally
            _funnelWidth = options.funnelWidth;
            _obstacleStrengthFactor = options.obstacleStrengthFactor;
            _allowCornerCutting = pathOptions.allowCornerCutting;
            _allowDiagonals = !pathOptions.preventDiagonalMoves;
            _announceAllNodes = modelUnit.pathNavigationOptions.announceAllNodes;

            _builtInContainment = options.builtInContainment;
            _updateInterval = options.updateInterval;

            // pre-allocate lists memory
            _openSet = new SimpleQueue<Cell>(31);
            _tempWalkableNeighbours = new DynamicArray<Cell>(8);
            _extraTempWalkableNeighbours = new DynamicArray<Cell>(8);

            _grid = GridManager.instance.GetGrid(group.modelUnit.position);
            if (_grid != null)
            {
                // we allocate half of the grid's size in order to have a bit more allocated memory than we expect to actually use
                int size = Mathf.CeilToInt((_grid.sizeX * _grid.sizeZ) / 2f);

                float minF = 9.99999944E-11f;
                _cellDirsSet = new Dictionary<Vector3, VectorFieldCell>(size, new Vector3EqualityComparer(minF));
                _fastMarchedCellsSet = new Dictionary<Vector3, PlaneVector>(size, new Vector3EqualityComparer(minF));
            }

            _funnelWidthSqr = _funnelWidth * _funnelWidth;
        }