Ejemplo n.º 1
0
    void OrderStickyIce(Transform t_other, ICone stickyOrigin)
    {
        _attribute.runSim = false;
        Transform myStickyParent = transform;

        ICone        otherIce = t_other.GetComponent <ICone>();
        IceAttribute stickyOrigin_Attribute = stickyOrigin.Get_attribute();


        stickyOrigin_Attribute.stickyParent = t_other;
        stickyOrigin.Set_posInCone(stickyOrigin.Get_transform().position - t_other.position);
        stickyOrigin_Attribute.onGround |= otherIce == null || otherIce.Get_attribute().onGround;
        stickyOrigin_Attribute.runSim   &= !stickyOrigin_Attribute.onGround && stickyOrigin == this;//false;
        if (stickyOrigin_Attribute.onGround)
        {
            return;
        }


        //Setze Impuls und entsprechend der Position zum Zentrum und setze anschließend das Zentrum neu:
        IceAttribute otherIce_Attribute = otherIce.Get_attribute();

        //Geschwindigkeit und Dreh-impuls:
        Vector2 diff     = ((Vector2)(otherIce.Get_transform().position - stickyOrigin.Get_transform().position)).normalized;
        Vector2 velocity = otherIce.Get_rb().velocity - stickyOrigin.Get_rb().velocity;
        //Debug.Log("diff_vel: " + velocity + "\nother_vel: " + otherIce.Get_rb().velocity + ", vel: " + stickyOrigin.Get_rb().velocity);

        float diff_angle       = Mathf.Atan2(velocity.y, velocity.x) - Mathf.Atan2(diff.y, diff.x);
        float vel_mag          = stickyOrigin.Get_rb().velocity.magnitude;
        float countFactor      = stickyOrigin_Attribute.stickyCount / (float)(stickyOrigin_Attribute.stickyCount + otherIce_Attribute.stickyCount);
        float otherCountFactor = 1 - countFactor;

        t_other.GetComponent <ICone>().Get_rb().velocity = t_other.GetComponent <ICone>().Get_rb().velocity *otherCountFactor - diff * vel_mag * Mathf.Cos(diff_angle) * countFactor;
        //otherIce_Attribute.stickyRotation += Mathf.Sin(diff_angle) * vel_mag * countFactor * 0.05f;//Drehimpuls

        //Update des Zentrums:
        otherIce_Attribute.stickyCenter = otherIce_Attribute.stickyCenter * otherCountFactor + stickyOrigin_Attribute.stickyCenter * countFactor;
        otherIce_Attribute.stickyCount += stickyOrigin_Attribute.stickyCount;
    }
