Exemple #1
0
    // Function for other squads to call for help in combat. The squad this function is called upon, either helps in the fight or flees from the opponent
    public void HelpAgainst(SquadCluster opponentCluster, float ratio)
    {
        try {
            Squad opponent = FindNewOpponent(opponentCluster.Select(item => item.GetComponent <Squad>()).ToArray());

            HelpAgainst(opponent, ratio);
        } catch (Exception e) {
            Debug.LogError(e);
        }
    }
Exemple #2
0
    IEnumerator UpdateEnemySquadClusters()
    {
        for (int x = 0; x < enemyContainer.transform.childCount; x++)
        {
            GameObject squadObj1 = enemyContainer.transform.GetChild(x).gameObject;

            if (squadObj1.gameObject.activeSelf)
            {
                Squad squad1 = squadObj1.GetComponent <Squad>();

                if (squad1 != null)
                {
                    squad1.cluster = null;
                }
            }
        }
        // Scrap existing clusters

        counter++;
        if (makeScreenshots)
        {
            ScreenCapture.CaptureScreenshot("screenshot #" + counter + ".png", 2);
        }
        enemySquadClusters = new System.Collections.Generic.List <SquadCluster>();

        // Check all enemy squads

        for (int x = 0; x < enemyContainer.transform.childCount; x++)
        {
            GameObject squadObj1 = enemyContainer.transform.GetChild(x).gameObject;

            if (!squadObj1.gameObject.activeSelf)
            {
                continue;
            }

            Squad squad1 = squadObj1.GetComponent <Squad>();

            if (squad1.cluster != null)
            {
                continue;
            }

            // Get all squadClusters which are near enough

            var   nearSquads      = new System.Collections.Generic.List <Squad>();
            var   nearClusters    = new System.Collections.Generic.List <SquadCluster>();
            float minimumDistance = squadObj1.GetComponent <Squad>().movementRadius + allyDistance;

            // Check
            for (int y = 0; y < enemyContainer.transform.childCount; y++)
            {
                GameObject squadObj2 = enemyContainer.transform.GetChild(y).gameObject;

                if (squadObj1 == squadObj2 || !squadObj2.gameObject.activeSelf)
                {
                    continue;
                }

                Squad squad2 = squadObj2.GetComponent <Squad>();

                // Test if near enough

                float distance = (squad2.GetPosition() - squad1.GetPosition()).magnitude - squad2.movementRadius;

                if (distance < minimumDistance)
                {
                    nearSquads.Add(squad2);

                    if (squad2.cluster != null)
                    {
                        nearClusters.Add(squad2.cluster);
                    }
                }
            }

            if (nearSquads.Count == 0)
            {
                continue;
            }


            if (nearClusters.Count == 0)
            {
                // Create new cluster

                SquadCluster cluster = new SquadCluster(nearSquads, 1);
                cluster.Add(squad1);

                enemySquadClusters.Add(cluster);
            }
            else if (nearClusters.Count == 1)
            {
                // Add to cluster

                nearClusters[0].AddRange(nearSquads);
                nearClusters[0].Add(squad1);
            }
            else
            {
                // Merge existing entityClusters and add object

                MergeSquadClusters(nearClusters);
                nearClusters[0].AddRange(nearSquads);
                nearClusters[0].Add(squad1);
            }
        }

        // Debug
        foreach (SquadCluster cluster in enemySquadClusters)
        {
            foreach (Transform squad1 in cluster)
            {
                foreach (Transform squad2 in cluster)
                {
                    Debug.DrawLine(squad1.GetComponent <Squad>().GetPosition(), squad2.GetComponent <Squad>().position, Color.gray, SquadClusterUpdatesEverySeconds);
                }
            }
        }
        counter2 = enemySquadClusters.Count;

        // recurse

        yield return(new WaitForSeconds(SquadClusterUpdatesEverySeconds));

        StartCoroutine(UpdateEnemySquadClusters());
    }
