public void GetAbsorbed(DamageNumber otherNumber)
        {
            otherNumber.myTarget     = this;
            myAbsorber               = otherNumber;
            myAbsorber.startLifeTime = Time.time + combinationSettings.bonusLifetime;

            if (combinationSettings.instantGain)
            {
                GiveNumber();
            }
        }
        /// <summary>
        /// Use this function on prefabs to spawn new damage numbers.
        /// Will clone this damage number.
        /// </summary>
        /// <returns></returns>
        public DamageNumber CreateNew(float newNumber, Vector3 newPosition)
        {
            DamageNumber newDN      = default;
            int          instanceID = GetInstanceID();

            //Check Pool:
            if (enablePooling && PoolAvailable(instanceID))
            {
                //Get from Pool:
                foreach (DamageNumber dn in pools[instanceID])
                {
                    newDN = dn;
                    break;
                }
                pools[instanceID].Remove(newDN);
            }
            else
            {
                //Create New:
                GameObject newGO = Instantiate <GameObject>(gameObject);
                newDN = newGO.GetComponent <DamageNumber>();

                if (enablePooling)
                {
                    newDN.CopyDefaults(this); //For Pooling Resets.
                }
            }

            newDN.number             = newNumber;   //Position
            newDN.transform.position = newPosition; //Position
            newDN.gameObject.SetActive(true);       //Active Gameobject

            if (enablePooling)
            {
                newDN.SetPoolingID(instanceID);
            }

            return(newDN);
        }
        void OnEnable()
        {
            dn = (DamageNumber)target;
            try
            {
                if (!Application.isPlaying || dn.GetTextA() == false)
                {
                    dn.GetReferences();
                }
            }catch
            {
                return;
            }

            if (!Application.isPlaying)
            {
                currentFadeIn = currentFadeOut = 0;
                dn.SetFadeIn(1);
            }

            textA = dn.GetTextA();
            textB = dn.GetTextB();
        }
        void PreparePooling()
        {
            //Add to Pool:
            pools[poolingID].Add(this);

            //Disable GameObject:
            gameObject.SetActive(false);

            //Queue Restart:
            performRestart = true;

            //Reset Runtime Variables:
            transform.localScale = baseScale = currentScale = originalScale;
            lastTargetPosition   = targetOffset = Vector3.zero;

            //Clear Combination Targets:
            myTarget = myAbsorber = null;

            //Reset some Setting Variables:
            combinationSettings.combinationGroup = originalCombinationGroup;
            prefix         = originalPrefix;
            suffix         = originalSuffix;
            followedTarget = originalFollowedTarget;
        }
        public override void OnInspectorGUI()
        {
            style          = new GUIStyle(GUI.skin.label);
            style.richText = true;

            if (repaintViews)
            {
                repaintViews = false;
                UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
            }

            //Top:
            EditorGUILayout.BeginVertical("Helpbox");
            EditorGUILayout.LabelField("<size=15><b>Damage Numbers Pro</b></size>", style);
            GUI.color = new Color(1, 1, 1, 0.7f);
            EditorGUILayout.LabelField("Variables show a <b>tooltip</b> when you hover over them.", style);
            EditorGUILayout.LabelField("Disabled features do not affect performance.", style);
            EditorGUILayout.Space();
            DisplayHints(ref spawnHelp, "How to spawn damage numbers.",
                         "Save your damage number as a <b>prefab</b>.",
                         "Spawn prefabs using <b>numberPrefab.CreateNew(number,position)</b>.",
                         "You can find more information in the documentation.");
            DisplayHints(ref glowHelp, "How to create glowing damage numbers.",
                         "First enable <b>HDR</b> and add <b>Bloom</b> post processing.",
                         "There is a detailed tutorial in the documentation.",
                         "Then increase the <b>Color Intensity</b> of the material.");
            DisplayHints(ref overlayHelp, "How to render damage numbers through walls.",
                         "Change the material's shader to <b>'Distance Field Overlay'</b>.",
                         "For 2D games look into the <b>'Sorting Group'</b> component below.");
            EditorGUILayout.EndVertical();
            EditorGUILayout.Space();

            if (textA == null || textB == null)
            {
                if (GUILayout.Button("Prepare"))
                {
                    Prepare(dn.gameObject);
                    OnEnable();
                }
                EditorGUILayout.LabelField("", style);
                EditorGUILayout.LabelField("Click the button above to prepare the gameobject.", style);
                EditorGUILayout.LabelField("Or use <b>[GameObject/2D Object/Damage Number]</b> to create a number.", style);
                return;
            }

            serializedObject.Update();

            //Warnings:
            if (dn.enablePooling && Application.isPlaying)
            {
                GUI.color = new Color(1, 0.9f, 0.5f);
                EditorGUILayout.BeginVertical("Helpbox");
                EditorGUILayout.LabelField("<b>Pooling</b> makes <b>tweaking</b> settings at <b>runtime</b> impossible.", style);
                EditorGUILayout.EndVertical();
                GUI.color = new Color(1, 1, 1);
            }

            EditorGUILayout.BeginVertical();

            //Properties:
            DisplayMainSettings();
            DisplayNumber();
            DisplayPrefix();
            DisplaySuffix();
            DisplayFading();
            DisplayMovement();
            DisplayShaking();
            DisplayStartRotation();
            DisplayCombination();
            DisplayFollowing();
            DisplayPerspective();
            DisplayPooling();

            EditorGUILayout.EndVertical();

            //Fix Variables:
            FixTextSettings(ref dn.numberSettings);
            FixTextSettings(ref dn.prefixSettings);
            FixTextSettings(ref dn.suffixSettings);
            FixFadeSettings(ref dn.fadeIn);
            FixFadeSettings(ref dn.fadeOut);
            MinZero(ref dn.digitSettings.decimals);
            MinZero(ref dn.digitSettings.dotDistance);

            //Apply Properties:
            serializedObject.ApplyModifiedProperties();

            //Update Text:
            if (Selection.gameObjects != null && Selection.gameObjects.Length > 1)
            {
                foreach (GameObject gameObject in Selection.gameObjects)
                {
                    if (gameObject != dn.gameObject)
                    {
                        DamageNumber other = gameObject.GetComponent <DamageNumber>();
                        if (other != null)
                        {
                            other.UpdateText();
                        }
                    }
                }
            }
            dn.UpdateText();

            //Preview
            FadePreview();

            //External Editors:
            ExternalEditors();
            DisplayFinalInformation();
        }
        void TryCombination()
        {
            if (enableCombination == false || combinationSettings.combinationGroup == "")
            {
                return;                                                                           //No Combination
            }
            myAbsorber            = myTarget = null;
            removedFromDictionary = false;
            givenNumber           = false;
            absorbProgress        = 0;

            //Create Dictionary:
            if (combinationDictionary == null)
            {
                combinationDictionary = new Dictionary <string, HashSet <DamageNumber> >();
            }

            //Create HashSet:
            if (!combinationDictionary.ContainsKey(combinationSettings.combinationGroup))
            {
                combinationDictionary.Add(combinationSettings.combinationGroup, new HashSet <DamageNumber>());
            }

            //Add to HashSet:
            combinationDictionary[combinationSettings.combinationGroup].Add(this);

            //Combination:
            if (combinationSettings.absorberType == AbsorberType.OLDEST)
            {
                float        oldestStartTime = Time.time + 0.5f;
                DamageNumber oldestNumber    = null;

                foreach (DamageNumber otherNumber in combinationDictionary[combinationSettings.combinationGroup])
                {
                    if (otherNumber != this && otherNumber.myAbsorber == null && otherNumber.startTime < oldestStartTime)
                    {
                        if (Vector3.Distance(otherNumber.position, position) < combinationSettings.maxDistance)
                        {
                            oldestStartTime = otherNumber.startTime;
                            oldestNumber    = otherNumber;
                        }
                    }
                }

                if (oldestNumber != null)
                {
                    GetAbsorbed(oldestNumber);
                }
            }
            else
            {
                foreach (DamageNumber otherNumber in combinationDictionary[combinationSettings.combinationGroup])
                {
                    if (otherNumber != this)
                    {
                        if (Vector3.Distance(otherNumber.position, position) < combinationSettings.maxDistance)
                        {
                            if (otherNumber.myAbsorber == null)
                            {
                                otherNumber.startTime = Time.time - 0.01f;
                            }

                            otherNumber.GetAbsorbed(this);
                        }
                    }
                }
            }
        }
