U
U
ukol_m2021-01-20 03:33:16
Arrays
ukol_m, 2021-01-20 03:33:16

How to determine the ordinal number of the active object from a two-dimensional array?

For understanding, the game is three in a line.
6008c99655d53355237712.png
There are two scripts.
The first one is responsible for filling the playing field with prefabs (working scripts, without long "ifs"). The smilebox objects are created in a two-dimensional array.

public class StartGame : MonoBehaviour
{
    public GameObject box;
    private float BoxX = 11;
    private float BoxY = 0.27f;
    public int countX;
    public int countY;
    private GameObject[,] field;
    public GameObject[] smile;
    public GameObject[,] smilebox;
    int rnd;
    int[,] rand;
    int tr1;
    int tr2;
    int tr3;
    int x1;
    int y1;

    void Start()
    {
        int n = 0;       
        rand = new int[countX+1, countY+1]; 
        field = new GameObject[countX+1, countY+1];
        smilebox = new GameObject[countX+1, countY+1];
       
        while (n == 0) //дополнительные проверки для создания поля смайлов, соответствующе необходимым критериям
        {
            n = 1;
            for (int x = 0; x <= countX; x++)
            {
                for (int y = 0; y <= countY; y++)
                {                 
                    rnd = Random.Range(0, 6);                    
                    if (y > 1 && rand[x, y - 2] == rand[x, y - 1])
                    {
                        tr1 = rand[x, y - 1]; }
                    if (x > 1 && rand[x - 2, y] == rand[x - 1, y])
                    {
                        tr2 = rand[x - 1, y];  }
                    if (x > 0 && y > 0 && rand[x - 1, y] == rand[x - 1, y - 1] && rand[x - 1, y] == rand[x, y - 1])
                    {
                        tr3 = rand[x - 1, y];
                    while (rnd == tr1 || rnd == tr2 || rnd == tr3)
                    {
                        rnd = Random.Range(0, 6);}
                    rand[x, y] = rnd;      
                    tr1 = 100;
                    tr2 = 100;
                    tr3 = 100;}}  }
        for (int x = 0; x <= countX; x++)
        {
            for (int y = 0; y <= countY; y++)
            {                
                rnd = rand[x, y]; // выбор спрайта
                field[x, y] = Instantiate(box, new Vector2(BoxX + x, BoxY - y), Quaternion.identity); // создание игрового поля
                smilebox[x, y] = Instantiate(smile[rnd], new Vector2(BoxX + x, BoxY - y), Quaternion.identity); //создание смайла объекта                
            } } } }


The second script called smilecontroller resides on each object created in the first script and is responsible for moving the active smilebox.

public class SmileController : MonoBehaviour
{
    public StartGame startgame;
    bool MouseDown = false;
    Vector2 StartPosicion;
    Vector2 FinishPosicion;
    Vector2 TriggerPosicion;
    Vector2 VectorTrigger;
    int x3;
    int y3;
   
    private void Start()
    {
        StartPosicion = GetComponent<Transform>().position; // присвоение начальной позиции 
    }
    void OnMouseDown()
    {       
        { MouseDown = true;
            GetComponent<BoxCollider2D>().isTrigger = true; //активация тригера у выбранного объекта           
        }
    }
    void OnMouseUp()
    {       
        MouseDown = false;
        GetComponent<BoxCollider2D>().isTrigger = false; 
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        TriggerPosicion = other.GetComponent<Transform>().position; // стартовая позиция объекта-взаимодействия 
        VectorTrigger =  TriggerPosicion - StartPosicion; // вектор перемещения при взаимодействии
        float x2 = Mathf.Abs(VectorTrigger.x);
        float y2 = Mathf.Abs(VectorTrigger.y);     
        if (VectorTrigger.x > 0) // разные условия направления перемещения по вектору
        {
            x3 = 1;
        }
        else
        {
            x3 = -1;
        }
        if (VectorTrigger.y > 0)
        {
            y3 = 1;
        }
        else
        {
            y3 = -1;
        }
        if (x2>y2)
        {
            FinishPosicion = new Vector2(x3, 0);                 
                }
        else
        {
            FinishPosicion = new Vector2(0, y3);
        }
        MouseDown = false;
        GetComponent<BoxCollider2D>().isTrigger = false;
        StartPosicion = StartPosicion + FinishPosicion; // окончательная позиция
    }
    private void Update()
    {
        Vector2 Cursor = Input.mousePosition;
        Cursor = Camera.main.ScreenToWorldPoint(Cursor);
        float x1 = Mathf.Abs(Cursor.x - StartPosicion.x);
        float y1 = Mathf.Abs(Cursor.y - StartPosicion.y);
        if (MouseDown) // перемещение только по вертикали, либо горизонтали
            {
                if (x1 > y1)
                {
                    this.transform.position = new Vector2(Cursor.x, StartPosicion.y);                }
                else
                {
                    this.transform.position = new Vector2(StartPosicion.x, Cursor.y);}}        
        else
        {
            this.transform.position = StartPosicion; }}}


It is necessary to define x and y of the active smilebox[x,y] to further check the conditions between adjacent smileys and remove them when the conditions are met.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-01-20
@freeExec

Get fields X and Y for smilebox. And when creating, set them to him.

D
Dmitry Pavlov, 2021-01-21
@Stalker31

Can you create a structure?

struct 2D{
public 2D(int x,int y){X=x;Y=y;}
int X;
int Y;
}

Enter the desired coordinates into it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question