public bool FindPositionOutOfSight(ConflictLocation conflict, Team team, out Vector2 result)
        {
            Cell thisCell = team == Team.Allies ? conflict.alliesCell : conflict.enemiesCell;
            Cell thatCell = team == Team.Allies ? conflict.enemiesCell : conflict.alliesCell;

            return(FindPositionOutOfSight(thisCell, thatCell, team, out result));
        }
        private void MaintainConflict(ConflictLocation conflictLocation)
        {
            var center = conflictLocation.center;

            if ((center - MainCharacter.current.position).CompareLength(maxDistanceFromMainCharacter) > 0)
            {
                return;
            }

            var unitSet = unitsManager.GetUnitSetAt(center);

            if (unitSet == null)
            {
#if UNITY_EDITOR
                Debug.DrawRay(center, Vector3.up * 4, Color.red, 999);
                Debug.DrawRay(center, Vector3.down * 4, Color.red, 999);
                Debug.DrawRay(center, Vector3.right * 4, Color.red, 999);
                Debug.DrawRay(center, Vector3.left * 4, Color.red, 999);
#endif
                throw new System.Exception($"No UnitSet is found at point {center}");
            }
            float radius        = roadManager.GetCellsInterval() * 3;
            var   unitsToSpawn  = unitSet.GetUnitsToSpawnAroundConflict(conflictLocation, radius);
            var   unitInstances = unitsPool.SpawnUnits(unitsToSpawn, conflictLocation);
            foreach (var unitInstance in unitInstances)
            {
                SetupUnitAI(unitInstance, conflictLocation);
            }
        }
        private bool FindPositionForUnit(GameObject go, ConflictLocation conflict, out Vector2 position)
        {
            Team team;

            if (!go.IsAllyOrEnemyObject(out team))
            {
                throw new System.Exception($"Unit {go.name}'s layer is neither the Allies nor the Enemies");
            }
            if (!battleMaintainer.FindPositionOutOfSight(conflict, team, out position))
            {
                //Debug.LogWarning($"Couldn't find a position out of sight for unit {go.name} around conflict {{{conflict}}}");
                return(false);
            }
            return(true);
        }
        private void SetupUnitAI(GameObject unitInstance, ConflictLocation conflictLocation)
        {
            var ai = unitInstance.GetComponent <Unit>()?.ai;

            if (ai != null)
            {
                Vector2 moveTo = conflictLocation.center + Random.insideUnitCircle * 15f;
                ai.navAgent.SamplePosition(moveTo, 9f, out moveTo);
                ai.state = new AI.MoveToPositionState(ai, moveTo);

                if (unitInstance.IsAlly())
                {
                    ai.followPath          = conflictLocation.enemiesCell.GetPath();
                    ai.secondaryFollowPath = roadManager.GetMainPath();
                }
            }
        }
Beispiel #5
0
        public IEnumerable <GameObject> GetUnitsToSpawnAroundConflict(ConflictLocation conflict, float radius)
        {
            var alliesThere  = Unit.GetInRadius <Unit>(conflict.center, radius, 1 << Unit.alliesLayer);
            var enemiesThere = Unit.GetInRadius <Unit>(conflict.center, radius, 1 << Unit.enemiesLayer);

            int addAllies  = maintainAlliesNumber - alliesThere.Count;
            int addEnemies = maintainEnemiesNumber - enemiesThere.Count;

            for (int i = 0; i < addAllies; i++)
            {
                yield return(_alliesPrefabs.GetRandomItem());
            }
            for (int i = 0; i < addEnemies; i++)
            {
                yield return(_enemiesPrefabs.GetRandomItem());
            }
        }
        public IEnumerable <GameObject> SpawnUnits(IEnumerable <GameObject> prefabs, ConflictLocation conflict)
        {
            Vector2 originalPos = conflict.center;

            foreach (var prefab in prefabs)
            {
                Vector2 position;
                if (!FindPositionForUnit(prefab, conflict, out position))
                {
                    continue;
                }

                GameObjectPool goPool   = GameObjectPool.Get(prefab);
                GameObject     instance = goPool.Take();

                SetupUnit(instance, position, goPool, originalPos);

                yield return(instance);
            }
        }