HeavenStudioPlus/Assets/Scripts/LevelEditor/TimelineEventObj.cs

114 lines
3.8 KiB
C#
Raw Normal View History

2022-01-09 23:35:55 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Starpelly;
using DG.Tweening;
namespace RhythmHeavenMania.Editor
{
public class TimelineEventObj : MonoBehaviour
{
private float startPosX;
private float startPosY;
2022-01-11 00:17:29 +00:00
public bool isDragging;
2022-01-09 23:35:55 +00:00
private Vector3 lastPos;
[Header("Components")]
[SerializeField] private RectTransform PosPreview;
[SerializeField] private RectTransform PosPreviewRef;
[SerializeField] public Image Icon;
[Header("Properties")]
private int enemyIndex;
public float length;
private bool eligibleToMove = false;
private void Update()
{
2022-01-11 00:17:29 +00:00
if (Conductor.instance.NotStopped())
2022-01-09 23:35:55 +00:00
{
Cancel();
return;
}
enemyIndex = GameManager.instance.Beatmap.entities.FindIndex(a => a.eventObj == this);
if (isDragging == true)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
2022-01-11 00:17:29 +00:00
this.transform.position = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY - 0.40f, 0);
this.transform.localPosition = new Vector3(Mathp.Round2Nearest(this.transform.localPosition.x, 0.25f), Mathp.Round2Nearest(this.transform.localPosition.y, 51.34f));
2022-01-09 23:35:55 +00:00
if (lastPos != transform.localPosition)
OnMove();
2022-01-11 00:17:29 +00:00
lastPos = this.transform.localPosition;
2022-01-09 23:35:55 +00:00
}
2022-01-11 00:17:29 +00:00
if (Input.GetMouseButtonUp(0))
OnUp();
2022-01-09 23:35:55 +00:00
}
private void OnMove()
{
2022-01-11 00:17:29 +00:00
if (GameManager.instance.Beatmap.entities.FindAll(c => c.beat == this.transform.localPosition.x && c.track == (int)(this.transform.localPosition.y / 51.34f * -1)).Count > 0)
2022-01-09 23:35:55 +00:00
{
2022-01-11 00:17:29 +00:00
// PosPreview.GetComponent<Image>().color = Color.red;
2022-01-09 23:35:55 +00:00
eligibleToMove = false;
}
else
{
2022-01-11 00:17:29 +00:00
// PosPreview.GetComponent<Image>().color = Color.yellow;
2022-01-09 23:35:55 +00:00
eligibleToMove = true;
}
}
private void OnComplete()
{
var entity = GameManager.instance.Beatmap.entities[enemyIndex];
2022-01-11 00:17:29 +00:00
entity.beat = this.transform.localPosition.x;
2022-01-09 23:35:55 +00:00
GameManager.instance.SortEventsList();
2022-01-11 00:17:29 +00:00
entity.track = (int)(this.transform.localPosition.y / 51.34f) * -1;
2022-01-09 23:35:55 +00:00
2022-01-11 00:17:29 +00:00
// this.transform.localPosition = this.transform.localPosition;
2022-01-09 23:35:55 +00:00
// transform.DOLocalMove(PosPreview.transform.localPosition, 0.15f).SetEase(Ease.OutExpo);
}
private void Cancel()
{
if (PosPreview) Destroy(PosPreview.gameObject);
eligibleToMove = false;
}
public void OnDown()
{
Vector3 mousePos;
2022-01-11 00:17:29 +00:00
/*PosPreview = Instantiate(PosPreviewRef, PosPreviewRef.transform.parent);
2022-01-09 23:35:55 +00:00
PosPreview.sizeDelta = new Vector2(100 * transform.GetComponent<RectTransform>().sizeDelta.x, transform.GetComponent<RectTransform>().sizeDelta.y);
PosPreview.transform.localPosition = this.transform.localPosition;
PosPreview.GetComponent<Image>().enabled = true;
2022-01-11 00:17:29 +00:00
PosPreview.GetComponent<Image>().color = Color.yellow;*/
2022-01-09 23:35:55 +00:00
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
2022-01-11 00:17:29 +00:00
startPosX = mousePos.x - this.transform.position.x;
startPosY = mousePos.y - this.transform.position.y;
2022-01-09 23:35:55 +00:00
isDragging = true;
}
public void OnUp()
{
isDragging = false;
if (eligibleToMove) OnComplete();
Cancel();
}
}
}