You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SnowFlakeBehaviour : MonoBehaviour
{
public bool Logo;
[AutoUI] public float Scale = 1;
[AutoUI] public int IconSize = 256;
[AutoUI] public int FlakeSize = 512;
//[AutoUI] public Vector2 Position;
[AutoUI] public float DisappearTime = 5;
[AutoUI] public float DisappearTimeRandomRange = 2;
[AutoUI] public bool ViewCollider = false;
[AutoUI] public Vector2 ColliderPosition = new Vector2(460, 0);
[AutoUI] public Vector2 ColliderSize = new Vector2(400, 400);
[SerializeField] private RectTransform _colliderRectTransform;
private RawImage _colliderImage;
public SnowFlake[] SnowFlakes;
[SerializeField] private SnowFlake _currentFlake;
public RectTransform _rectTransform;
private int positionSeed;
// Start is called before the first frame update
void Start()
{
foreach(var flake in SnowFlakes)
{
flake._snowFlakeBehaviour = this;
}
_colliderImage = _colliderRectTransform.GetComponent<RawImage>();
_rectTransform = GetComponent<RectTransform>();
positionSeed = 0;
Play();
}
// Update is called once per frame
void Update()
{
if((Time.frameCount & 0x19) == 0)
{
_colliderImage.color = ViewCollider ? Color.white : new Color(1,1,1,0);
//_rectTransform.anchoredPosition = Position;
_rectTransform.localScale = Vector3.one * Scale;
}
if(ViewCollider)
{
_colliderRectTransform.anchoredPosition = ColliderPosition;
_colliderRectTransform.sizeDelta = ColliderSize;
}
}
IEnumerator WaitToPlay()
{
float waitTime = DisappearTime + Random.Range(-DisappearTimeRandomRange, DisappearTimeRandomRange) / 2.0f;
waitTime = Mathf.Max(0, waitTime);
yield return new WaitForSeconds(waitTime);
Play();
}
public void Play()
{
if(Logo == false)
{
if (positionSeed != 0)
FlakePosition.Instance.Put(positionSeed);
positionSeed = FlakePosition.Instance.Take(out var newPos);
_rectTransform.anchoredPosition = newPos;
}
_currentFlake = SnowFlakes[Random.Range(0, SnowFlakes.Length)];
Debug.Log(_currentFlake.gameObject.name);
_currentFlake.PlayAnimation();
}
public void Trigger()
{
_currentFlake.Trigger();
}
public void ResetFlake()
{
StartCoroutine(WaitToPlay());
}
}