public List <ICollider> GetAdjecentUnits(Vector2 position)
 {
     return(mUnitMap.GetAdjacentUnits(position));
 }
        public void Update(GameTime gametime)
        {
            mUnitMap?.Update(gametime);

            #region Check targets for friendly units

            foreach (var unit in mFriendlyMilitary)
            {
                // iterate through each friendly unit, if there's a target nearby, shoot the closest one.
                // get all the adjacent units
                var adjacentUnits = mUnitMap?.GetAdjacentUnits(unit.AbsolutePosition);

                // remember the closest adjacent unit.
                ICollider closestAdjacent         = null;
                var       closestAdjacentDistance =
                    1500f; // a separate variable is used so that it can be initalized with a very big value.

                if (adjacentUnits == null)
                {
                    continue;
                }
                // iterate through all adjacent units to find the closest adjacent unit.
                foreach (var adjacentUnit in adjacentUnits)
                {
                    // only calculate the distance to the adjacent unit if the unit is not friendly.
                    if (!adjacentUnit.Friendly)
                    {
                        // calculate the distance
                        var dist = Geometry.GetQuickDistance(unit.AbsolutePosition, adjacentUnit.AbsolutePosition);
                        // check if it's within range
                        if (dist < unit.Range)
                        {
                            // if yes, check if it's closer than the previous closest
                            if (dist < closestAdjacentDistance)
                            {
                                closestAdjacentDistance = dist;
                                closestAdjacent         = adjacentUnit;
                            }
                        }
                    }
                }

                // if there is something close enough, shoot it. Else, set the target to null.
                if (closestAdjacent != null)
                {
                    unit.SetShootingTarget(closestAdjacent);
                }
                else
                {
                    unit.SetShootingTarget(null);
                }
            }

            #endregion

            #region Check targets for friendly turrets

            foreach (var turret in mFriendlyDefensePlatforms)
            {
                // iterate through each friendly turret, if there's a target nearby, shoot it.
                // get all the adjacent units
                var adjacentUnits = mUnitMap?.GetAdjacentUnits(turret.AbsolutePosition);

                // remember the closest adjacent unit.
                ICollider closestAdjacent         = null;
                var       closestAdjacentDistance =
                    1500f; // a separate variable is used so that it can be initalized with a very big value.

                if (adjacentUnits == null)
                {
                    continue;
                }
                // iterate through all adjacent units to find the closest adjacent unit.
                foreach (var adjacentUnit in adjacentUnits)
                {
                    // only calculate the distance to the adjacent unit if the unit is not friendly.
                    if (!adjacentUnit.Friendly)
                    {
                        // calculate the distance
                        var dist = Geometry.GetQuickDistance(turret.AbsolutePosition, adjacentUnit.AbsolutePosition);
                        // check if it's within range
                        if (dist < turret.Range)
                        {
                            // if yes, check if it's closer than the previous closest
                            if (dist < closestAdjacentDistance)
                            {
                                closestAdjacentDistance = dist;
                                closestAdjacent         = adjacentUnit;
                            }
                        }
                    }
                }

                // if there is something close enough, shoot it. Else, set the target to null.
                if (closestAdjacent != null)
                {
                    turret.SetShootingTarget(closestAdjacent);
                }
                else
                {
                    turret.SetShootingTarget(null);
                }
            }

            #endregion

            #region Check targets for hostile units

            foreach (var unit in mHostileMilitary)
            {
                // iterate through each hostile unit, if there's a target nearby, shoot it.
                // get all the adjacent units
                var adjacentUnits = mUnitMap?.GetAdjacentUnits(unit.AbsolutePosition);

                // remember the closest adjacent unit.
                ICollider closestAdjacent         = null;
                var       closestAdjacentDistance =
                    1500f; // a separate variable is used so that it can be initalized with a very big value.

                if (adjacentUnits == null)
                {
                    continue;
                }
                // iterate through all adjacent units to find the closest adjacent unit.
                foreach (var adjacentUnit in adjacentUnits)
                {
                    // only calculate the distance to the adjacent unit if the unit is friendly.
                    if (adjacentUnit.Friendly)
                    {
                        // calculate the distance
                        var dist = Geometry.GetQuickDistance(unit.AbsolutePosition, adjacentUnit.AbsolutePosition);
                        // check if it's within range
                        if (dist < unit.Range)
                        {
                            // if yes, check if it's closer than the previous closest
                            if (dist < closestAdjacentDistance)
                            {
                                closestAdjacentDistance = dist;
                                closestAdjacent         = adjacentUnit;
                            }
                        }
                    }
                }

                // if there is something close enough, shoot it. Else, set the target to null.
                if (closestAdjacent != null)
                {
                    var platform = closestAdjacent as PlatformBlank;
                    if (platform != null && !platform.GetBluePrintStatus())
                    {
                        unit.SetShootingTarget(closestAdjacent);
                    }
                    else if (platform == null)
                    {
                        unit.SetShootingTarget(closestAdjacent);
                    }
                }
                else
                {
                    unit.SetShootingTarget(null);
                }
            }

            #endregion

            #region Check targets for hostile turrets

            foreach (var turret in mHostileDefensePlatforms)
            {
                // iterate through each friendly turret, if there's a target nearby, shoot it.
                // get all the adjacent units
                var adjacentUnits = mUnitMap?.GetAdjacentUnits(turret.AbsolutePosition);

                // remember the closest adjacent unit.
                ICollider closestAdjacent         = null;
                var       closestAdjacentDistance =
                    1500f; // a separate variable is used so that it can be initalized with a very big value.

                if (adjacentUnits == null)
                {
                    continue;
                }
                // iterate through all adjacent units to find the closest adjacent unit.
                foreach (var adjacentUnit in adjacentUnits)
                {
                    // only calculate the distance to the adjacent unit if the unit is friendly.
                    if (adjacentUnit.Friendly)
                    {
                        // calculate the distance
                        var dist = Geometry.GetQuickDistance(turret.AbsolutePosition, adjacentUnit.AbsolutePosition);
                        // check if it's within range
                        if (dist < turret.Range)
                        {
                            // if yes, check if it's closer than the previous closest
                            if (dist < closestAdjacentDistance)
                            {
                                closestAdjacentDistance = dist;
                                closestAdjacent         = adjacentUnit;
                            }
                        }
                    }
                }

                // if there is something close enough, shoot it. Else, set the target to null.
                if (closestAdjacent != null)
                {
                    turret.SetShootingTarget(closestAdjacent);
                }
                else
                {
                    turret.SetShootingTarget(null);
                }
            }

            #endregion

            #region Flocking adding

            if (mSelected.Count > 0)
            {
                mIsSelected = true;
                mSelectedGroup.Reset();
                mSelected.ForEach(u => mSelectedGroup.AssignUnit(u));
                mGroups.Add(mSelectedGroup);
            }
            else if (mIsSelected)
            {
                mIsSelected    = false;
                mSelectedGroup = new FlockingGroup(ref mDirector, ref mMap);
            }
            mSelected = new List <IFlocking>();

            mGroups.RemoveAll(g => g.Die());

            mGroups.ForEach(g => g.Update(gametime));

            #endregion
        }