コード例 #1
0
        void Init()
        {
            var cathetus = Mathf.Sqrt((_searchRadius * _searchRadius) / 2);

            _searchBounds = new Vector3(cathetus, 0, cathetus);
            _unit         = GetComponent <RBTUnit>();
        }
コード例 #2
0
        void FixedUpdate()
        {
            if (_unit.CommandGroupId != "" || !_unit.Alive)
            {
                return;
            }

            var     nearbyUnits = RBTUnitBehaviourManager.UnitGrid.FindNear(_unit.Position, _searchBounds);
            var     sqrRadius   = _searchRadius * _searchRadius;
            RBTUnit closestUnit = null;
            float   closestDist = 0;

            foreach (var unit in nearbyUnits)
            {
                var dist = (unit.Position - _unit.Position).sqrMagnitude;
                if (dist < sqrRadius && unit.Team != _unit.Team && unit.Alive)
                {
                    if (closestUnit == null || dist < closestDist)
                    {
                        closestDist = dist;
                        closestUnit = unit;
                    }
                    break;
                }
            }
            if (closestUnit)
            {
                RBTUnitBehaviourManager.Instance.CommandAttack(new List <RBTUnit>()
                {
                    _unit
                }, closestUnit);
            }
        }
コード例 #3
0
        public void Add(RBTUnit unit)
        {
            if (_unitToCells.ContainsKey(unit))
            {
                return;
            }
            else
            {
                _unitToCells.Add(unit, new List <CellNode>());
            }

            var cellIndices = CalcOccupiedCells(unit);

            foreach (var cellIndex in cellIndices)
            {
                var newHead = new CellNode()
                {
                    unit = unit, cellIndex = cellIndex
                };
                if (_indexToCell.ContainsKey(cellIndex))
                {
                    var prevHead = _indexToCell[cellIndex];
                    prevHead.previous = newHead;
                    newHead.next      = prevHead;
                }

                _indexToCell[cellIndex] = newHead;
                _unitToCells[unit].Add(newHead);
            }
        }
コード例 #4
0
        public void Remove(RBTUnit unit)
        {
            var cellNodes = _unitToCells[unit];

            foreach (var node in cellNodes)
            {
                var previous = node.previous;
                var next     = node.next;

                if (previous == null && next == null)
                {
                    _indexToCell.Remove(node.cellIndex);
                }
                else
                {
                    if (previous != null)
                    {
                        previous.next = next;
                    }
                    if (next != null)
                    {
                        next.previous = previous;
                    }
                }
            }

            _unitToCells.Remove(unit);
        }
コード例 #5
0
        public HashSet <RBTUnit> FindNear(RBTUnit unit)
        {
            var grid  = RBTUnitBehaviourManager.UnitGrid;
            var scale = GetLargestDimension(unit.Bounds.Extents);
            var scaledSearchBounds = _baseSearchBounds * scale;

            return(grid.FindNear(unit.Position, scaledSearchBounds));
        }
コード例 #6
0
        public bool WithinFov(RBTUnit source, RBTUnit target)
        {
            var dirSourceToTarget = (target.Position - source.Position).normalized;
            var sourceVelocityDir = source.Velocity.normalized;

            var dotProd = Vector3.Dot(dirSourceToTarget, sourceVelocityDir);

            return(dotProd > _minFovDotProd);
        }
コード例 #7
0
        public List <RBTUnit> GetUnitsWithinFov(RBTUnit unit)
        {
            var nearby          = FindNear(unit);
            var nearbyWithinFov = new List <RBTUnit>();

            nearbyWithinFov.Capacity = nearbyWithinFov.Count;
            foreach (var nu in nearby)
            {
                if (WithinFov(unit, nu))
                {
                    nearbyWithinFov.Add(nu);
                }
            }
            return(nearbyWithinFov);
        }
コード例 #8
0
        // Public
        public float GetMaxDistance(RBTUnit unit)
        {
            var scale = GetLargestDimension(unit.Bounds.Extents);

            return(Reach * scale);
        }
コード例 #9
0
 public Vector3Int[] GetOccupiedCells(RBTUnit unit)
 {
     return(_unitToCells[unit].Select(node => node.cellIndex).ToArray());
 }