Ejemplo n.º 2
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        int _layer = other.gameObject.layer;

        if (isGettingDestroyed || _layer == 19 /*iceOnly*/)
        {
            return;
        }

        if (id > 0)//Eis ist auf der Waffel
        {
            if (other.tag == "Col" || _layer == 8 || (_layer > 11 && _layer < 20))
            {
                return;                                                                   //Falls von einem anderen Eis/Waffel getroffen, dann ignoriere es: Eingehendes Eis handelt sich selbst
            }
            //Eis wird runtergeschubst:
            //Debug.Log(name + " leaving: posInCone: " + posInCone);
            if (_attribute.explosionRange > 0)
            {
                GameObject explsn = Instantiate(iceManager.explosionPrefab, transform.position, Quaternion.identity);
                explsn.transform.localScale = Vector3.one * _attribute.explosionRange * transform.localScale.x * .5f;
                explsn.GetComponent <ExplosionScript>().exclusions.Add(gameObject);
            }

            cone.blockCone();
            cone.RemoveIce(id);
            RemoveFromCone();
            StartCoroutine(PushIceOut());
            cScript.DoShake(2000);
            return;
        }
        else//Eis fliegt durch die Gegend
        {
            if (block_ice_enter || _layer == 13 || _layer == 14)
            {
                return;                                                  //ist in der nicht-berühr-Phase oder von einem Effekt betroffen
            }
            //Weder Eis noch Cone getroffen:
            if (_layer != 8 && _layer != 12)
            {
                //Hier den Code einfügen, der beschreibt, was passiert, wenn fliegendes Eis in Kontakt mit der Umgebung kommt:

                if (_attribute.reactOnImpact)
                {
                    if (_attribute.life_current > 1)
                    {
                        //Growth
                        if (_attribute.growing != 0 && Mathf.Sign(_attribute.growing) == Mathf.Sign(_attribute.endScale - transform.localScale.x))
                        {
                            _attribute.scale    += _attribute.growing * 50;
                            transform.localScale = Vector3.one * _attribute.scale;
                        }
                        //Explosion:
                        GameObject explsn = null;
                        if (_attribute.explosionRange > 0)
                        {
                            explsn = Instantiate(iceManager.explosionPrefab, transform.position, Quaternion.identity);
                            explsn.transform.localScale = Vector3.one * _attribute.explosionRange * transform.localScale.x * .5f;
                            explsn.GetComponent <ExplosionScript>().exclusions.Add(gameObject);
                        }
                        //Split:
                        if (_attribute.splitCount > 0)
                        {
                            IceScript splitIce = Instantiate(gameObject).GetComponent <IceScript>();
                            splitIce.Set_Attribute(_attribute);
                            splitIce.Get_attribute().ResetIce();
                            splitIce.Get_attribute().life_current = _attribute.life_current;
                            rb.velocity = -prevVel;

                            _attribute.life_current  = _attribute.life;
                            _attribute.reactOnImpact = false;
                            _attribute.splitCount--;
                            _attribute.scale     *= .75f;
                            _attribute.mass      *= .75f;
                            transform.localScale *= .75f;
                            rb.mass = _attribute.mass;

                            if (explsn != null)
                            {
                                explsn.GetComponent <ExplosionScript>().exclusions.Add(splitIce.gameObject);
                            }

                            Rigidbody2D split_rb = splitIce.GetComponent <Rigidbody2D>();
                            split_rb.constraints = RigidbodyConstraints2D.FreezeRotation;
                            split_rb.velocity    = prevVel;
                            iceManager.SplitIgnore(splitIce.gameObject, transform.GetChild(0).gameObject);
                        }
                    }
                }

                //Sticking on ground:
                ICone iceOrigin = GetHighestIce(this);
                if (_attribute.sticky && !_attribute.onGround)
                {
                    _attribute.onGround = true;
                    rb.constraints      = RigidbodyConstraints2D.FreezeAll;
                    OrderStickyIce(other.transform, iceOrigin);

                    /*
                     * Debug.Log(name + " hits ground");
                     * Debug.Log("pos: " + transform.position + ", calcPos: " + (other.transform.position + (Vector3)posInCone));
                     * Debug.Log("posInCone: " + posInCone);
                     * Debug.Break();
                     * //*/
                    return;
                }
                //Ground- Bounce einer Gruppe:
                if (!(_attribute.stickyParent == null || iceOrigin.Get_attribute().reflecting))
                {
                    StartCoroutine(iceOrigin.Get_attribute().SetReflectBlock());
                    iceOrigin.Get_rb().velocity *= -1;//simple reflection (pls dont kill me XP)
                }

                _attribute.life_current--;
                return;
            }

            ICone other_Ice = other.GetComponent <ICone>();
            //falls das getroffene Eis nicht auf der waffel ist:
            if (other_Ice.GetID() < 0)
            {
                //sticky:
                if (((_attribute.sticky)) &&                                             //!_attribute.onGround)) &&// || (other_Ice.Get_attribute().sticky && !other_Ice.Get_attribute().onGround)) && //Falls sticky und nicht auf Boden berührt Eis und
                    ((_attribute.stickyParent == null) ||
                     (_attribute.stickyParent != other_Ice.Get_attribute().stickyParent) //Origin ungleich other.Origin (gegen doppelkontakt)
                    ))
                {
                    //Ermittle höchstes Eis:
                    Transform myStickyParent  = transform;
                    ICone     iceOrigin       = GetHighestIce(this);
                    ICone     iceOrigin_other = GetHighestIce(other_Ice);
                    if (iceOrigin == iceOrigin_other)
                    {
                        return;                             //haben schon die gleiche Quelle
                    }
                    if (iceOrigin.Get_attribute().onGround) //Wenn die ein am Boden klebendes Eis ist, dann wird das andere eis diesem zugeordnet
                    {
                        iceOrigin_other.Get_rb().constraints = RigidbodyConstraints2D.FreezeAll;
                        OrderStickyIce(transform, iceOrigin_other);
                        StartCoroutine(Set_sticky(iceOrigin_other.Get_attribute()));
                        return;
                    }
                    //Debug.Log("origin: " + iceOrigin.Get_transform() + "\nother: " + other.transform);

                    /*
                     * //Debug.Log("sticky pre-parent: " + _attribute.stickyParent);
                     * if (_attribute.stickyParent != null)
                     * {
                     *  Debug.Log("parent: " + _attribute.stickyParent + ", parent-origin: " + _attribute.stickyParent.GetComponent<ICone>().Get_attribute().stickyParent
                     + "\nother: " + other.transform);
                     +  Debug.Log("connect: " + ((_attribute.stickyParent != other.transform) && (_attribute.stickyParent.GetComponent<ICone>().Get_attribute().stickyParent != other.transform)));
                     + }
                     + //*/
                    iceOrigin.Get_rb().constraints = RigidbodyConstraints2D.FreezeAll;
                    OrderStickyIce(other.transform, iceOrigin);

                    /*
                     * Debug.Log(name + " hits " + other.name);
                     * //Debug.Log("other: " + other.transform.position + ", self: " + transform.position + "\ndiff: " + (transform.position - _attribute.stickyParent.position) + "posInCone: " + posInCone);
                     * Debug.Log("runSim of parent: " + _attribute.stickyParent.GetComponent<ICone>().Get_attribute().runSim);
                     * Debug.Log("sticky parent " + _attribute.stickyParent + " follows " + _attribute.stickyParent.GetComponent<ICone>().Get_attribute().stickyParent);
                     * //Debug.Log("is already paired to it: " + (_attribute.stickyParent == other.transform));
                     * Debug.Break();
                     * //*/
                    StartCoroutine(Set_sticky(_attribute));
                    return;
                }
                //*
                //Bounce
                else if (!_attribute.sticky && !other_Ice.Get_attribute().sticky&& other_Ice.Get_attribute().stickyParent != null && _attribute.stickyParent == null)
                {
                    ICone iceOrigin = GetHighestIce(other_Ice);
                    if (iceOrigin.Get_attribute().onGround)
                    {
                        Vector2 diff = ((Vector2)(transform.position - other.transform.position)).normalized;
                        rb.velocity = rb.velocity.magnitude * diff;
                    }
                    else
                    {
                        Debug.Log(name + " bounced " + other.name);
                        Vector2 vel = iceOrigin.Get_rb().velocity;
                        iceOrigin.Get_rb().velocity += rb.velocity;// / iceOrigin.Get_attribute().stickyCount;
                        rb.velocity = vel;
                        //rb.velocity = (vel * iceOrigin.Get_attribute().stickyCount + rb.velocity) / (iceOrigin.Get_attribute().stickyCount + 1);
                    }
                }
                //*/
                return;
            }

            //Eis wird in den Turm integriert:
            //Update den Turm:
            rb.constraints   = RigidbodyConstraints2D.FreezeRotation;
            transform.parent = cone.transform;
            //id = other_Ice.GetID()+1;
            SetID(other_Ice.GetID() + 1);
            _attribute.ResetIce();
            _attribute.AddToCone();
            cScript.offset = new Vector2(0, ++coneIceCount * .3f);
            Camera.main.orthographicSize = cone.camSize + .3f * coneIceCount;
            cone.iceTower.Insert(id, this);
            cone.UpdateConeTower(id);
            StartCoroutine(TriggerBlink());//Damit Gruppen aufgelöst werden können
        }
    }