Beispiel #7
0
        void SpawnNumber()
        {
            Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));

            position.z = 0;

            RaycastHit hit3D;

            Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit3D, 500);

            if (hit3D.collider != null)
            {
                position = hit3D.point;
            }
            else
            {
                if (Camera.main.GetComponent <DNP_DemoCamera>() != null)
                {
                    return;                                                     //Only spawn numbers on ray hits.
                }
            }

            float number = 1 + Mathf.Pow(Random.value, 2.5f) * 200;

            if (numberPrefabs[currentIndex].name == "Gold")
            {
                number = 1;
            }

            if (numberPrefabs[currentIndex].name == "Big Numbers")
            {
                number *= 800000 * Random.value;
            }

            DamageNumber dn = numberPrefabs[currentIndex].CreateNew(Mathf.RoundToInt(number), position); //Creating a new Damage Number from Prefab.

            if (numberPrefabs[currentIndex].name == "Text")
            {
                float random = Random.value;
                if (random < 0.33f)
                {
                    dn.prefix = "Wow";
                }
                else if (random < 0.66f)
                {
                    dn.prefix = "Nice";
                }
                else
                {
                    dn.prefix = "Great";
                }
            }

            RaycastHit2D hit = Physics2D.Raycast(position, Vector2.down, 0.1f);

            if (hit.collider != null || hit3D.collider != null)
            {
                DNP_Player target          = null;
                Transform  targetTransform = null;

                if (hit.collider != null)
                {
                    target = hit.collider.GetComponent <DNP_Player>();

                    if (target != null)
                    {
                        targetTransform = hit.collider.transform;
                    }
                }
                else
                {
                    if (hit3D.collider != null && hit3D.collider.GetComponent <DNP_SineMover>() != null)
                    {
                        targetTransform = hit3D.collider.transform;
                        dn.followDrag   = 0;
                        dn.followSpeed  = 10;
                    }
                }

                if (targetTransform != null)
                {
                    if (target != null && Input.GetMouseButtonDown(0) && numberPrefabs[currentIndex].name != "Text" && numberPrefabs[currentIndex].name != "Gold" && numberPrefabs[currentIndex].name != "Health" && numberPrefabs[currentIndex].name != "Experience")
                    {
                        target.Hurt();
                    }

                    dn.followedTarget = targetTransform;
                    dn.combinationSettings.combinationGroup += targetTransform.GetInstanceID();
                }
            }

            if (numberPrefabs[currentIndex].name == "Outline")
            {
                dn.GetReferences();
                dn.GetTextA().color = dn.GetTextB().color = Color.HSVToRGB(Random.value, 1f, 1f);
            }

            if (numberPrefabs[currentIndex].name == "Shadow")
            {
                dn.GetReferences();
                dn.GetTextA().color = dn.GetTextB().color = Color.HSVToRGB(Random.value, 0.5f, 0.9f);
            }
        }