Ejemplo n.º 1
0
        private void Collision(Collision2D collision)
        {
            if (cooldown <= 0.0f)
            {
                if (D2dHelper.IndexInMask(collision.gameObject.layer, mask) == true)
                {
                    var contacts = collision.contacts;

                    for (var i = contacts.Length - 1; i >= 0; i--)
                    {
                        var contact = contacts[i];
                        var normal  = collision.relativeVelocity;
                        var force   = normal.magnitude;

                        if (force >= threshold)
                        {
                            if (useSurfaceNormal == true)
                            {
                                normal = contact.normal;
                            }
                            else
                            {
                                normal /= force;
                            }

                            var point  = contact.point;
                            var pointA = point - normal * offset;
                            var pointB = point + normal * depth;
                            var matrix = D2dSlice.CalculateMatrix(pointA, pointB, thickness);

                            cooldown = delay;

                            cachedDestructible.Paint(paint, matrix, shape, color);

                            if (prefab != null)
                            {
                                Instantiate(prefab, point, Quaternion.identity);
                            }

                            if (onImpact != null)
                            {
                                onImpact.Invoke();
                            }

                            if (delay > 0.0f)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        protected virtual void Update()
        {
            // Get the main camera
            var mainCamera = Camera.main;

            // Begin dragging
            if (Input.GetKey(Requires) == true && down == false)
            {
                down = true;
                startMousePosition = Input.mousePosition;
            }

            // End dragging
            if (Input.GetKey(Requires) == false && down == true)
            {
                down = false;

                // Main camera exists?
                if (mainCamera != null)
                {
                    var endMousePosition = Input.mousePosition;
                    var startPos         = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                    var endPos           = D2dHelper.ScreenToWorldPosition(endMousePosition, Intercept, mainCamera);

                    D2dSlice.All(Paint, startPos, endPos, Thickness, Shape, Color, Layers);
                }
            }

            // Update indicator?
            if (down == true && mainCamera != null && IndicatorPrefab != null)
            {
                if (indicatorInstance == null)
                {
                    indicatorInstance = Instantiate(IndicatorPrefab);
                }

                var startPos   = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                var currentPos = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);
                var scale      = Vector3.Distance(currentPos, startPos);
                var angle      = D2dHelper.Atan2(currentPos - startPos) * Mathf.Rad2Deg;

                // Transform the indicator so it lines up with the slice
                indicatorInstance.transform.position   = startPos;
                indicatorInstance.transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
                indicatorInstance.transform.localScale = new Vector3(Thickness, scale, scale);
            }
            // Destroy indicator?
            else if (indicatorInstance != null)
            {
                Destroy(indicatorInstance.gameObject);
            }
        }
Ejemplo n.º 3
0
        public void Slice()
        {
            var positionA = transform.position;
            var positionB = transform.TransformPoint(0.0f, 1.0f, 0.0f);

            D2dSlice.All(Paint, positionA, positionB, Thickness, Shape, Color, Layers);

            // The slice won't happen until next frame, so delay the force application
            if (Force != 0.0f)
            {
                StartCoroutine(DelayedForce(positionA, positionB));
            }

            if (ParticleSystem != null && ParticlesPerUnit > 0.0f)
            {
                var particleCount = Mathf.CeilToInt(Vector3.Distance(positionA, positionB) * ParticlesPerUnit);

                if (particleCount > 0.0f)
                {
                    var emitParams = new ParticleSystem.EmitParams();
                    var positionD  = positionB - positionA;

                    if (ParticlesRandom == true)
                    {
                        for (var i = 0; i < particleCount; i++)
                        {
                            emitParams.position = positionA + positionD * Random.value;
                            emitParams.velocity = Random.insideUnitSphere;

                            ParticleSystem.Emit(emitParams, 1);
                        }
                    }
                    else
                    {
                        var step = positionD / particleCount;

                        emitParams.position = positionA + step * 0.5f;

                        for (var i = 0; i < particleCount; i++)
                        {
                            emitParams.velocity = Random.insideUnitSphere;

                            ParticleSystem.Emit(emitParams, 1);

                            emitParams.position += step;
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private IEnumerator DelayedForce(Vector3 oldPosition, Vector3 newPosition)
        {
            yield return(new WaitForEndOfFrame());

            D2dSlice.ForceAll(oldPosition, newPosition, Thickness, Force, ForceMode, Layers);
        }