private void DeprocessDestroyable(List <DestroyableGameObject> destroyable, List <DestroyableGameObject> processing, bool isMisc)
    {
        for (int i = processing.Count - 1; i >= 0; --i)
        {
            DestroyableGameObject toProcess = processing[i];

            if (toProcess.fragmenterStat != null && toProcess.fragmenterStat.isDone)
            {
                if (toProcess.state == DestroyableGameObject.StateType.fragmenting)
                {
                    Fragmenter.Stats fs = new Fragmenter.Stats();
                    toProcess.gameObject     = toProcess.fragmenterStat.fragmentedGameObject;
                    toProcess.fragmenterStat = fs;
                    toProcess.state          = DestroyableGameObject.StateType.exploding;

                    Debug.Log($"Exploding {toProcess.gameObject.name}");

                    StartCoroutine(Fragmenter.Explode(
                                       CalculateExplosionPoint(toProcess.gameObject, true),
                                       isMisc ? Constants.MiscExplosionRadius : Constants.BuildExplosionRadius,
                                       isMisc ? Constants.MiscExplosionForce  : Constants.BuildExplosionForce,
                                       fs,
                                       isMisc)
                                   );
                }
                else if (toProcess.state == DestroyableGameObject.StateType.exploding)
                {
                    processing.RemoveAt(i);

                    if (toProcess.fragmenterStat.destroyedAll)
                    {
                        toProcess.state = DestroyableGameObject.StateType.destroyed;
                    }
                    else
                    {
                        toProcess.state = DestroyableGameObject.StateType.fragmented;
                        destroyable.Add(toProcess);
                    }
                }
            }
        }
    }
    private void ProcessDestroyable(List <DestroyableGameObject> destroyable, List <DestroyableGameObject> processing, bool isMisc)
    {
        if (Random.Range(0.0f, 1.0f) < 0.25f && destroyable.Count > 0)
        {
            int index = Random.Range(0, destroyable.Count - 1);
            DestroyableGameObject toDestroy = destroyable[index];

            if (toDestroy.state == DestroyableGameObject.StateType.decaying)
            {
                Debug.Log($"Fragmenting {toDestroy.gameObject.name}");
                Fragmenter.Stats fs = new Fragmenter.Stats();
                toDestroy.fragmenterStat = fs;
                toDestroy.state          = DestroyableGameObject.StateType.fragmenting;

                destroyable.RemoveAt(index);
                processing.Add(toDestroy);

                StartCoroutine(Fragmenter.Fragment(toDestroy.gameObject, fs, isMisc));
            }
            else if (toDestroy.state == DestroyableGameObject.StateType.fragmented)
            {
                Debug.Log($"Exploding {toDestroy.gameObject.name}");
                Fragmenter.Stats fs = new Fragmenter.Stats();
                toDestroy.fragmenterStat = fs;
                toDestroy.state          = DestroyableGameObject.StateType.exploding;

                destroyable.RemoveAt(index);
                processing.Add(toDestroy);

                StartCoroutine(Fragmenter.Explode(
                                   CalculateExplosionPoint(toDestroy.gameObject),
                                   isMisc ? Constants.MiscExplosionRadius : Constants.BuildExplosionRadius,
                                   isMisc ? Constants.MiscExplosionForce  : Constants.BuildExplosionForce,
                                   fs)
                               );
            }
            else
            {
                Debug.Log("Oops");
            }
        }
    }
Example #3
0
    void Update()
    {
        // Check if user clicked on a mesh with active collider
        if (Input.GetMouseButtonDown(0) &&
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity) &&
            !runningFragmentations.ContainsKey(hit) && !runningExplosions.ContainsKey(hit))
        {
            if (!Fragmenter.IsFragmented(hit.transform.gameObject))
            {
                Fragmenter.Stats fs = new Fragmenter.Stats();
                runningFragmentations.Add(hit, fs);
                Debug.Log($"Fragmentation Start Time: {Time.timeSinceLevelLoad}");
                StartCoroutine(Fragmenter.Fragment(hit.transform.gameObject, fs));
            }
            else
            {
                Fragmenter.Stats fs = new Fragmenter.Stats();
                runningExplosions.Add(hit, fs);
                StartCoroutine(Fragmenter.Explode(hit.point, radius, force, fs));
            }
        }

        // When fragmentation finishes, create an explosion where user clicked
        List <RaycastHit> toRemove = new List <RaycastHit>();

        foreach (KeyValuePair <RaycastHit, Fragmenter.Stats> entry in runningFragmentations)
        {
            if (entry.Value.isDone)
            {
                Debug.Log($"Fragmentation Total Time: {entry.Value.totalTime / 1000.0f}");
                toRemove.Add(entry.Key);
            }
        }
        foreach (RaycastHit rhit in toRemove)
        {
            Fragmenter.Stats fs = new Fragmenter.Stats();
            runningFragmentations.Remove(rhit);
            runningExplosions.Add(rhit, fs);
            StartCoroutine(Fragmenter.Explode(rhit.point, radius, force, fs));
        }


        toRemove.Clear();
        foreach (KeyValuePair <RaycastHit, Fragmenter.Stats> entry in runningExplosions)
        {
            if (entry.Value.isDone)
            {
                toRemove.Add(entry.Key);
            }
        }
        foreach (RaycastHit rhit in toRemove)
        {
            runningExplosions.Remove(rhit);
        }

        /*
         * for (int i = runningFragmentations.Count - 1; i >= 0; --i)
         * {
         *  (Fragmenter.Stats stats, RaycastHit rhit) = runningFragmentations[i];
         *
         *  if (stats.isDone)
         *  {
         *      StartCoroutine(Fragmenter.Explode(rhit.point, radius, force, null));
         *      runningFragmentations.RemoveAt(i);
         *  }
         * }
         */
    }
 public DestroyableGameObject(GameObject gameObject)
 {
     this.gameObject = gameObject;
     state           = StateType.decaying;
     fragmenterStat  = null;
 }