Advertisement
OwlyOwl

asfsadgsdgadrg

Apr 16th, 2021
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using Kuhpik;
  6. using NaughtyAttributes;
  7.  
  8. public class StickAnimation : MonoBehaviour
  9. {
  10.     [SerializeField] GameObject particleSys;
  11.     [SerializeField] GameObject machete;
  12.     [SerializeField] GameObject LoseScreen;
  13.     [SerializeField] GameObject WinScreen;
  14.  
  15.     [Header("Values")]
  16.     [SerializeField] float drawDelay;
  17.     [SerializeField] Vector2Int pixelsYToDraw;
  18.     [SerializeField] float offset;
  19.     [SerializeField] float additionalZToWorld;
  20.  
  21.     [Header("Presets")]
  22.     [SerializeField] List<Vector3> linesPresets; //3 линии внутри V3. Выбираем рандомные элемент и рисуем.
  23.     [SerializeField] GameObject linePrefab;
  24.  
  25.     [Header("Helper Scripts")]
  26.     [SerializeField] GameObject Slicer;
  27.  
  28.     [SerializeField] [ReadOnly] List<float> conturePoints;
  29.  
  30.     Dictionary<float, GameObject> lines;
  31.     float firstTouch, secondTouch;
  32.  
  33.     private int successfulCuts = 0;
  34.  
  35.     void Awake()
  36.     {
  37.         // Отключаем апдейт до момента, когда можно будет резать.
  38.         enabled = false;
  39.         linePrefab.SetActive(false);
  40.         conturePoints = new List<float>();
  41.     }
  42.  
  43.     void Update()
  44.     {
  45.         if (successfulCuts == 3)
  46.         {
  47.             WinScreen.SetActive(true);
  48.         }
  49.        
  50.  
  51.         if (Input.GetMouseButtonDown(0))
  52.         {
  53.             firstTouch = GetWorldPosition().x;
  54.             Debug.Log($"First touch {firstTouch}");
  55.         }
  56.  
  57.         else if (Input.GetMouseButtonUp(0))
  58.         {
  59.             secondTouch = GetWorldPosition().x;
  60.             Debug.Log($"Second touch {secondTouch}");
  61.  
  62.             var touches = new float[] { firstTouch, secondTouch };
  63.             var pointsCount = conturePoints.Count;
  64.             var tryCount = 0;
  65.  
  66.             for (int i = 0; i < pointsCount; i++)
  67.             {
  68.                 var hits = touches.Length;
  69.                 var point = conturePoints[i];
  70.  
  71.                 for (int j = 0; j < touches.Length; j++)
  72.                 {
  73.                     if (touches[j] <= point + offset && touches[j] >= point - offset)
  74.                     {
  75.                         //Попали куда надо
  76.                     }
  77.  
  78.                     else
  79.                     {
  80.                         hits--;
  81.                     }
  82.                 }
  83.  
  84.                 if (hits < touches.Length) //Не попали хоть в 1 точке (начало или конец свайпа)
  85.                 {
  86.                     tryCount++;
  87.                 }
  88.  
  89.                 else
  90.                 {
  91.                     //Попали везде. Удаляем линию из списка и выходим из цикла.
  92.                     successfulCuts++;
  93.                     Debug.Log(successfulCuts);
  94.                     Debug.Log("<color=orange>User's on FIRE</color>");
  95.                     lines[point].SetActive(false);
  96.                     conturePoints.Remove(point);
  97.                     return;
  98.                 }
  99.             }
  100.  
  101.             if (tryCount == pointsCount) //Не попали ни по 1 линии. Луз?
  102.             {
  103.                 Debug.Log("<color=green>Oops...</color>");
  104.                 LoseScreen.SetActive(true);
  105.             }
  106.         }
  107.     }
  108.  
  109.     public void PlayAnimation()
  110.     {
  111.         particleSys.SetActive(true);
  112.         Sequence mySequence = DOTween.Sequence();
  113.         mySequence.Append(transform.DOMove(new Vector3(0f, 0.9f, -6f), 0.8f));
  114.         mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.8f));
  115.         mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.8f));
  116.         mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.4f));
  117.         mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.4f));
  118.         mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.2f));
  119.         mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.2f)).OnComplete(() =>
  120.         {
  121.             StartCoroutine(DrawConturesRoutine());
  122.         });
  123.     }
  124.  
  125.     public void MachetePick()
  126.     {
  127.         GetComponent<MeshRenderer>().enabled = false;
  128.         machete.SetActive(true);
  129.         enabled = true;
  130.     }
  131.  
  132.     IEnumerator DrawConturesRoutine()
  133.     {
  134.         var preset = linesPresets.GetRandom();
  135.         lines = new Dictionary<float, GameObject>();
  136.         var points = new float[] { preset.x, preset.y, preset.z };
  137.  
  138.         for (int i = 0; i < points.Length; i++)
  139.         {
  140.             // Не особо нужны. Думал надо рисовать lineRenderer
  141.             // var point1 = new Vector2Int(points[i], pixelsYToDraw.x);
  142.             // var point2 = new Vector2Int(points[i], pixelsYToDraw.y);
  143.  
  144.             conturePoints.Add(points[i]); //Нет отступа по X сейчас между началом и концом разреза.
  145.  
  146.             var position = linePrefab.transform.position;
  147.             var rotation = linePrefab.transform.rotation;
  148.             position.x = points[i];
  149.  
  150.             var line = Instantiate(linePrefab, position, rotation);
  151.             lines.Add(points[i], line);
  152.             line.SetActive(true);
  153.  
  154.             yield return new WaitForSeconds(drawDelay);
  155.         }
  156.  
  157.         Slicer.SetActive(true);
  158.         MachetePick();
  159.     }
  160.  
  161.     Vector3 GetWorldPosition()
  162.     {
  163.         var mousePosition = Input.mousePosition;
  164.         mousePosition.z = Mathf.Abs(Camera.main.transform.position.z) + additionalZToWorld;
  165.  
  166.         return Camera.main.ScreenToWorldPoint(mousePosition);
  167.     }
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement