Example #1
0
    [HideInInspector] public int dirc;                    //这个变量表明N极(红)所对应方向,0123分别表示上右下左。

    //private AudioManager amr;
    //private AudioClip clip_move = Resources.Load<AudioClip>("Sound/移动");

    void Start()
    {
        ani        = GetComponent <Animator>();
        rBody      = GetComponent <Rigidbody2D>();
        meg_script = MegArea.GetComponent <megarea_control>();
        IsGround   = true;
        IsOnPlayer = false;
        ani.SetBool("Isplayer1", Isplayer1?true:false);//选择玩家不同的动画

        //如果是另一个小人,则换精灵(磁极,脚 )
        if (!Isplayer1)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();
            dic.Add("MegArea", "player_1");
            dic.Add("foot_left", "player_13");
            dic.Add("foot_right", "player_14");

            Sprite[]         spPokers = Resources.LoadAll <Sprite>("player");
            SpriteRenderer[] childs;
            childs = GetComponentsInChildren <SpriteRenderer>();
            foreach (SpriteRenderer child in childs)
            {
                if (dic.ContainsKey(child.name))
                {
                    //child.sprite = spPokers.
                    foreach (Sprite sp in spPokers)
                    {
                        if (dic[child.name] == sp.name)
                        {
                            child.sprite = sp;
                            break;
                        }
                    }
                }
            }
        }
    }
Example #2
0
    //小人触发引力区域
    private void OnTriggerStay2D(Collider2D collision)
    {
        //非磁性物质引力模板
        if (collision.gameObject.tag == "???")//其它非磁物质,总吸引
        {
            Rigidbody2D other = collision.gameObject.GetComponent <Rigidbody2D>();
            Rigidbody2D rBody = GetComponentInParent <Rigidbody2D>();
            //int other_angel = collision.GetComponent<PlayerControl>().angle;
            Vector2 r_n = collision.gameObject.transform.position - NP.transform.position;
            Vector2 r_s = collision.gameObject.transform.position - SP.transform.position;
            float   mu  = Time.fixedDeltaTime * Maxstrength_H * other.mass;
            Vector2 f_n = (r_n.normalized) * mu /
                          (Mathf.Pow(Mathf.Max(r_n.magnitude, 0.2f), 3));
            Vector2 f_s = (r_s.normalized) * mu /
                          (Mathf.Pow(Mathf.Max(r_s.magnitude, 0.2f), 3));
            other.AddForce(-1f * f_n);
            other.AddForce(-1f * f_s);
            rBody.AddForceAtPosition(1f * f_n, NP.transform.position);
            rBody.AddForceAtPosition(1f * f_s, SP.transform.position);
        }
        //2个玩家相互吸引和排斥,重写磁力,改为线性磁力20190718
        //第4次调整模板20190718
        else if (collision.gameObject.tag == "Player")// collision.gameObject.tag == "Player")//与其它玩家,有相互吸引和排斥。
        {
            //取父物体的rdgidbody用于施力
            Rigidbody2D other = collision.gameObject.GetComponentInParent <Rigidbody2D>();
            //取对象的strength量及np,sp方位
            megarea_control other_script = collision.gameObject.GetComponentInChildren <megarea_control>();
            GameObject      other_NP     = other_script.NP;
            GameObject      other_SP     = other_script.SP;
            GameObject      other_CE     = other_script.CE;
            Vector2         other_dis    = other_NP.transform.position - other_SP.transform.position;
            Vector2         me_dis       = NP.transform.position - SP.transform.position;
            //4组距离向量
            //当2个小人站在单方向平台上时,计算它们自身的me_dis并不能==0,
            //而是无限接近于0,不能使用==0来判断  20190719
            if (Mathf.Abs(other_dis.x) < 0.1f && Mathf.Abs(me_dis.x) < 0.1f)
            {
                direct = 1;                                                             //上下结构
            }
            else if (Mathf.Abs(other_dis.y) < 0.1f && Mathf.Abs(me_dis.y) < 0.1f)
            {
                direct = 2;                                                                  //左右结构
            }
            else
            {
                direct = 0; force_type = 0;
            }
            if (direct == 1 || direct == 2)     //同为上下结构或左右结构时产生力
            {
                Vector2[] dis = new Vector2[4]; // r_n_n, r_s_s, r_n_s, r_s_n;
                if (direct == 1)
                {                               //上下结构
                    //上下结构中,如果2个角色减向量在x轴投影大于y轴投影,则磁力不成立。
                    //主要防止2个角色上下结构但并列排放时产生力作用
                    Vector2 ce_ver = other_CE.transform.position - CE.transform.position;
                    if (Mathf.Abs(ce_ver.x) > 0.75 * Mathf.Abs(ce_ver.y))
                    {
                        return;                                                //不成立时返回。
                    }
                    dis[0]             = new Vector2(0f, other_NP.transform.position.y - NP.transform.position.y);
                    dis[1]             = new Vector2(0f, other_SP.transform.position.y - SP.transform.position.y);
                    dis[2]             = new Vector2(0f, other_SP.transform.position.y - NP.transform.position.y);
                    dis[3]             = new Vector2(0f, other_NP.transform.position.y - SP.transform.position.y);
                    strength_max       = Maxstrength_V;
                    other_strength_max = other_script.Maxstrength_V;
                    strength_min       = Minstrength_V;
                    other_strength_min = other_script.Minstrength_V;
                }
                else if (direct == 2)//左右结构
                {
                    //同上下结构
                    Vector2 ce_ver = other_CE.transform.position - CE.transform.position;
                    if (Mathf.Abs(ce_ver.y) > 0.75 * Mathf.Abs(ce_ver.x))
                    {
                        return;                                                //不成立时返回。
                    }
                    dis[0]             = new Vector2(other_NP.transform.position.x - NP.transform.position.x, 0f);
                    dis[1]             = new Vector2(other_SP.transform.position.x - SP.transform.position.x, 0f);
                    dis[2]             = new Vector2(other_SP.transform.position.x - NP.transform.position.x, 0f);
                    dis[3]             = new Vector2(other_NP.transform.position.x - SP.transform.position.x, 0f);
                    strength_max       = Maxstrength_H;
                    other_strength_max = other_script.Maxstrength_H;
                    strength_min       = Minstrength_H;
                    other_strength_min = other_script.Minstrength_H;
                }
                //从4组向量中找出距离最小的向量进而施力,将其余向量置0
                float minn = 0x3f3f3f3f;
                int   pos  = 0;//取向量距离最小的向量序号
                for (int i = 0; i < 4; i++)
                {
                    if (minn > dis[i].sqrMagnitude)
                    {
                        pos  = i;
                        minn = dis[i].sqrMagnitude;
                    }
                }
                Vector2[] f_res  = new Vector2[4];                    // r_n_n, r_s_s, r_n_s, r_s_n;各个向量的力大小
                float     mu_max = strength_max + other_strength_max; // *Time.fixedDeltaTime ;
                float     mu_min = strength_min + other_strength_min;
                for (int i = 0; i < 4; i++)
                {
                    if (i == pos)
                    {
                        //线性磁力,在0.5到3.5的距离内,磁力从mu_max到mu_min之间线性递减
                        //mu为约定的磁铁挨在一起的最大磁力,0.5为约定的磁铁靠近时最近的距离,5f为约定的磁铁最远距离为3.5时的磁力值
                        f_res[i] = (dis[i].normalized) * Mathf.Max(0f, mu_max - (dis[i].magnitude - 0.5f) * (mu_max - mu_min) / (3.5f - 0.5f));
                        //Debug.Log("time:" + Time.fixedDeltaTime);
                        // Debug.Log("str:"+ strength_max + strength_max);

                        if (i < 2)
                        {
                            force_type = 2;       //0和1 时,斥力,否则引力。
                        }
                        else
                        {
                            force_type = 1;
                        }
                    }
                    else
                    {
                        f_res[i] = new Vector2(0f, 0f);
                    }
                }
                //Debug.Log(f_res[0]+ f_res[1]+ f_res[2]+ f_res[3]);
                //4组力施力
                other.AddForceAtPosition(1f * f_res[0], other_CE.transform.position);
                other.AddForceAtPosition(1f * f_res[1], other_CE.transform.position);
                other.AddForceAtPosition(-1f * f_res[2], other_CE.transform.position);
                other.AddForceAtPosition(-1f * f_res[3], other_CE.transform.position);
            }
        }
    }
