Exemplo n.º 1
0
        private void UpdateFade()
        {
            if (FadeDuration > 0.0f)
            {
                if (destructible == null)
                {
                    destructible = GetComponent <D2dDestructible>();
                }

                if (destructible != null)
                {
                    if (FadeDuration > 0.0f && Life < FadeDuration)
                    {
                        if (startColor == default(Color))
                        {
                            startColor = destructible.Color;
                        }

                        var finalColor = startColor;

                        finalColor.a *= Life / FadeDuration;

                        destructible.Color = finalColor;
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void UpdateBeforeBuild()
        {
            if (cachedDestructible == null)
            {
                cachedDestructible = GetComponent <D2dDestructible>();
            }

            if (child == null)
            {
                ReconnectChild();

                if (child == null)
                {
                    child = new GameObject("Collider");

                    child.layer = transform.gameObject.layer;

                    child.transform.SetParent(transform, false);
                }
            }

            if (cachedDestructible.Ready == true)
            {
                var w = cachedDestructible.AlphaScale.x / cachedDestructible.AlphaWidth;
                var h = cachedDestructible.AlphaScale.y / cachedDestructible.AlphaHeight;

                var offsetX = cachedDestructible.AlphaOffset.x + w * 0.5f;
                var offsetY = cachedDestructible.AlphaOffset.y + h * 0.5f;
                var scaleX  = w / 255.0f;
                var scaleY  = h / 255.0f;

                child.transform.localPosition = new Vector3(offsetX, offsetY, 0.0f);
                child.transform.localScale    = new Vector3(scaleX, scaleY, 1.0f);
            }
        }
        private void Stamp(Vector2 from, Vector2 to)
        {
            // Main camera exists?
            var mainCamera = Camera.main;

            if (mainCamera != null)
            {
                if (from != to)
                {
                    var delta = to - from;

                    lastAngle = -Mathf.Atan2(delta.x, delta.y) * Mathf.Rad2Deg;
                }

                var positionA = D2dHelper.ScreenToWorldPosition(from, Intercept, mainCamera);
                var positionB = D2dHelper.ScreenToWorldPosition(to, Intercept, mainCamera);
                var positionM = (positionA + positionB) * 0.5f;
                var length    = Vector3.Distance(positionA, positionB) * Stretch;

                if (length < Size.y)
                {
                    length = Size.y;
                }

                var size = new Vector2(Size.x, length);

                // Stamp at that point
                D2dDestructible.StampAll(positionM, size, lastAngle, StampTex, Hardness, Layers);
            }
        }
        public void UpdateMass()
        {
            if (cachedRigidbody2DSet == false)
            {
                cachedRigidbody2D    = GetComponent <Rigidbody2D>();
                cachedRigidbody2DSet = true;
            }

            if (cachedDestructibleSet == false)
            {
                cachedDestructible    = GetComponent <D2dDestructible>();
                cachedDestructibleSet = true;
            }

            var newMass = cachedDestructible.AlphaCount * MassPerSolidPixel;

            if (factorInSharpness == true)
            {
                newMass *= cachedDestructible.AlphaSharpness * cachedDestructible.AlphaSharpness;
            }

            if (newMass != lastSetMass)
            {
                cachedRigidbody2D.mass = lastSetMass = newMass;
            }
        }
Exemplo n.º 5
0
        private bool TryFixTo(D2dDestructible destructible)
        {
            var isDifferent = cachedDestructible != destructible;

            // Temporarily change parent
            transform.SetParent(destructible.transform, false);

            // Find world position of fixture if it were attached to tempDestructible
            var worldPosition = transform.TransformPoint(Offset);

            // Can fix to new point?
            if (destructible.SampleAlpha(worldPosition).a >= 128)
            {
                if (isDifferent == true)
                {
                    Unhook();

                    cachedDestructible = destructible;

                    Hook();
                }

                return(true);
            }

            // Change back to old parent
            transform.SetParent(cachedDestructible.transform, false);

            return(false);
        }
Exemplo n.º 6
0
 protected virtual void OnEnable()
 {
     if (cachedDestructible == null)
     {
         cachedDestructible = GetComponent <D2dDestructible>();
     }
 }
Exemplo n.º 7
0
        private void UpdateBeforeBuild()
        {
            if (destructible == null)
            {
                destructible = GetComponent <D2dDestructible>();
            }

            if (child == null)
            {
                ReconnectChild();

                if (child == null)
                {
                    child = new GameObject("Collider");

                    child.layer = transform.gameObject.layer;
                    child.tag   = transform.tag;
                    child.transform.SetParent(transform, false);
                }
            }

            if (destructible.AlphaIsValid == true)
            {
                var offsetX = destructible.AlphaRect.x;
                var offsetY = destructible.AlphaRect.y;
                var scaleX  = destructible.AlphaRect.width / destructible.AlphaWidth;
                var scaleY  = destructible.AlphaRect.height / destructible.AlphaHeight;

                child.transform.localPosition = new Vector3(offsetX, offsetY, 0.0f);
                child.transform.localScale    = new Vector3(scaleX, scaleY, 0.0f);
            }
        }
Exemplo n.º 8
0
        /// <summary>This will store the specified D2dDestructible's state to this snapshot.</summary>
        public void Save(D2dDestructible destructible)
        {
            if (destructible != null && destructible.Ready == true)
            {
                var total = destructible.AlphaWidth * destructible.AlphaHeight;

                if (AlphaData == null || AlphaData.Length != total)
                {
                    AlphaData = new Color32[total];
                }

                for (var i = 0; i < total; i++)
                {
                    AlphaData[i] = destructible.AlphaData[i];
                }

                Ready       = true;
                AlphaOffset = destructible.AlphaOffset;
                AlphaScale  = destructible.AlphaScale;
                AlphaWidth  = destructible.AlphaWidth;
                AlphaHeight = destructible.AlphaHeight;
            }
            else
            {
                Ready = false;
            }
        }
Exemplo n.º 9
0
        protected virtual void Update()
        {
            if (ReplaceTextureWithSprite != null)
            {
                if (destructible == null)
                {
                    destructible = GetComponent <D2dDestructible>();
                }

                destructible.ReplaceTextureWith(ReplaceTextureWithSprite);

                ReplaceTextureWithSprite = null;
            }

            if (ReplaceTextureWithTexture != null)
            {
                if (destructible == null)
                {
                    destructible = GetComponent <D2dDestructible>();
                }

                destructible.ReplaceTextureWith(ReplaceTextureWithTexture);

                ReplaceTextureWithTexture = null;
            }
        }
    void Start()
    {
        gameManager = GameObject.FindGameObjectWithTag("GameController").GetComponent <GameManager>();
        d2d         = GetComponent <Destructible2D.D2dDestructible>();
        score       = GameObject.Find("Scoreboard").GetComponent <ScoreAlpha>();

        startAlpha = d2d.OriginalAlphaCount;
    }
Exemplo n.º 11
0
        protected virtual void OnCollisionEnter2D(Collision2D collision)
        {
            if (DebugCollisions == true)
            {
                Debug.Log(name + " hit " + collision.collider.name + " for " + collision.relativeVelocity.magnitude);
            }

            if (ImpactDelay > 0.0f)
            {
                if (Time.time >= cooldownTime)
                {
                    cooldownTime = Time.time + ImpactDelay;
                }
                else
                {
                    return;
                }
            }

            var collisionMask = 1 << collision.collider.gameObject.layer;

            if ((collisionMask & ImpactMask) != 0)
            {
                var contacts = collision.contacts;

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

                    if (impact >= ImpactThreshold)
                    {
                        if (DamageOnImpact == true)
                        {
                            if (DamageDestructible == null)
                            {
                                DamageDestructible = GetComponentInChildren <D2dDestructible>();
                            }

                            if (DamageDestructible != null)
                            {
                                DamageDestructible.Damage += impact * DamageScale;
                            }
                        }

                        if (OnImpact != null)
                        {
                            OnImpact.Invoke(contact.point);
                        }

                        if (UseFirstOnly == true)
                        {
                            break;
                        }
                    }
                }
            }
        }
        public static void Fracture(D2dDestructible destructible, int count, float irregularity)
        {
            if (destructible != null && count > 0)
            {
                D2dSplitGroup.ClearAll();
                {
                    var width    = destructible.AlphaWidth;
                    var height   = destructible.AlphaHeight;
                    var mainQuad = new D2dQuad();

                    quadCount  = 1;
                    pointCount = 0;
                    xMin       = 0;
                    xMax       = width - 1;
                    yMin       = 0;
                    yMax       = height - 1;

                    mainQuad.BL = new D2dVector2(xMin, yMin);
                    mainQuad.BR = new D2dVector2(xMax, yMin);
                    mainQuad.TL = new D2dVector2(xMin, yMax);
                    mainQuad.TR = new D2dVector2(xMax, yMax);
                    mainQuad.Calculate();

                    if (quads.Count > 0)
                    {
                        quads[0] = mainQuad;
                    }
                    else
                    {
                        quads.Add(mainQuad);
                    }

                    for (var i = 0; i < count; i++)
                    {
                        SplitLargest();
                    }

                    if (irregularity > 0.0f)
                    {
                        FindPoints();
                        ShiftPoints(irregularity);
                    }

                    for (var i = 0; i < quadCount; i++)
                    {
                        var quad  = quads[i];
                        var group = D2dSplitGroup.GetSplitGroup();

                        group.AddTriangle(quad.BL, quad.BR, quad.TL);
                        group.AddTriangle(quad.TR, quad.TL, quad.BR);
                    }

                    destructible.SplitWhole(D2dSplitGroup.SplitGroups);
                }
                D2dSplitGroup.ClearAll();
            }
        }
        protected virtual void Awake()
        {
            if (destructible == null)
            {
                destructible = GetComponent <D2dDestructible>();
            }

            // Get a snapshot of the current Destructible's alpha data
            snapshot = destructible.GetSnapshot();
        }
Exemplo n.º 14
0
        private void Hook()
        {
            if (cachedDestructible == null)
            {
                cachedDestructible = GetComponentInParent <D2dDestructible>();
            }

            cachedDestructible.OnSplitStart += OnStartSplit;
            cachedDestructible.OnSplitEnd   += OnEndSplit;
        }
        protected virtual void OnEnable()
        {
            if (cachedDestructible == null)
            {
                cachedDestructible = GetComponent <D2dDestructible>();
            }

            cachedDestructible.OnSplitStart += HandleSplitStart;
            cachedDestructible.OnSplitEnd   += HandleSplitEnd;
        }
        protected virtual void Update()
        {
            // Get the main camera?
            if (mainCamera == null)
            {
                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;

                // Slice all Destructibles?
                if (mainCamera != null)
                {
                    var endMousePosition = Input.mousePosition;
                    var startPos         = mainCamera.ScreenToWorldPoint(startMousePosition);
                    var endPos           = mainCamera.ScreenToWorldPoint(endMousePosition);

                    D2dDestructible.SliceAll(startPos, endPos, Thickness, StampTex, Hardness);
                }
            }

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

                var startPos   = mainCamera.ScreenToWorldPoint(startMousePosition);
                var currentPos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
                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   = new Vector3(startPos.x, startPos.y, indicatorInstance.transform.position.z);
                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);
            }
        }
        protected virtual void Start()
        {
            if (Stamp == true)
            {
                var stampPosition = transform.position;
                var stampAngle    = StampRandomDirection == true?Random.Range(-180.0f, 180.0f) : 0.0f;



                D2dDestructible.StampAll(stampPosition, StampSize, stampAngle, StampTex, StampHardness, Mask);
            }

            if (Raycast == true && RaycastCount > 0)
            {
                var angleStep = 360.0f / RaycastCount;

                for (var i = 0; i < RaycastCount; i++)
                {
                    var angle     = i * angleStep;
                    var direction = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
                    var hit       = Physics2D.Raycast(transform.position, direction, RaycastRadius, Mask);
                    var collider  = hit.collider;

                    // Make sure the raycast hit something, and that it wasn't a trigger
                    if (collider != null && collider.isTrigger == false)
                    {
                        var strength = 1.0f - hit.fraction;                         // Do less damage if the hit point is far from the explosion

                        // Add damage?
                        if (DamagePerRay != 0.0f)
                        {
                            var destructible = collider.GetComponentInParent <D2dDestructible>();

                            if (destructible != null)
                            {
                                destructible.Damage += DamagePerRay * strength;
                            }
                        }

                        // Add force?
                        if (ForcePerRay != 0.0f)
                        {
                            var rigidbody2D = collider.attachedRigidbody;

                            if (rigidbody2D != null)
                            {
                                var force = direction * ForcePerRay * strength;

                                rigidbody2D.AddForceAtPosition(force, hit.point);
                            }
                        }
                    }
                }
            }
        }
        public virtual void Fracture()
        {
            RemainingFractures -= 1;
            RequiredDamage     *= RequiredDamageMultiplier;
            FractureCount       = Mathf.CeilToInt(FractureCount * FractureCountMultiplier);

            if (destructible == null)
            {
                destructible = GetComponent <D2dDestructible>();
            }
        }
        protected virtual void OnEnable()
        {
            if (cachedDestructibleSet == false)
            {
                cachedDestructible    = GetComponent <D2dDestructible>();
                cachedDestructibleSet = true;
            }

            cachedDestructible.OnModified += HandleModified;
            cachedDestructible.OnRebuilt  += UpdateMass;
        }
Exemplo n.º 20
0
        protected virtual void Update()
        {
            if (Input.GetMouseButton(0) && Camera.main != null)
            {
                var ray      = Camera.main.ScreenPointToRay(Input.mousePosition);
                var distance = D2dHelper.Divide(ray.origin.z, ray.direction.z);
                var point    = ray.origin - ray.direction * distance;

                D2dDestructible.StampAll(point, Size, Angle, StampTex, Hardness, Layers);
            }
        }
 protected virtual void OnEnable()
 {
     if (mainRigidbody2D == null)
     {
         mainRigidbody2D = GetComponent <Rigidbody2D>();
     }
     if (destructible == null)
     {
         destructible = GetComponent <D2dDestructible>();
     }
 }
Exemplo n.º 22
0
        public static int CalculatePointCount(D2dDestructible destructible, float pointsPerSolidPixel, bool factorInSharpness, int maxPoints)
        {
            var points = destructible.AlphaCount * pointsPerSolidPixel;

            if (factorInSharpness == true)
            {
                points *= destructible.AlphaSharpness * destructible.AlphaSharpness;
            }

            return(Mathf.Min(Mathf.CeilToInt(points), maxPoints));
        }
Exemplo n.º 23
0
        public static void Blur(D2dDestructible destructible)
        {
            var alphaData   = destructible.AlphaData;
            var alphaWidth  = destructible.AlphaWidth;
            var alphaHeight = destructible.AlphaHeight;

            D2dHelper.ReserveTempAlphaData(alphaWidth, alphaHeight);

            BlurHorizontally(alphaData, D2dHelper.tempAlphaData, alphaWidth, alphaHeight);

            BlurVertically(D2dHelper.tempAlphaData, alphaData, alphaWidth, alphaHeight);
        }
Exemplo n.º 24
0
        private bool CheckMet()
        {
            if (damage == true)
            {
                if (cachedDamage == null)
                {
                    cachedDamage = GetComponent <D2dDamage>();
                }

                if (cachedDamage != null)
                {
                    if (cachedDamage.Damage < damageMin || cachedDamage.Damage >= damageMax)
                    {
                        return(false);
                    }
                }
            }

            if (alphaCount == true)
            {
                if (cachedDestructible == null)
                {
                    cachedDestructible = GetComponent <D2dDestructible>();
                }

                if (cachedDestructible != null)
                {
                    if (cachedDestructible.AlphaCount < alphaCountMin || cachedDestructible.AlphaCount >= alphaCountMax)
                    {
                        return(false);
                    }
                }
            }

            if (alphaRatio == true)
            {
                if (cachedDestructible == null)
                {
                    cachedDestructible = GetComponent <D2dDestructible>();
                }

                if (cachedDestructible != null)
                {
                    if (cachedDestructible.AlphaRatio < alphaRatioMin || cachedDestructible.AlphaRatio >= alphaRatioMax)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 25
0
        protected virtual void Update()
        {
            cooldown -= Time.deltaTime;

            if (cooldown <= 0.0f)
            {
                cooldown = Delay;

                var angle = Random.Range(0.0f, 360.0f);

                D2dDestructible.StampAll(transform.position, Size, angle, StampTex, Hardness, Layers);
            }
        }
Exemplo n.º 26
0
        protected virtual void OnEnable()
        {
            if (cachedCollisionHandler == null)
            {
                cachedCollisionHandler = GetComponent <D2dCollisionHandler>();
            }
            if (cachedDestructible == null)
            {
                cachedDestructible = GetComponent <D2dDestructible>();
            }

            cachedCollisionHandler.OnCollision += Collision;
        }
        protected virtual void OnEnable()
        {
            if (destructible == null)
            {
                destructible = GetComponent <D2dDestructible>();
            }
            if (destructible.OnDamageChanged == null)
            {
                destructible.OnDamageChanged = new D2dFloatFloatEvent();
            }

            destructible.OnDamageChanged.AddListener(OnDamageChanged);
        }
Exemplo n.º 28
0
        /// <summary>This will return a snapshot of the specified D2dDestructible.</summary>
        public static D2dSnapshotData Create(D2dDestructible destructible)
        {
            if (destructible != null && destructible.Ready == true)
            {
                var data = new D2dSnapshotData();

                data.Save(destructible);

                return(data);
            }

            return(null);
        }
Exemplo n.º 29
0
        public static void TrySplit(D2dDestructible destructible, int feather)
        {
            if (destructible != null)
            {
                Search(destructible);

                if (islands.Count > 1)
                {
                    var baseRect = new D2dRect(0, alphaWidth, 0, alphaHeight);

                    if (feather > 0)
                    {
                        baseField.Transform(baseRect, alphaWidth, alphaHeight, alphaData);

                        destructible.SplitBegin();

                        for (var i = islands.Count - 1; i >= 0; i--)
                        {
                            var island = islands[i];
                            var sprite = destructible.SplitNext(i == 0);
                            var rect   = new D2dRect(island.MinX, island.MaxX, island.MinY, island.MaxY); rect.Expand(feather); rect.ClampTo(baseRect);

                            D2dHelper.ReserveTempAlphaDataClear(rect.SizeX, rect.SizeY);

                            island.Fill(baseField, baseRect, rect);

                            sprite.SubsetAlphaWith(D2dHelper.tempAlphaData, rect, island.Count);
                        }
                    }
                    else
                    {
                        destructible.SplitBegin();

                        for (var i = islands.Count - 1; i >= 0; i--)
                        {
                            var island = islands[i];
                            var chunk  = destructible.SplitNext(i == 0);
                            var rect   = new D2dRect(island.MinX, island.MaxX, island.MinY, island.MaxY); rect.ClampTo(baseRect);

                            D2dHelper.ReserveTempAlphaDataClear(rect.SizeX, rect.SizeY);

                            island.Fill(rect);

                            chunk.SubsetAlphaWith(D2dHelper.tempAlphaData, rect);
                        }
                    }

                    destructible.SplitEnd();
                }
            }
        }
        public void UpdateFracture()
        {
            if (RemainingFractures > 0)
            {
                if (destructible == null)
                {
                    destructible = GetComponent <D2dDestructible>();
                }

                if (destructible.Damage >= RequiredDamage)
                {
                    Fracture();
                }
            }
        }