コード例 #10
0
        List <Vector3Int> CalcOccupiedCells(RBTUnit unit)
        {
            var boundsCorners = unit.Bounds.Corners;
            // var cellIndices = new HashSet<Vector3Int>();
            var occupiedCells = new OccupiedCells();

            for (int i = 0; i < 4; i++)
            {
                var startCorner = boundsCorners[i % 4];
                var endCorner   = boundsCorners[(i + 1) % 4];

                var startCell = GetCellIndex(startCorner);
                var endCell   = GetCellIndex(endCorner);

                occupiedCells.Add(startCell);
                if (startCell == endCell)
                {
                    continue;
                }
                occupiedCells.Add(endCell);

                var rayDir = (endCorner - startCorner).normalized;

                var step_dzdx = Mathf.Abs(rayDir.z / (rayDir.x + 0.0001f));
                var step_dxdz = Mathf.Abs(rayDir.x / (rayDir.z + 0.0001f));

                var rayStepX = Mathf.Sqrt(1 + Mathf.Pow(step_dzdx, 2));
                var rayStepZ = Mathf.Sqrt(1 + Mathf.Pow(step_dxdz, 2));

                int   stepX, stepZ;
                float initialAdjustmentX, initialAdjustmentZ;

                if (rayDir.x < 0)
                {
                    stepX = -1;
                    initialAdjustmentX = startCorner.x - startCell.x;
                }
                else
                {
                    stepX = 1;
                    initialAdjustmentX = (startCell.x + 1) - startCorner.x;
                }

                if (rayDir.z < 0)
                {
                    stepZ = -1;
                    initialAdjustmentZ = startCorner.z - startCell.z;
                }
                else
                {
                    stepZ = 1;
                    initialAdjustmentZ = (startCell.z + 1) - startCorner.z;
                }

                step_dxdz *= stepX;
                step_dzdx *= stepZ;

                var rayLengthX = initialAdjustmentX * rayStepX;
                var rayLengthZ = initialAdjustmentZ * rayStepZ;

                float zAlongX = startCorner.z + step_dzdx * initialAdjustmentX;
                float xAlongZ = startCorner.x + step_dxdz * initialAdjustmentZ;

                var maxRayLength = (endCorner - startCorner).magnitude;

                var currentCellX = GetCellIndex(new Vector3(startCorner.x + stepX * initialAdjustmentX, startCorner.y, zAlongX));
                if (rayLengthX < maxRayLength)
                {
                    occupiedCells.Add(currentCellX);
                }

                var currentCellZ = GetCellIndex(new Vector3(xAlongZ, startCorner.y, startCorner.z + stepZ * initialAdjustmentZ));
                if (rayLengthZ < maxRayLength)
                {
                    occupiedCells.Add(currentCellZ);
                }

                bool endCellReached = false;
                while (!endCellReached)
                {
                    bool commonAdjacentCell = currentCellZ.z - currentCellX.z == 1 && currentCellX.x - currentCellZ.x == 1;
                    if (commonAdjacentCell)
                    {
                        occupiedCells.Add(new Vector3Int(currentCellZ.x, startCell.y, currentCellX.z));
                    }

                    if (rayLengthX < rayLengthZ)
                    {
                        rayLengthX += rayStepX;
                        if (rayLengthX < maxRayLength)
                        {
                            zAlongX        += step_dzdx;
                            currentCellX.x += stepX;
                            currentCellX.z  = Mathf.FloorToInt(zAlongX);
                            occupiedCells.Add(currentCellX);
                        }
                    }
                    else
                    {
                        rayLengthZ += rayStepZ;
                        if (rayLengthZ < maxRayLength)
                        {
                            xAlongZ        += step_dxdz;
                            currentCellZ.z += stepZ;
                            currentCellZ.x  = Mathf.FloorToInt(xAlongZ);
                            occupiedCells.Add(currentCellZ);
                        }
                    }

                    endCellReached = rayLengthX >= maxRayLength && rayLengthZ >= maxRayLength;
                }
            }

            foreach (var entry in occupiedCells.rowEdgeValues)
            {
                var valuePair = entry.Value;
                var smallestX = valuePair.Item1;
                var biggestX  = valuePair.Item2;
                var z         = entry.Key;
                var y         = Mathf.FloorToInt(boundsCorners[0].y);
                for (int x = smallestX + 1; x < biggestX; x++)
                {
                    occupiedCells.Add(new Vector3Int(x, y, z));
                }
            }

            int topCount = occupiedCells.Count;
            var allCells = new List <Vector3Int>();

            allCells.Capacity = topCount;
            foreach (var row in occupiedCells.rowToCells.Values)
            {
                foreach (var cell in row)
                {
                    allCells.Add(cell);
                }
            }
            var diffY = GetCellIndex(boundsCorners[0]).y - GetCellIndex(boundsCorners[4]).y;

            for (int y = 1; y <= diffY; y++)
            {
                for (int i = 0; i < topCount; i++)
                {
                    allCells.Add(new Vector3Int(allCells[i].x, allCells[i].y - y, allCells[i].z));
                }
            }

            return(allCells);
        }
コード例 #11
0
 public void Update(RBTUnit unit)
 {
     Remove(unit);
     Add(unit);
 }
コード例 #12
0
 public UnitEvent(RBTUnit sender)
 {
     Sender = sender;
 }
コード例 #13
0
 public OnStateChangedEvent(RBTUnit sender, UnitState prevState, UnitState newState)
     : base(sender)
 {
     PrevState = prevState;
     NewState  = newState;
 }
コード例 #14
0
 public OnDeActivatedEvent(RBTUnit sender)
     : base(sender)
 {
 }
コード例 #15
0
 public OnDeselectedEvent(RBTUnit sender)
     : base(sender)
 {
 }