Example #3
0
    void Start()
    {
        ani        = GetComponent <Animator>();
        rBody      = GetComponent <Rigidbody2D>();
        meg_script = MegArea.GetComponent <megarea_control>();
        ani.SetBool("Isplayer1", Isplayer1?true:false);//选择玩家不同的动画

        //如果是另一个小人,则换精灵(磁极,脚 )
        if (!Isplayer1)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();
            dic.Add("MegArea", "player_1");
            dic.Add("foot_left", "player_13");
            dic.Add("foot_right", "player_14");

            Sprite[]         spPokers = Resources.LoadAll <Sprite>("player");
            SpriteRenderer[] childs;
            childs = GetComponentsInChildren <SpriteRenderer>();
            foreach (SpriteRenderer child in childs)
            {
                if (dic.ContainsKey(child.name))
                {
                    //child.sprite = spPokers.
                    foreach (Sprite sp in spPokers)
                    {
                        if (dic[child.name] == sp.name)
                        {
                            child.sprite = sp;
                            break;
                        }
                    }
                }
            }
        }
        //代码获取四个边共8个对象
        foreach (SpriteRenderer it in edges.GetComponentsInChildren <SpriteRenderer>())
        {
            switch (it.name)
            {
            case "left_n":
                left_n = it;
                break;

            case "left_s":
                left_s = it;
                break;

            case "right_n":
                right_n = it;
                break;

            case "right_s":
                right_s = it;
                break;

            case "top_n":
                top_n = it;
                break;

            case "top_s":
                top_s = it;
                break;

            case "bottom_n":
                bottom_n = it;
                break;

            case "bottom_s":
                bottom_s = it;
                break;
            }
        }
        Setedges();
    }