コード例 #1
0
    // Checks if there are any nearby entityClusters, which the GameObject can be pooled with
    public static bool ReconsiderEntityCluster(Transform t)
    {
        if (t == null)
        {
            return(false);
        }

        // Remove from old cluster

        EntityCluster oldCluster = t.GetComponent <Entity>().cluster;

        if (oldCluster != null)
        {
            if (oldCluster.Count == 1)
            {
                entityClusters.Remove(oldCluster);
            }
            oldCluster.RemoveEntity(t);
        }

        // Get all nearby entityClusters

        var   nearClusters    = new System.Collections.Generic.List <EntityCluster>();
        float minimumDistance = t.GetComponent <Entity>().obstacleSize + minPassDist;

        for (var i = 0; i < entityClusters.Count; i++)
        { // potential loop
            EntityCluster cluster = entityClusters[i];

            if (cluster == null)
            {
                entityClusters.Remove(cluster);
                i--;
                continue;
            }
            // Test if near enough

            if (cluster.GetClosestPassingDistance(t.transform.position) < minimumDistance)
            {
                nearClusters.Add(cluster);

                // Add cluster connections

                foreach (Transform companion in cluster)
                {
                    float distance = (companion.position - t.position).magnitude -
                                     companion.GetComponent <Entity>().obstacleSize;
                    if (distance < minimumDistance)
                    {
                        if (t.GetComponent <Moving>() != null &&
                            !t.GetComponent <Moving>().clusteredEntities.Contains(companion))
                        {
                            t.GetComponent <Moving>().clusteredEntities.Add(companion);
                        }
                        if (companion.GetComponent <Moving>() != null &&
                            !companion.GetComponent <Moving>().clusteredEntities.Contains(t))
                        {
                            companion.GetComponent <Moving>().clusteredEntities.Add(t);
                        }
                    }
                }
            }
        }

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

            System.Collections.Generic.List <Transform> clusterList = new System.Collections.Generic.List <Transform> {
                t
            };
            EntityCluster cluster = new EntityCluster(clusterList);
            entityClusters.Add(cluster);

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

            nearClusters[0].Add(t);

            if (oldCluster != null)
            {
                return(oldCluster == nearClusters[0]);
            }
            else
            {
                return(true);
            }
        }
        else
        {
            // Merge existing entityClusters and add object

            MergeEntityClusters(nearClusters);
            nearClusters[0].Add(t);

            return(true);
        }
    }
