Exemplo n.º 1
0
        /// <summary>
        /// deactivate this fragment piece
        /// </summary>
        public void Deactivate()
        {
            if (activePlayback == audioSource)
            {
                activePlayback = null;
            }

            Sleep();
            ExploderUtils.SetActiveRecursively(gameObject, false);
            Visible  = false;
            IsActive = false;

            if (meshFilter && meshFilter.sharedMesh)
            {
                DestroyImmediate(meshFilter.sharedMesh, true);
            }

            // turn off particles
            if (particleSystems != null)
            {
                foreach (var psystem in particleSystems)
                {
                    psystem.Clear();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// create pool (array) of fragment game objects with all necessary components
        /// </summary>
        /// <param name="poolSize">number of fragments</param>
        /// <param name="useMeshColliders">use mesh colliders</param>
        /// <param name="use2dCollision">enable Unity 2D collision system</param>
        public void Allocate(int poolSize, bool useMeshColliders, bool use2dCollision, bool useGravity)
        {
            ExploderUtils.Assert(poolSize > 0, "");

            if (pool == null || pool.Length < poolSize || useMeshColliders != this.meshColliders)
            {
                DestroyFragments();

                pool = new Fragment[poolSize];

                this.meshColliders = useMeshColliders;

                for (int i = 0; i < poolSize; i++)
                {
                    var fragment = new GameObject("fragment_" + i);
                    fragment.AddComponent <MeshFilter>();
                    fragment.AddComponent <MeshRenderer>();

                    if (use2dCollision)
                    {
#if PHYSICS_2D
                        fragment.AddComponent <PolygonCollider2D>();
                        fragment.AddComponent <Rigidbody2D>();
#endif
                    }
                    else
                    {
                        if (useMeshColliders)
                        {
                            var meshCollider = fragment.AddComponent <MeshCollider>();
                            meshCollider.convex = true;
                        }
                        else
                        {
                            fragment.AddComponent <BoxCollider>();
                        }

                        Rigidbody rb = fragment.AddComponent <Rigidbody>();
                        rb.useGravity = useGravity;
                    }

                    fragment.AddComponent <ExploderOption>();

                    var fragmentComponent = fragment.AddComponent <Fragment>();

                    fragment.transform.parent = gameObject.transform;

                    pool[i] = fragmentComponent;

                    ExploderUtils.SetActiveRecursively(fragment.gameObject, false);

                    fragmentComponent.RefreshComponentsCache();

                    fragmentComponent.Sleep();
                }
            }
        }
Exemplo n.º 3
0
        public long Explode()
        {
            int count = this.pool.Count;
            int index = 0;

            if (count == 0)
            {
                return(0);
            }
            this.watch.Start();
            if (this.parameters.Callback != null)
            {
                this.parameters.Callback(0.0f, ExploderObject.ExplosionState.ExplosionStarted);
            }
            Vector3    vector3    = Vector3.get_zero();
            Quaternion quaternion = Quaternion.get_identity();

            if (Object.op_Implicit((Object)this.originalObject))
            {
                vector3    = Vector3.op_Subtraction(this.originalObject.get_transform().get_position(), this.initPos);
                quaternion = Quaternion.op_Multiply(this.originalObject.get_transform().get_rotation(), Quaternion.Inverse(this.initRot));
            }
            while (index < count)
            {
                Fragment fragment = this.pool[index];
                ++index;
                if (Object.op_Inequality((Object)this.originalObject, (Object)this.parameters.ExploderGameObject))
                {
                    ExploderUtils.SetActiveRecursively(this.originalObject, false);
                }
                else
                {
                    ExploderUtils.EnableCollider(this.originalObject, false);
                    ExploderUtils.SetVisible(this.originalObject, false);
                }
                Transform transform1 = ((Component)fragment).get_transform();
                transform1.set_position(Vector3.op_Addition(transform1.get_position(), vector3));
                Transform transform2 = ((Component)fragment).get_transform();
                transform2.set_rotation(Quaternion.op_Multiply(transform2.get_rotation(), quaternion));
                fragment.Explode(this.parameters);
            }
            if (this.parameters.DestroyOriginalObject && Object.op_Implicit((Object)this.originalObject) && !Object.op_Implicit((Object)this.originalObject.GetComponent <Fragment>()))
            {
                Object.Destroy((Object)this.originalObject);
            }
            if (this.parameters.ExplodeSelf && !this.parameters.DestroyOriginalObject)
            {
                ExploderUtils.SetActiveRecursively(this.parameters.ExploderGameObject, false);
            }
            if (this.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(this.parameters.ExploderGameObject, false);
            }
            this.watch.Stop();
            return(this.watch.ElapsedMilliseconds);
        }
Exemplo n.º 4
0
        /// <summary>
        /// this is called from exploder class to start the explosion
        /// </summary>
        public void Explode()
        {
            activeObj = true;
            ExploderUtils.SetActiveRecursively(gameObject, true);
            visibilityCheckTimer = 0.1f;
            visible         = true;
            deactivateTimer = deactivateTimeout;
            originalScale   = transform.localScale;

            if (explodable)
            {
                tag = ExploderObject.Tag;
            }
        }
Exemplo n.º 5
0
        public static void SetActiveRecursively(GameObject obj, bool status)
        {
            if (!Object.op_Implicit((Object)obj))
            {
                return;
            }
            int childCount = obj.get_transform().get_childCount();

            for (int index = 0; index < childCount; ++index)
            {
                ExploderUtils.SetActiveRecursively(((Component)obj.get_transform().GetChild(index)).get_gameObject(), status);
            }
            obj.SetActive(status);
        }
Exemplo n.º 6
0
 public void Explode(ExploderParams parameters)
 {
     this.settings = parameters;
     this.IsActive = true;
     ExploderUtils.SetActiveRecursively(((Component)this).get_gameObject(), true);
     this.visibilityCheckTimer = 0.1f;
     this.Visible         = true;
     this.Cracked         = false;
     this.collided        = false;
     this.deactivateTimer = this.settings.FragmentDeactivation.DeactivateTimeout;
     this.originalScale   = ((Component)this).get_transform().get_localScale();
     if (this.settings.FragmentOptions.ExplodeFragments)
     {
         ((Component)this).set_tag(ExploderObject.Tag);
     }
     this.Emit(true);
 }
Exemplo n.º 7
0
        /// <summary>
        /// this is called from exploder class to start the explosion
        /// </summary>
        public void Explode(ExploderParams parameters)
        {
            this.settings = parameters;

            IsActive = true;
            ExploderUtils.SetActiveRecursively(gameObject, true);
            visibilityCheckTimer = 0.1f;
            Visible         = true;
            Cracked         = false;
            collided        = false;
            deactivateTimer = settings.FragmentDeactivation.DeactivateTimeout;
            originalScale   = transform.localScale;

            if (settings.FragmentOptions.ExplodeFragments)
            {
                tag = ExploderObject.Tag;
            }

            Emit(true);
        }
Exemplo n.º 8
0
 public void Deactivate()
 {
     if (Object.op_Equality((Object)Fragment.activePlayback, (Object)this.audioSource))
     {
         Fragment.activePlayback = (AudioSource)null;
     }
     this.Sleep();
     ExploderUtils.SetActiveRecursively(((Component)this).get_gameObject(), false);
     this.Visible  = false;
     this.IsActive = false;
     if (Object.op_Implicit((Object)this.meshFilter) && Object.op_Implicit((Object)this.meshFilter.get_sharedMesh()))
     {
         Object.DestroyImmediate((Object)this.meshFilter.get_sharedMesh(), true);
     }
     if (this.particleSystems == null)
     {
         return;
     }
     foreach (ParticleSystem particleSystem in this.particleSystems)
     {
         particleSystem.Clear();
     }
 }
Exemplo n.º 9
0
        void Update()
        {
            if (activeObj)
            {
                if (deactivateOptions == DeactivateOptions.DeactivateTimeout)
                {
                    deactivateTimer -= Time.deltaTime;

                    if (deactivateTimer < 0.0f)
                    {
                        Sleep();
                        activeObj = false;
                        ExploderUtils.SetActiveRecursively(gameObject, false);

                        // return fragment to previous fadout state
                        switch (fadeoutOptions)
                        {
                        case FadeoutOptions.Fadeout:
                            break;
                        }
                    }
                    else
                    {
                        var t = deactivateTimer / deactivateTimeout;

                        switch (fadeoutOptions)
                        {
                        case FadeoutOptions.Fadeout:
                            if (meshRenderer.material && meshRenderer.material.HasProperty("_Color"))
                            {
                                var color = meshRenderer.material.color;
                                color.a = t;
                                meshRenderer.material.color = color;
                            }
                            break;

                        case FadeoutOptions.ScaleDown:
                            gameObject.transform.localScale = originalScale * t;
                            break;
                        }
                    }
                }

                visibilityCheckTimer -= Time.deltaTime;

                if (visibilityCheckTimer < 0.0f)
                {
                    var viewportPoint = UnityEngine.Camera.main.WorldToViewportPoint(transform.position);

                    if (viewportPoint.z < 0 || viewportPoint.x < 0 || viewportPoint.y < 0 ||
                        viewportPoint.x > 1 || viewportPoint.y > 1)
                    {
                        if (deactivateOptions == DeactivateOptions.DeactivateOutsideOfCamera)
                        {
                            Sleep();
                            activeObj = false;
                            ExploderUtils.SetActiveRecursively(gameObject, false);
                        }

                        visible = false;
                    }
                    else
                    {
                        visible = true;
                    }

                    visibilityCheckTimer = Random.Range(0.1f, 0.3f);

                    if (explodable)
                    {
                        var size = GetComponent <Collider>().bounds.size;

                        if (Mathf.Max(size.x, size.y, size.z) < minSizeToExplode)
                        {
                            tag = string.Empty;
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        public long Explode()
        {
            var count   = pool.Count;
            var poolIdx = 0;

            if (count == 0)
            {
                return(0);
            }

            watch.Start();

            if (parameters.Callback != null)
            {
                parameters.Callback(0, ExploderObject.ExplosionState.ExplosionStarted);
            }

            var diffPos = Vector3.zero;
            var diffRot = Quaternion.identity;

            if (originalObject)
            {
                diffPos = originalObject.transform.position - initPos;
                diffRot = originalObject.transform.rotation * Quaternion.Inverse(initRot);
            }

            while (poolIdx < count)
            {
                var fragment = pool[poolIdx];

                poolIdx++;

                if (originalObject != parameters.ExploderGameObject)
                {
                    ExploderUtils.SetActiveRecursively(originalObject, false);
                }
                else
                {
                    ExploderUtils.EnableCollider(originalObject, false);
                    ExploderUtils.SetVisible(originalObject, false);
                }

                fragment.transform.position += diffPos;
                fragment.transform.rotation *= diffRot;

                fragment.Explode();
            }

            if (parameters.DestroyOriginalObject)
            {
                if (originalObject && !originalObject.GetComponent <Fragment>())
                {
                    GameObject.Destroy(originalObject);
                }
            }

            if (parameters.ExplodeSelf)
            {
                if (!parameters.DestroyOriginalObject)
                {
                    ExploderUtils.SetActiveRecursively(parameters.ExploderGameObject, false);
                }
            }

            if (parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(parameters.ExploderGameObject, false);
            }

            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }
Exemplo n.º 11
0
        public override bool Run(float frameBudget)
        {
            var count = core.pool.Count;

            while (core.poolIdx < count)
            {
                var fragment = core.pool[core.poolIdx];
                var mesh     = core.postList[core.poolIdx];

                core.poolIdx++;

                if (!mesh.original)
                {
                    continue;
                }

                var unityMesh = mesh.mesh.ToUnityMesh();

                fragment.meshFilter.sharedMesh = unityMesh;

                // choose proper material

                if (mesh.option && mesh.option.FragmentMaterial)
                {
                    fragment.meshRenderer.sharedMaterial = mesh.option.FragmentMaterial;
                }
                else
                {
                    if (core.parameters.FragmentOptions.FragmentMaterial != null)
                    {
                        fragment.meshRenderer.sharedMaterial = core.parameters.FragmentOptions.FragmentMaterial;
                    }
                    else
                    {
                        fragment.meshRenderer.sharedMaterial = mesh.material;
                    }
                }

                unityMesh.RecalculateBounds();

                var oldParent = fragment.transform.parent;
                fragment.transform.parent     = mesh.parent;
                fragment.transform.position   = mesh.position;
                fragment.transform.rotation   = mesh.rotation;
                fragment.transform.localScale = mesh.localScale;
                fragment.transform.parent     = null;
                fragment.transform.parent     = oldParent;

                if (core.parameters.PartialExplosion)
                {
                }
                else
                {
                    if (mesh.original != core.parameters.ExploderGameObject)
                    {
                        ExploderUtils.SetActiveRecursively(mesh.original, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(mesh.original, false);
                        ExploderUtils.SetVisible(mesh.original, false);
                    }

                    if (mesh.skinnedOriginal && mesh.skinnedOriginal != core.parameters.ExploderGameObject)
                    {
                        ExploderUtils.SetActiveRecursively(mesh.skinnedOriginal, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(mesh.skinnedOriginal, false);
                        ExploderUtils.SetVisible(mesh.skinnedOriginal, false);
                    }

                    if (mesh.skinnedOriginal && mesh.bakeObject)
                    {
                        GameObject.DestroyObject(mesh.bakeObject, 1);
                    }
                }

                var plane = mesh.option && mesh.option.Plane2D;

                var use2d = core.parameters.Use2DCollision;

                if (!core.parameters.FragmentOptions.DisableColliders)
                {
                    if (core.parameters.MeshColliders && !use2d)
                    {
                        // dont use mesh colliders for 2d plane
                        if (!plane)
                        {
                            fragment.meshCollider.sharedMesh = unityMesh;
                        }
                    }
                    else
                    {
                        if (core.parameters.Use2DCollision)
                        {
                            MeshUtils.GeneratePolygonCollider(fragment.polygonCollider2D, unityMesh);
                        }
                        else
                        {
                            fragment.boxCollider.center = unityMesh.bounds.center;
                            fragment.boxCollider.size   = unityMesh.bounds.extents;
                        }
                    }
                }

                if (mesh.option)
                {
                    mesh.option.DuplicateSettings(fragment.options);
                }

                fragment.Explode();

                var force = core.parameters.Force;
                if (mesh.option && mesh.option.UseLocalForce)
                {
                    force = mesh.option.Force;
                }

                // apply force to rigid body
                fragment.ApplyExplosion(mesh.transform, mesh.mesh.centroid, core.parameters.Position, core.parameters.FragmentOptions, core.parameters.UseForceVector,
                                        core.parameters.ForceVector, force, mesh.original, core.parameters.TargetFragments);

#if SHOW_DEBUG_LINES
                UnityEngine.Debug.DrawLine(settings.Position, forceVector * settings.Force, Color.yellow, 3);
#endif

                if (Watch.ElapsedMilliseconds > frameBudget)
                {
                    return(false);
                }
            }

            if (core.parameters.DestroyOriginalObject)
            {
                foreach (var mesh in core.postList)
                {
                    if (mesh.original && !mesh.original.GetComponent <Fragment>())
                    {
                        Object.Destroy(mesh.original);
                    }

                    if (mesh.skinnedOriginal)
                    {
                        Object.Destroy(mesh.skinnedOriginal);
                    }
                }
            }

            if (core.parameters.ExplodeSelf)
            {
                if (!core.parameters.DestroyOriginalObject)
                {
                    ExploderUtils.SetActiveRecursively(core.parameters.ExploderGameObject, false);
                }
            }

            if (core.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(core.parameters.ExploderGameObject, false);
            }

#if DBG
            ExploderUtils.Log("Explosion finished! " + postList.Count + postList[0].original.transform.gameObject.name);
#endif
//            core.exploder.OnExplosionFinished(true);

            Watch.Stop();

            return(true);
        }
Exemplo n.º 12
0
        void Update()
        {
            if (activeObj)
            {
                //
                // clamp velocity
                //
                if (rigidBody)
                {
                    if (rigidBody.velocity.sqrMagnitude > maxVelocity * maxVelocity)
                    {
                        var vel = rigidBody.velocity.normalized;
                        rigidBody.velocity = vel * maxVelocity;
                    }
                }
                else if (rigid2D)
                {
                    if (rigid2D.velocity.sqrMagnitude > maxVelocity * maxVelocity)
                    {
                        var vel = rigid2D.velocity.normalized;
                        rigid2D.velocity = vel * maxVelocity;
                    }
                }

                if (deactivateOptions == DeactivateOptions.Timeout)
                {
                    deactivateTimer -= Time.deltaTime;

                    if (deactivateTimer < 0.0f)
                    {
                        Sleep();
                        activeObj = false;
                        ExploderUtils.SetActiveRecursively(gameObject, false);

                        // return fragment to previous fadout state
                        switch (fadeoutOptions)
                        {
                        case FadeoutOptions.FadeoutAlpha:
                            break;
                        }
                    }
                    else
                    {
                        var t = deactivateTimer / deactivateTimeout;

                        if (emmiters != null)
                        {
                            foreach (var emitter in emmiters)
                            {
                                for (int i = 0; i < emitter.particles.Length; i++)
                                {
                                    var c = emitter.particles[i].color;
                                    c.a = 1.0f - t;
                                    emitter.particles[i].color = c;
                                }
                            }
                        }

                        switch (fadeoutOptions)
                        {
                        case FadeoutOptions.FadeoutAlpha:
                            if (meshRenderer.material && meshRenderer.material.HasProperty("_Color"))
                            {
                                var color = meshRenderer.material.color;
                                color.a = t;
                                meshRenderer.material.color = color;
                            }
                            break;

                        case FadeoutOptions.ScaleDown:
                            gameObject.transform.localScale = originalScale * t;
                            break;
                        }
                    }
                }

                // emmiter timeout
                if (stopEmitOnTimeout)
                {
                    emmitersTimeout -= Time.deltaTime;

                    if (emmitersTimeout < 0.0f)
                    {
                        // turn off particles
                        if (particleChild != null)
                        {
                            var emiter = particleChild.GetComponentInChildren <ParticleEmitter>();

                            if (emiter)
                            {
                                emiter.emit = false;
                            }
                        }

                        stopEmitOnTimeout = false;
                    }
                }

                visibilityCheckTimer -= Time.deltaTime;

                if (visibilityCheckTimer < 0.0f && UnityEngine.Camera.main)
                {
                    var viewportPoint = UnityEngine.Camera.main.WorldToViewportPoint(transform.position);

                    if (viewportPoint.z < 0 || viewportPoint.x < 0 || viewportPoint.y < 0 ||
                        viewportPoint.x > 1 || viewportPoint.y > 1)
                    {
                        if (deactivateOptions == DeactivateOptions.OutsideOfCamera)
                        {
                            Sleep();
                            activeObj = false;
                            ExploderUtils.SetActiveRecursively(gameObject, false);
                        }

                        visible = false;
                    }
                    else
                    {
                        visible = true;
                    }

                    visibilityCheckTimer = Random.Range(0.1f, 0.3f);

                    if (explodable)
                    {
                        var size = GetComponent <Collider>().bounds.size;

                        if (Mathf.Max(size.x, size.y, size.z) < minSizeToExplode)
                        {
                            tag = string.Empty;
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// create pool (array) of fragment game objects with all necessary components
        /// </summary>
        /// <param name="poolSize">number of fragments</param>
        /// <param name="useMeshColliders">use mesh colliders</param>
        /// <param name="use2dCollision">enable Unity 2D collision system</param>
        public void Allocate(int poolSize, bool useMeshColliders, bool use2dCollision)
        {
            ExploderUtils.Assert(poolSize > 0, "");

            if (pool == null || pool.Length < poolSize || useMeshColliders != this.meshColliders)
            {
//                if (useMeshColliders != meshColliders)
//                {
//                    Debug.LogWarning("Re-allocating pool, mesh colliders");
//                }

                DestroyFragments();

                pool = new Fragment[poolSize];

                this.meshColliders = useMeshColliders;

                GameObject fragmentPrefab = null;

                var fragmentInScene = GameObject.FindObjectOfType <Fragment>();

                if (fragmentInScene)
                {
                    fragmentPrefab = fragmentInScene.gameObject;
                }
                else
                {
                    var fragmentResource = Resources.Load("ExploderFragment");

                    if (fragmentResource)
                    {
                        fragmentPrefab = Instantiate(fragmentResource) as GameObject;
                    }
                }

                for (int i = 0; i < poolSize; i++)
                {
                    GameObject fragment = null;

                    if (fragmentPrefab)
                    {
                        fragment      = Instantiate(fragmentPrefab) as GameObject;
                        fragment.name = "fragment_" + i;
                    }
                    else
                    {
                        fragment = new GameObject("fragment_" + i);
                    }

                    fragment.AddComponent <MeshFilter>();
                    fragment.AddComponent <MeshRenderer>();

                    if (use2dCollision)
                    {
                        fragment.AddComponent <PolygonCollider2D>();
                        fragment.AddComponent <Rigidbody2D>();
                    }
                    else
                    {
                        if (useMeshColliders)
                        {
                            var meshCollider = fragment.AddComponent <MeshCollider>();
                            meshCollider.convex = true;
                        }
                        else
                        {
                            fragment.AddComponent <BoxCollider>();
                        }

                        fragment.AddComponent <Rigidbody>();
                    }

                    fragment.AddComponent <ExploderOption>();

                    var fragmentComponent = fragment.GetComponent <Fragment>();

                    if (!fragmentComponent)
                    {
                        fragmentComponent = fragment.AddComponent <Fragment>();
                    }

                    fragment.transform.parent = gameObject.transform;

                    pool[i] = fragmentComponent;

                    ExploderUtils.SetActiveRecursively(fragment.gameObject, false);

                    fragmentComponent.RefreshComponentsCache();

                    fragmentComponent.Sleep();
                }
            }
        }
Exemplo n.º 14
0
        public override bool Run(float frameBudget)
        {
            if (crackedObject == null)
            {
                return(true);
            }

            var count = crackedObject.pool.Count;

            while (core.poolIdx < count)
            {
                var fragment = crackedObject.pool[core.poolIdx];
                var mesh     = core.postList[core.poolIdx];

                core.poolIdx++;

                if (!mesh.original)
                {
                    continue;
                }

                ExploderUtils.SetActiveRecursively(fragment.gameObject, false);

                var unityMesh = mesh.mesh.ToUnityMesh();

                fragment.meshFilter.sharedMesh = unityMesh;

                // choose proper material

                if (mesh.option && mesh.option.FragmentMaterial)
                {
                    fragment.meshRenderer.sharedMaterial = mesh.option.FragmentMaterial;
                }
                else
                {
                    if (core.parameters.FragmentOptions.FragmentMaterial != null)
                    {
                        fragment.meshRenderer.sharedMaterial = core.parameters.FragmentOptions.FragmentMaterial;
                    }
                    else
                    {
                        fragment.meshRenderer.sharedMaterial = mesh.material;
                    }
                }

                unityMesh.RecalculateBounds();

                var oldParent = fragment.transform.parent;
                fragment.transform.parent     = mesh.parent;
                fragment.transform.position   = mesh.position;
                fragment.transform.rotation   = mesh.rotation;
                fragment.transform.localScale = mesh.localScale;
                fragment.transform.parent     = null;
                fragment.transform.parent     = oldParent;
                fragment.cracked = true;

                var plane = mesh.option && mesh.option.Plane2D;

                var use2d = core.parameters.Use2DCollision;

                if (!core.parameters.FragmentOptions.DisableColliders)
                {
                    if (core.parameters.MeshColliders && !use2d)
                    {
                        // dont use mesh colliders for 2d plane
                        if (!plane)
                        {
                            fragment.meshCollider.sharedMesh = unityMesh;
                        }
                    }
                    else
                    {
                        if (core.parameters.Use2DCollision)
                        {
                            MeshUtils.GeneratePolygonCollider(fragment.polygonCollider2D, unityMesh);
                        }
                        else
                        {
                            fragment.boxCollider.center = unityMesh.bounds.center;
                            fragment.boxCollider.size   = unityMesh.bounds.extents;
                        }
                    }
                }

                if (mesh.option)
                {
                    mesh.option.DuplicateSettings(fragment.options);
                }

                var force = core.parameters.Force;
                if (mesh.option && mesh.option.UseLocalForce)
                {
                    force = mesh.option.Force;
                }

                // apply force to rigid body
                fragment.ApplyExplosion(mesh.transform, mesh.mesh.centroid, core.parameters.Position, core.parameters.FragmentOptions, core.parameters.UseForceVector,
                                        core.parameters.ForceVector, force, mesh.original, core.parameters.TargetFragments);

#if SHOW_DEBUG_LINES
                UnityEngine.Debug.DrawLine(settings.Position, forceVector * settings.Force, Color.yellow, 3);
#endif

                if (Watch.ElapsedMilliseconds > frameBudget)
                {
                    return(false);
                }
            }

            Watch.Stop();

            return(true);
        }
        public override bool Run(float frameBudget)
        {
            int count = this.core.pool.Count;

            while (this.core.poolIdx < count)
            {
                Fragment   fragment = this.core.pool[this.core.poolIdx];
                MeshObject post     = this.core.postList[this.core.poolIdx];
                ++this.core.poolIdx;
                if (Object.op_Implicit((Object)post.original))
                {
                    Mesh unityMesh = post.mesh.ToUnityMesh();
                    fragment.AssignMesh(unityMesh);
                    if (Object.op_Implicit((Object)post.option) && Object.op_Implicit((Object)post.option.FragmentMaterial))
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(post.option.FragmentMaterial);
                    }
                    else if (Object.op_Inequality((Object)this.core.parameters.FragmentOptions.FragmentMaterial, (Object)null))
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(this.core.parameters.FragmentOptions.FragmentMaterial);
                    }
                    else
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(post.material);
                    }
                    unityMesh.RecalculateBounds();
                    Transform parent = ((Component)fragment).get_transform().get_parent();
                    ((Component)fragment).get_transform().set_parent(post.parent);
                    ((Component)fragment).get_transform().set_position(post.position);
                    ((Component)fragment).get_transform().set_rotation(post.rotation);
                    ((Component)fragment).get_transform().set_localScale(post.localScale);
                    ((Component)fragment).get_transform().set_parent((Transform)null);
                    ((Component)fragment).get_transform().set_parent(parent);
                    if (Object.op_Inequality((Object)post.original, (Object)this.core.parameters.ExploderGameObject))
                    {
                        ExploderUtils.SetActiveRecursively(post.original, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(post.original, false);
                        ExploderUtils.SetVisible(post.original, false);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal) && Object.op_Inequality((Object)post.skinnedOriginal, (Object)this.core.parameters.ExploderGameObject))
                    {
                        ExploderUtils.SetActiveRecursively(post.skinnedOriginal, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(post.skinnedOriginal, false);
                        ExploderUtils.SetVisible(post.skinnedOriginal, false);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal) && Object.op_Implicit((Object)post.bakeObject))
                    {
                        Object.Destroy((Object)post.bakeObject, 1f);
                    }
                    bool flag           = Object.op_Implicit((Object)post.option) && post.option.Plane2D;
                    bool use2Dcollision = this.core.parameters.Use2DCollision;
                    if (!this.core.parameters.FragmentOptions.DisableColliders)
                    {
                        if (this.core.parameters.FragmentOptions.MeshColliders && !use2Dcollision)
                        {
                            if (!flag)
                            {
                                fragment.meshCollider.set_sharedMesh(unityMesh);
                            }
                        }
                        else if (this.core.parameters.Use2DCollision)
                        {
                            MeshUtils.GeneratePolygonCollider(fragment.polygonCollider2D, unityMesh);
                        }
                        else
                        {
                            BoxCollider boxCollider1 = fragment.boxCollider;
                            Bounds      bounds1      = unityMesh.get_bounds();
                            Vector3     center       = ((Bounds) ref bounds1).get_center();
                            boxCollider1.set_center(center);
                            BoxCollider boxCollider2 = fragment.boxCollider;
                            Bounds      bounds2      = unityMesh.get_bounds();
                            Vector3     extents      = ((Bounds) ref bounds2).get_extents();
                            boxCollider2.set_size(extents);
                        }
                    }
                    fragment.Explode(this.core.parameters);
                    float force = this.core.parameters.Force;
                    if (Object.op_Implicit((Object)post.option) && post.option.UseLocalForce)
                    {
                        force = post.option.Force;
                    }
                    fragment.ApplyExplosion(post.transform, post.mesh.centroid, force, post.original, this.core.parameters);
                    if ((double)this.Watch.ElapsedMilliseconds > (double)frameBudget)
                    {
                        return(false);
                    }
                }
            }
            if (this.core.parameters.DestroyOriginalObject)
            {
                foreach (MeshObject post in this.core.postList)
                {
                    if (Object.op_Implicit((Object)post.original) && !Object.op_Implicit((Object)post.original.GetComponent <Fragment>()))
                    {
                        Object.Destroy((Object)post.original);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal))
                    {
                        Object.Destroy((Object)post.skinnedOriginal);
                    }
                }
            }
            if (this.core.parameters.ExplodeSelf && !this.core.parameters.DestroyOriginalObject)
            {
                ExploderUtils.SetActiveRecursively(this.core.parameters.ExploderGameObject, false);
            }
            if (this.core.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(this.core.parameters.ExploderGameObject, false);
            }
            this.Watch.Stop();
            return(true);
        }
Exemplo n.º 16
0
        public void Allocate(int poolSize, bool useMeshColliders, bool use2dCollision)
        {
            if (this.pool != null && this.pool.Length >= poolSize && useMeshColliders == this.meshColliders)
            {
                return;
            }
            this.DestroyFragments();
            this.pool          = new Fragment[poolSize];
            this.meshColliders = useMeshColliders;
            GameObject gameObject1  = (GameObject)null;
            Fragment   objectOfType = (Fragment)Object.FindObjectOfType <Fragment>();

            if (Object.op_Implicit((Object)objectOfType))
            {
                gameObject1 = ((Component)objectOfType).get_gameObject();
            }
            else
            {
                Object @object = Resources.Load("ExploderFragment");
                if (Object.op_Implicit(@object))
                {
                    gameObject1 = Object.Instantiate(@object) as GameObject;
                }
            }
            for (int index = 0; index < poolSize; ++index)
            {
                GameObject gameObject2;
                if (Object.op_Implicit((Object)gameObject1))
                {
                    gameObject2 = (GameObject)Object.Instantiate <GameObject>((M0)gameObject1);
                    ((Object)gameObject2).set_name("fragment_" + (object)index);
                }
                else
                {
                    gameObject2 = new GameObject("fragment_" + (object)index);
                }
                gameObject2.AddComponent <MeshFilter>();
                gameObject2.AddComponent <MeshRenderer>();
                if (use2dCollision)
                {
                    gameObject2.AddComponent <PolygonCollider2D>();
                    gameObject2.AddComponent <Rigidbody2D>();
                }
                else
                {
                    if (useMeshColliders)
                    {
                        ((MeshCollider)gameObject2.AddComponent <MeshCollider>()).set_convex(true);
                    }
                    else
                    {
                        gameObject2.AddComponent <BoxCollider>();
                    }
                    gameObject2.AddComponent <Rigidbody>();
                }
                gameObject2.AddComponent <ExploderOption>();
                Fragment fragment = (Fragment)gameObject2.GetComponent <Fragment>();
                if (!Object.op_Implicit((Object)fragment))
                {
                    fragment = (Fragment)gameObject2.AddComponent <Fragment>();
                }
                gameObject2.get_transform().set_parent(((Component)this).get_gameObject().get_transform());
                this.pool[index] = fragment;
                ExploderUtils.SetActiveRecursively(gameObject2.get_gameObject(), false);
                fragment.RefreshComponentsCache();
                fragment.Sleep();
            }
        }