Exemple #3
0
    IEnumerator UpdateFriendlySquadClusters()
    {
        if (!isPlayerDead && player.GetComponent <Squad>().cluster != null)
        {
            player.GetComponent <Squad>().cluster = null;
        }

        for (int x = 0; x < friendContainer.transform.childCount; x++)
        {
            GameObject squadObj1 = friendContainer.transform.GetChild(x).gameObject;

            if (squadObj1.gameObject.activeSelf)
            {
                Squad squad1 = squadObj1.GetComponent <Squad>();

                if (squad1 != null)
                {
                    squad1.cluster = null;
                }
            }
        }
        // Scrap existing clusters

        if (friendSquadClusters != null)
        {
            foreach (SquadCluster cluster in friendSquadClusters)
            {
                for (var i = 0; i < cluster.Count; i++)
                {                 // potential loop
                    try {
                        Transform squad = cluster[i];

                        if (squad == null)
                        {
                            //cluster.RemoveSquad(squad);
                            //i--;
                            continue;
                        }

                        squad.GetComponent <Squad>().cluster = null;
                    } catch (Exception e) {
                        Debug.LogError(e);
                    }
                }
            }
        }

        friendSquadClusters = new System.Collections.Generic.List <SquadCluster>();

        // Check all friendly squads (and player)

        for (int x = -1; x < friendContainer.transform.childCount; x++)
        {
            if (isPlayerDead && x == -1)
            {
                continue;
            }

            GameObject squadObj1;

            if (x == -1)             // also add player as a friend
            {
                squadObj1 = GameMaster.player.gameObject;
            }
            else
            {
                squadObj1 = friendContainer.transform.GetChild(x).gameObject;
            }

            if (squadObj1 == null || !squadObj1.gameObject.activeSelf)
            {
                continue;
            }

            Squad squad1 = squadObj1.GetComponent <Squad>();

            if (squad1 == null || squad1.cluster != null)
            {
                continue;
            }

            // Get all squadClusters which are near enough

            var   nearSquads      = new System.Collections.Generic.List <Squad>();
            var   nearClusters    = new System.Collections.Generic.List <SquadCluster>();
            float minimumDistance = squadObj1.GetComponent <Squad>().movementRadius + allyDistance;

            // Check
            for (int y = 0; y < friendContainer.transform.childCount; y++)
            {
                GameObject squadObj2 = friendContainer.transform.GetChild(y).gameObject;

                if (squadObj1 == squadObj2 || !squadObj2.gameObject.activeSelf)
                {
                    continue;
                }

                Squad squad2 = squadObj2.GetComponent <Squad>();

                // Test if near enough

                float distance = (squad2.GetPosition() - squad1.GetPosition()).magnitude - squad2.movementRadius;

                if (distance < minimumDistance)
                {
                    nearSquads.Add(squad2);

                    if (squad2.cluster != null)
                    {
                        nearClusters.Add(squad2.cluster);
                    }
                }
            }

            if (nearSquads.Count == 0)
            {
                continue;
            }


            if (nearClusters.Count == 0)
            {
                // Create new cluster

                SquadCluster cluster = new SquadCluster(nearSquads, 0);
                cluster.Add(squad1);

                friendSquadClusters.Add(cluster);
            }
            else if (nearClusters.Count == 1)
            {
                // Add to cluster

                nearClusters[0].AddRange(nearSquads);
                nearClusters[0].Add(squad1);
            }
            else
            {
                // Merge existing entityClusters and add object

                MergeSquadClusters(nearClusters);
                nearClusters[0].AddRange(nearSquads);
                nearClusters[0].Add(squad1);
            }
        }

        // Debug
        foreach (SquadCluster cluster in friendSquadClusters)
        {
            foreach (Transform squad1 in cluster)
            {
                foreach (Transform squad2 in cluster)
                {
                    Debug.DrawLine(squad1.GetComponent <Squad>().GetPosition(), squad2.GetComponent <Squad>().position, Color.gray, SquadClusterUpdatesEverySeconds);
                }
            }
        }
        counter1 = friendSquadClusters.Count;

        // recurse

        yield return(new WaitForSeconds(SquadClusterUpdatesEverySeconds));

        StartCoroutine(UpdateFriendlySquadClusters());
    }