Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- using Kuhpik;
- using NaughtyAttributes;
- public class StickAnimation : MonoBehaviour
- {
- [SerializeField] GameObject particleSys;
- [SerializeField] GameObject machete;
- [SerializeField] GameObject LoseScreen;
- [SerializeField] GameObject WinScreen;
- [Header("Values")]
- [SerializeField] float drawDelay;
- [SerializeField] Vector2Int pixelsYToDraw;
- [SerializeField] float offset;
- [SerializeField] float additionalZToWorld;
- [Header("Presets")]
- [SerializeField] List<Vector3> linesPresets; //3 линии внутри V3. Выбираем рандомные элемент и рисуем.
- [SerializeField] GameObject linePrefab;
- [Header("Helper Scripts")]
- [SerializeField] GameObject Slicer;
- [SerializeField] [ReadOnly] List<float> conturePoints;
- Dictionary<float, GameObject> lines;
- float firstTouch, secondTouch;
- private int successfulCuts = 0;
- void Awake()
- {
- // Отключаем апдейт до момента, когда можно будет резать.
- enabled = false;
- linePrefab.SetActive(false);
- conturePoints = new List<float>();
- }
- void Update()
- {
- if (successfulCuts == 3)
- {
- WinScreen.SetActive(true);
- }
- if (Input.GetMouseButtonDown(0))
- {
- firstTouch = GetWorldPosition().x;
- Debug.Log($"First touch {firstTouch}");
- }
- else if (Input.GetMouseButtonUp(0))
- {
- secondTouch = GetWorldPosition().x;
- Debug.Log($"Second touch {secondTouch}");
- var touches = new float[] { firstTouch, secondTouch };
- var pointsCount = conturePoints.Count;
- var tryCount = 0;
- for (int i = 0; i < pointsCount; i++)
- {
- var hits = touches.Length;
- var point = conturePoints[i];
- for (int j = 0; j < touches.Length; j++)
- {
- if (touches[j] <= point + offset && touches[j] >= point - offset)
- {
- //Попали куда надо
- }
- else
- {
- hits--;
- }
- }
- if (hits < touches.Length) //Не попали хоть в 1 точке (начало или конец свайпа)
- {
- tryCount++;
- }
- else
- {
- //Попали везде. Удаляем линию из списка и выходим из цикла.
- successfulCuts++;
- Debug.Log(successfulCuts);
- Debug.Log("<color=orange>User's on FIRE</color>");
- lines[point].SetActive(false);
- conturePoints.Remove(point);
- return;
- }
- }
- if (tryCount == pointsCount) //Не попали ни по 1 линии. Луз?
- {
- Debug.Log("<color=green>Oops...</color>");
- LoseScreen.SetActive(true);
- }
- }
- }
- public void PlayAnimation()
- {
- particleSys.SetActive(true);
- Sequence mySequence = DOTween.Sequence();
- mySequence.Append(transform.DOMove(new Vector3(0f, 0.9f, -6f), 0.8f));
- mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.8f));
- mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.8f));
- mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.4f));
- mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.4f));
- mySequence.Append(transform.DOMove(new Vector3(-0.5f, 0.9f, -6f), 0.2f));
- mySequence.Append(transform.DOMove(new Vector3(0.3f, 0.9f, -6f), 0.2f)).OnComplete(() =>
- {
- StartCoroutine(DrawConturesRoutine());
- });
- }
- public void MachetePick()
- {
- GetComponent<MeshRenderer>().enabled = false;
- machete.SetActive(true);
- enabled = true;
- }
- IEnumerator DrawConturesRoutine()
- {
- var preset = linesPresets.GetRandom();
- lines = new Dictionary<float, GameObject>();
- var points = new float[] { preset.x, preset.y, preset.z };
- for (int i = 0; i < points.Length; i++)
- {
- // Не особо нужны. Думал надо рисовать lineRenderer
- // var point1 = new Vector2Int(points[i], pixelsYToDraw.x);
- // var point2 = new Vector2Int(points[i], pixelsYToDraw.y);
- conturePoints.Add(points[i]); //Нет отступа по X сейчас между началом и концом разреза.
- var position = linePrefab.transform.position;
- var rotation = linePrefab.transform.rotation;
- position.x = points[i];
- var line = Instantiate(linePrefab, position, rotation);
- lines.Add(points[i], line);
- line.SetActive(true);
- yield return new WaitForSeconds(drawDelay);
- }
- Slicer.SetActive(true);
- MachetePick();
- }
- Vector3 GetWorldPosition()
- {
- var mousePosition = Input.mousePosition;
- mousePosition.z = Mathf.Abs(Camera.main.transform.position.z) + additionalZToWorld;
- return Camera.main.ScreenToWorldPoint(mousePosition);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment