Skip to content
Snippets Groups Projects
backup 20.2 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
using System.Collections.Generic;

public class PinchZoom : MonoBehaviour
{
    public Canvas canvas;
    static public float canH;
    public Camera cam;
    //gambiarra é muito mais inteligente que encontrar soluções. PET - 2017
    public Button extra;
    public Button transp;
    public Image imagem;
    // heh ¬u¬
    public Button[] butt = new Button[9];
    //a escala normal dos botões vem dessa variável
    Vector3 tam;

    float tamanho;
    float prevTouchDeltaMag;
    float touchDeltaMag;
    float deltaMagnitudeDiff;
    float orthoZoomSpeed = 0.5f;// The rate of change of the orthographic size in orthographic mode.
    //smaller makes the zoom faster
    int DIV = 50;
    //O quadrante
    int quad = 10;

    bool mudou = false;
    int quadra;

    public int tesouro;
	public int pontuacao = 0;
	public Text textPontuacao;

    

    //valida a mudança de escala
    int validateScale (int button)
    {
        double x = butt[button].transform.localScale.x - deltaMagnitudeDiff * orthoZoomSpeed/DIV;
        if (x >= 1 && x <= 3.5) 
            return 1;
        else if (x < 1)
            return 0;
        else
            return 2;
    }

   //os quadrantes que não estão sendo tocados voltam pro tamanho original
   void tamanhoNormal (int qualMexi)
    {  
        // deltaMagnitudeDiff * orthoZoomSpeed/100 -> cheguei nesse valor com testes.
        //é a escala de alteração dos tamanhos dos eixos
        for (int i = 0; i < 9; i++)
        {
            if (i == qualMexi)
                butt[i].transform.localScale = ( validateScale(i) == 1)? butt[i].transform.localScale - 
                new Vector3(deltaMagnitudeDiff * orthoZoomSpeed/DIV,deltaMagnitudeDiff * orthoZoomSpeed/DIV,0) : 
                ( ( validateScale(i) == 0)? tam :butt[i].transform.localScale );
            else
                butt[i].transform.localScale = tam;
        }

    }



    static int diff = 0;
    public int numBotoes;
    public List<int> botoesAtivos = new List<int>();
    int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,a8=0,a9=0;




     void doisBotoes(int b1, int b2)
    {  
        //1x1
        butt[b1].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b1].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/2, (float)2/3);
        butt[b1].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/2, (float)2/3);
        //1x2
        butt[b2].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 0);
        butt[b2].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/2, (float)1/12);
        butt[b2].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/2, (float)1/12);
        float canvasH = canvas.GetComponent<RectTransform>().rect.height;
        float canvasW = canvas.GetComponent<RectTransform>().rect.width;
        tamanho = canvasW/(float)2 < canvasH / 4 ? canvasW/(float)2 : canvasH /4;
        for (int i = 0; i < 9; i++)
        {
            if ((i != b1) && (i != b2))
            {
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().pivot = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMax = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMin = new Vector2 (0,0);
                butt[i].GetComponent<Image>().sprite = transp.GetComponent<Image>().sprite;
            }
            else
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho,tamanho);
        }
        extra.GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho, tamanho);

    }
    void quatroBotoes(int b1,int b2,int b3,int b4)
    {
        //1x1
        butt[b1].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b1].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/4, (float)2/3);
        butt[b1].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/4, (float)2/3);
        //1x2
        butt[b2].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b2].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)3/4, (float)2/3);
        butt[b2].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)3/4, (float)2/3);
        //2x1
        butt[b3].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 0);
        butt[b3].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/4, (float)0.5/6);
        butt[b3].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/4, (float)0.5/6);
        //2x1
        butt[b4].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 0);
        butt[b4].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)3/4, (float)0.5/6);
        butt[b4].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)3/4, (float)0.5/6);
        float canvasH = canvas.GetComponent<RectTransform>().rect.height;
        float canvasW = canvas.GetComponent<RectTransform>().rect.width;
        tamanho = canvasW/(float)2.5 < canvasH / 4 ? canvasW/(float)2.5 : canvasH / 4;
        for (int i = 0; i < 9; i++)
        {
            if ((i != b1) && (i != b2)&& (i != b3) && (i != b4))
            {
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().pivot = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMax = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMin = new Vector2 (0,0);
                butt[i].GetComponent<Image>().sprite = transp.GetComponent<Image>().sprite;
            }
            else
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho,tamanho);
        }
        extra.GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho, tamanho);
    }
    void seisBotoes(int b1,int b2,int b3,int b4,int b5,int b6)
    {
        float heightSum = 1.0f/20f;
        //1x1
        butt[b1].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b1].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/4, (float)2/3+heightSum);
        butt[b1].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/4, (float)2/3+heightSum);

        //1x2
        butt[b2].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b2].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)3/4, (float)2/3+heightSum);
        butt[b2].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)3/4, (float)2/3+heightSum);

        //2x1
        butt[b3].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, (float)1/2);
        butt[b3].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/4, (float)1/3+heightSum);
        butt[b3].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/4, (float)1/3+heightSum);

        //2x2
        butt[b4].GetComponent<RectTransform>().pivot = new Vector2 ((float)2/4,  (float)1/2);
        butt[b4].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)3/4, (float)1/3+heightSum);
        butt[b4].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)3/4, (float)1/3+heightSum);

        //3x1
        butt[b5].GetComponent<RectTransform>().pivot = new Vector2 ((float)2/4,  0);
        butt[b5].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/4, 0+heightSum);
        butt[b5].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/4, 0+heightSum);

        //3x2
        butt[b6].GetComponent<RectTransform>().pivot = new Vector2 ((float)2/4,  0);
        butt[b6].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)3/4, 0+heightSum);
        butt[b6].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)3/4, 0+heightSum);
        float canvasH = canvas.GetComponent<RectTransform>().rect.height;
        float canvasW = canvas.GetComponent<RectTransform>().rect.width;
        tamanho = canvasW/(float)2.5 < canvasH / 5 ? canvasW/(float)2.5 : canvasH / 5;
        for (int i = 0; i < 9; i++)
        {
            if ((i != b1) && (i != b2)&& (i != b3) && (i != b4)&& (i != b5)&& (i != b6))
            {
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().pivot = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMax = new Vector2 (0,0);
                butt[i].GetComponent<RectTransform>().anchorMin = new Vector2 (0,0);
                butt[i].GetComponent<Image>().sprite = transp.GetComponent<Image>().sprite;
                
            }
            else
                butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho,tamanho);
        }
        extra.GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho, tamanho);
    }
    void noveBotoes(int b1,int b2,int b3,int b4,int b5,int b6,int b7,int b8,int b9)
    {
        float heightSum = 1.0f/20f;
        //1x1
        butt[b1].GetComponent<RectTransform>().pivot = new Vector2 (0,1);
        butt[b1].GetComponent<RectTransform>().anchorMax = new Vector2 (0, (float)2/3+heightSum);
        butt[b1].GetComponent<RectTransform>().anchorMin = new Vector2 (0, (float)2/3+heightSum);
        //1x2
        butt[b2].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 1);
        butt[b2].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/2, (float)2/3+heightSum);
        butt[b2].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/2, (float)2/3+heightSum);

        //1x3
        butt[b3].GetComponent<RectTransform>().pivot = new Vector2 (1, 1);
        butt[b3].GetComponent<RectTransform>().anchorMax = new Vector2 (1, (float)2/3+heightSum);
        butt[b3].GetComponent<RectTransform>().anchorMin = new Vector2 (1, (float)2/3+heightSum);

        //2x1
        butt[b4].GetComponent<RectTransform>().pivot = new Vector2 (0,  (float)1/2);
        butt[b4].GetComponent<RectTransform>().anchorMax = new Vector2 (0, (float)1/3+heightSum);
        butt[b4].GetComponent<RectTransform>().anchorMin = new Vector2 (0, (float)1/3+heightSum);

        //2x2
        butt[b5].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, (float)1/2);
        butt[b5].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/2, (float)1/3+heightSum);
        butt[b5].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/2, (float)1/3+heightSum);

        //2x3
        butt[b6].GetComponent<RectTransform>().pivot = new Vector2 (1, (float)1/2);
        butt[b6].GetComponent<RectTransform>().anchorMax = new Vector2 (1, (float)1/3+heightSum);
        butt[b6].GetComponent<RectTransform>().anchorMin = new Vector2 (1, (float)1/3+heightSum);

        //3x1
        butt[b7].GetComponent<RectTransform>().pivot = new Vector2 (0, 0);
        butt[b7].GetComponent<RectTransform>().anchorMax = new Vector2 (0, 0+heightSum);
        butt[b7].GetComponent<RectTransform>().anchorMin = new Vector2 (0, 0+heightSum);

        //3x2
        butt[b8].GetComponent<RectTransform>().pivot = new Vector2 ((float)1/2, 0);
        butt[b8].GetComponent<RectTransform>().anchorMax = new Vector2 ((float)1/2, 0+heightSum);
        butt[b8].GetComponent<RectTransform>().anchorMin = new Vector2 ((float)1/2, 0+heightSum);

        //3x3
        butt[b9].GetComponent<RectTransform>().pivot = new Vector2 (1, 0);
        butt[b9].GetComponent<RectTransform>().anchorMax = new Vector2 (1, 0+heightSum);
        butt[b9].GetComponent<RectTransform>().anchorMin = new Vector2 (1, 0+heightSum);



        // redimensiona os botões com base no tamanho do canvas
        float canvasH = canvas.GetComponent<RectTransform>().rect.height;
        float canvasW = canvas.GetComponent<RectTransform>().rect.width;
        tamanho = canvasW/(float)3.5 < canvasH / 5 ? canvasW/(float)3.5 : canvasH / 5;
        for (int i = 0; i < 9; i++)
        {
            butt[i].GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho, tamanho);
        }
        extra.GetComponent<RectTransform>().sizeDelta = new Vector2 (tamanho, tamanho);

    }



    void adicionaBotoesNaTela (int b1,int b2,int b3,int b4,int b5,int b6,int b7,int b8,int b9)
    {
        switch (diff)
        {
            case 1:
            case 2:
                doisBotoes(b1,b2);
            break;

            case 3:
            case 4:
                quatroBotoes(b1,b2,b3,b4);
            break;

            case 5:
            case 6:
            case 7:
                seisBotoes(b1,b2,b3,b4,b5,b6);
            break;

            case 8:
            case 9:
            case 10:
                noveBotoes(b1,b2,b3,b4,b5,b6,b7,b8,b9);
            break;

        }
    }




    public void Start ()
    {
		//textPontuacao = this.GetComponent<UnityEngine.UI.Text>();
		setPontuacao();
		//a orientação deve ser "Portrait" para essa fase
        Screen.orientation = ScreenOrientation.Portrait;
        //altura do canvas. Uso muitas vezes pelo script
        canH = canvas.GetComponent<RectTransform>().rect.height;
        //quais botões serão usados na fase
        a1=-1;a2=-1;a3=-1;a4=-1;a5=-1;a6=-1;a7=-1;a8=-1;a9=-1;
        //dificuldade da fase
        diff += diff < 10?1:0;
        // inicia pra usar no transform localScale dos botões
        tam = butt[0].transform.localScale;
        //gambiarra mas funciona
        //b é uma lista, dá pra acessar com índice
       
        //Nenhum botão está ativo (-1 indica botão não ativo)
        for (int i = 0; i < 10; i++)
        {
            botoesAtivos.Add(-1);
        }
        //quantos botões na tela, baseado na dificuldade da fase
        switch (diff)
            {
                case 1:
                    numBotoes = 2;
                break;
                case 2:
                    numBotoes = 2;
                break;
                case 3:
                    numBotoes = 4;
                break;
                case 4:
                    numBotoes = 4;
                break;
                case 5:
                    numBotoes = 6;
                break;
                case 6:
                    numBotoes = 6;
                break;
                case 7:
                    numBotoes = 6;
                break;
                case 8:
                    numBotoes = 9;
                break;
                case 9:
                    numBotoes = 9;
                break;
                case 10:
                    numBotoes = 9;
                break;               
            }
        int k = 0;
        int x;
        
        //quais botoes vao ser usados nessa fase?
        while (k < numBotoes)
        {
            //retorna um valor [0,9)
            x = Random.Range(0,9);
            //se ainda não foi sorteado esse número, coloca ele na lista de botoões que estão ativos
            if ( botoesAtivos[x] == -1)
            {
                botoesAtivos[x]=1;
                k++;
                if (a1 == -1) a1 = x;
                else if (a2 == -1) a2 = x;
                else if (a3 == -1) a3 = x;
                else if (a4 == -1) a4 = x;
                else if (a5 == -1) a5 = x;
                else if (a6 == -1) a6 = x;
                else if (a7 == -1) a7 = x;
                else if (a8 == -1) a8 = x;
                else if (a9 == -1) a9 = x;
                abreNovaTela.setaImagem(x);
            }
        }

        //coloca os botões na tela
        adicionaBotoesNaTela (a1,a2,a3,a4,a5,a6,a7,a8,a9);
        //escolhe um dos botões pra ter o tesouro
        tesouro = Random.Range(0,numBotoes);
        switch (tesouro)
        {
            case 0:
                tesouro = a1;
            break;
            case 1:
                tesouro = a2;
            break;
            case 2:
                tesouro = a3;
            break;
            case 3:
                tesouro = a4;
            break;
            case 4:
                tesouro = a5;
            break;
            case 5:
                tesouro = a6;
            break;
            case 6:
                tesouro = a7;
            break;
            case 7:
                tesouro = a8;
            break;
            case 8:
                tesouro = a9;
            break;
        }

    }





    void Update()
    {

        // If there are two touches on the device...
        if (Input.touchCount == 2)
        {
    


            // Store both touches.
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

            double posX = (touchOne.position.x + touchZero.position.x)/2;
            double posY = (touchOne.position.y + touchZero.position.y)/2;
       

            // Find the position in the previous frame of each touch.
            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

            // Find the magnitude of the vector (the distance) between the touches in each frame.
            prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

            // Find the difference in the distances between each frame.
            deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
            quad = HandleTwoTouches.quadrante(posX,posY, diff, quad);
            //se já não deu zoom em algum. Se tiver dado zoom, aumenta só esse
            if(butt[quadra].transform.localScale.x < 1.5 )
                switch (quad)
                {
                    case 1: 
                        quadra = a1;
                    break;
                    case 2: 
                        quadra = a2;
                    break;
                    case 3: 
				quadra = a3;                   Debug.Log("Venceu!");
                    break;
                    case 4: 
                        quadra = a4;
                    break;
                    case 5: 
                        quadra = a5;
                    break;
                    case 6: 
                        quadra = a6;
                    break;
                    case 7: 
                        quadra = a7;
                    break;
                    case 8: 
                        quadra = a8;
                    break;
                    case 9: 
                        quadra = a9;
                    break;
                    default:
                        quadra = 10;
                    break;
                }
            //se aumento 2x e ainda não mudou de imagem, dá fade out e abre a imagem da ilha
            if(butt[quadra].transform.localScale.x > 3 && !mudou)
            {
                GetComponent<fadeOut>().OnButtonClick(true, quadra);
                if (quadra == tesouro)
                {
					pontuacao++;
                	Debug.Log("Venceu!");
					// Aqui ele converte nossa pontuacao em texto, esse parâmetro "0" indica que não queremos casas decimais
					setPontuacao()
                }
                mudou = true;
            }
            //se já trocou de imagem e começou a diminuir o zoom, dá fade out e volta pra imagem inicial com botões
            else if (butt[quadra].transform.localScale.x <= 3 && mudou)
            {
                mudou = false;
                
                
                GetComponent<fadeOut>().OnButtonClick(false, quadra);
            }

            // If the camera is orthographic...
            if (cam.orthographic && (quadra < 10) && (quadra >= 0) )
            {
                //deixa todos os botões no tamanho normal, menos o que eu dei zoom
                tamanhoNormal(quadra);
                //uso um botão extra que fica por cima de todos os outros, e seu sprite é o sprite do botão que estão dando zoom
                extra.GetComponent<RectTransform>().pivot = butt[quadra].GetComponent<RectTransform>().pivot;
                extra.GetComponent<RectTransform>().position = butt[quadra].GetComponent<RectTransform>().position;
                extra.transform.localScale = butt[quadra].transform.localScale; 
                extra.GetComponent<Image>().sprite = butt[quadra].GetComponent<Image>().sprite;
            }
        }
    }
}

void setPontuacao(){
	textPontuacao.text = "Pontos: " + pontuacao.ToString();
}