コード例 #2
0
    public void UpdateNearEntities()
    {
        float         minPassDist    = GameMaster.minPassDist;
        float         minClusterDist = minPassDist + GetComponent <Entity>().obstacleSize;
        EntityCluster thisCluster    = GetComponent <Entity>().cluster;

        foreach (GameObject g in GameMaster.entities)
        {
            if (g == null)
            {
                GameMaster.entities.Remove(g);
                break;
            }

            if (g.GetComponent <Entity>() == null)
            {
                GameMaster.entities.Remove(g);
                break;
            }

            Transform t = g.transform;

            if (t == transform)
            {
                continue;
            }

            float dist = Vector2.Distance(t.transform.position, transform.position);
            if (nearEntities.Contains(t.transform))
            {
                if (dist > 7)
                {
                    nearEntities.Remove(t.transform);

                    // Check if their cluster data should also be deleted
                    // Check if it was the last entity from it's cluster

                    EntityCluster cluster = t.GetComponent <Entity>().cluster;

                    if (cluster == null)
                    {
                        Debug.LogError("cluster fragment");

                        GameMaster.ReconsiderEntityCluster(transform);
                        cluster = t.GetComponent <Entity>().cluster;
                    }
                    bool last = true;
                    foreach (Transform nearEntity in nearEntities)
                    {
                        if (cluster.Contains(nearEntity))
                        {
                            last = false;
                            break;
                        }
                    }

                    if (last)
                    {
                        clusterSides.Remove(cluster);
                    }

                    // Remove from cluster (this code should never be executed under normal circumstances)

                    if (clusteredEntities.Contains(t))
                    {
                        // Remove clustered entities from former cluster

                        foreach (Transform clean in clusteredEntities)
                        {
                            try {
                                thisCluster.RemoveEntity(clean);
                            } catch (Exception ex) {
                                Debug.LogError(ex);
                            }
                        }
                        thisCluster.RemoveEntity(transform);

                        // Reconsider cluster for nearby entities

                        foreach (Transform companion in clusteredEntities)
                        {
                            GameMaster.ReconsiderEntityCluster(companion);
                        }

                        // Remove from clustered entities
                        clusteredEntities.Remove(t);
                        if (t.GetComponent <Moving>() != null)
                        {
                            t.GetComponent <Moving>().clusteredEntities.Remove(transform);
                        }

                        // Reconsider own cluster

                        GameMaster.ReconsiderEntityCluster(transform);
                    }
                }                 // check if should be clustered
                else if (!clusteredEntities.Contains(t) && dist < (minClusterDist + t.GetComponent <Entity>().obstacleSize - 0.15f))
                {
                    clusteredEntities.Add(t);
                    if (t.GetComponent <Moving>() != null)
                    {
                        t.GetComponent <Moving>().clusteredEntities.Add(transform);
                    }

                    GameMaster.ReconsiderEntityCluster(transform);
                    GameMaster.ReconsiderEntityCluster(t);
                }
            }
            else if (dist < 7)
            {
                nearEntities.Add(t.transform);

                // Check if should be moved to same cluster

                if (dist < (minClusterDist + t.GetComponent <Entity>().obstacleSize - 0.15f))
                {
                    GameMaster.ReconsiderEntityCluster(transform);
                }
            }
        }

        // Sort list

        nearEntities.Sort(new PositionComparer(transform));

        // Check if clustered entities are still in cluster range

        List <Transform> toBeRemoved = new List <Transform>();

        for (var i = 0; i < clusteredEntities.Count; i++)
        {
            Transform entity = clusteredEntities[i];
            if (entity.GetComponent <Entity>() == null)            // means it is dead
            {
                clusteredEntities.Remove(entity);
                continue;
            }

            float dist = (entity.position - transform.position).magnitude;

            if (dist > (minClusterDist + entity.GetComponent <Entity>().obstacleSize + 0.15f))
            {
                // should be removed

                toBeRemoved.Add(entity);
            }
        }

        if (toBeRemoved.Count > 0)
        {
            // Remove clustered from former cluster
            Transform[] companions = thisCluster.Where(transform => nearEntities.Contains(transform)).ToArray();

            foreach (Transform clean in companions)
            {
                try
                {
                    thisCluster.RemoveEntity(clean);
                }
                catch (Exception ex)
                {
                    Debug.LogError(ex);
                }
            }
            thisCluster.RemoveEntity(transform);

            // Reconsider cluster for nearby entities

            foreach (Transform companion in companions)
            {
                GameMaster.ReconsiderEntityCluster(companion);
            }

            // Remove too distant entities

            foreach (var t in toBeRemoved)
            {
                clusteredEntities.Remove(t);
                float distance = (t.position - transform.position).magnitude;
                if (t.GetComponent <Moving>() != null)
                {
                    t.GetComponent <Moving>().clusteredEntities.Remove(transform);
                }
            }

            // Reconsider own cluster

            GameMaster.ReconsiderEntityCluster(transform);
        }

        if (toBeRemoved.Count != 0)
        {
            GameMaster.ReconsiderEntityCluster(transform);
        }

        // Debug

        if (ShowDebugInfo)
        {
            // Mode
            Vector3 pos = transform.position;
            pos += new Vector3(.5f, -.5f, 0);

            // Destination
            if (mode == 1)
            {
                DrawSquare(targetPlace, Color.green, (1 / updateRate));
            }
            else if (mode == 2)
            {
                DrawSquare(targetPlace, new Color(1, 0, 1), (1 / updateRate));
            }
        }
    }
コード例 #3
0
    private void Die(DeathType type)
    {
        // Move entity

        // DEBUG ONLY

        GameObject parent = GameObject.Find("wrecks") ?? new GameObject("wrecks");

        transform.parent = parent.transform;

        // Remove entity

        cluster?.RemoveEntity(transform);

        if (transform != GameMaster.player && team == 0)
        {
            GameMaster.friends.Remove(gameObject);
        }
        else if (transform != GameMaster.player)
        {
            GameMaster.enemies.Remove(gameObject);
        }

        GameMaster.entities.Remove(gameObject);

        if (hasSquad)
        {
            squad.RemoveMember(transform);
        }

        if (squad != null && squad.members.Count == 0)
        {
            // Remove squad

            if (!isPlayer)
            {
                Destroy(squad.gameObject);
            }

            squad.cluster?.RemoveSquad(squad.transform);

            DebugLevel.squads.Remove(squad);
        }

        if (explosionMode == ExplosionMode.BURN && type == DeathType.DESTRUCTION)
        {
            // Play death animation

            if (explosionEffect != null)
            {
                Instantiate(explosionEffect, transform.position, transform.rotation);
            }

            // Change sprite

            body.GetComponent <SpriteRenderer>().sprite = wrecked;

            // Remove GameObjects

            foreach (Object obj in removeOnDeath)
            {
                Destroy(obj);
            }

            // Change layer

            gameObject.layer = 10;
        }
        else if (type == DeathType.DROWNING)
        {
        }

        if (transform.name == "Player" && !GameMaster.isPlayerDead)
        {
            // player death

            GameMaster.PlayerDeath(type);
        }

        // Remove itself

        Destroy(transform.GetComponent <Entity>());

        // Update Map

        Bounds bounds = GetComponent <Collider2D>().bounds;

        bounds.Expand(2f);

        GameObject.Find("A*").GetComponent <UpdatePathfindingMap>().UpdateSection(bounds);

        // Test if won

        if (GameMaster.enemies.Count == 0)
        {
            GameMaster.instance.Win();
        }
    }