WXGame/Blacksmith/Assets/Script/UI/CraftingPanel.cs

130 lines
3.1 KiB
C#
Raw Normal View History

2024-11-13 16:56:37 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class CraftingPanel : MonoBehaviour
{
[SerializeField] private RectTransform pointer;
[SerializeField] private float time=0;
[SerializeField] private float maxCDTime = 2;
[SerializeField] private float minCDTime = 1;
[SerializeField] private float cdTime = 1;
[SerializeField] private float progressMax=50;
[SerializeField] private float nowProgress = 0;
[SerializeField] private Image _image;
[SerializeField] private GameObject start;
[SerializeField] private GameObject close;
[SerializeField] private SkeletonGraphic _spine;
[SerializeField] private Button closeButton;
private bool StartTime = false;
private Action<bool> _action;
private bool openData=false;
// Start is called before the first frame update
void Start()
{
}
private void OnEnable()
{
closeButton.onClick.AddListener(CLose);
}
// Update is called once per frame
void Update()
{
}
private void OnDisable()
{
closeButton.onClick.RemoveListener(CLose);
}
void CLose()
{
gameObject.SetActive(false);
}
public void StartOpen(bool bo,Action<bool> craftAction)
{
start.SetActive(true);
close.SetActive(false);
StartTime = true;
openData = bo;
_action = null;
_action = craftAction;
cdTime = Random.Range(minCDTime, maxCDTime);
time = 0;
nowProgress = 0;
_image.fillAmount = nowProgress / progressMax;
}
public void OnEvent()
{
if (time/180>0.754)
{
cdTime = Random.Range(minCDTime, maxCDTime);
time = 0;
nowProgress += 20;
EventAction();
}
else
{
cdTime = Random.Range(minCDTime, maxCDTime);
time = 0;
nowProgress -= 5;
EventAction();
}
}
private void FixedUpdate()
{
if (StartTime)
{
time += Time.deltaTime * (180/cdTime);
if (time>180)
{
cdTime = Random.Range(minCDTime, maxCDTime);
time = 0;
nowProgress += 10;
EventAction();
}
pointer.rotation = Quaternion.Euler(0,0,-time);
}
}
private void EventAction()
{
if (nowProgress<0)
{
StartTime = false;
_action.Invoke(false);
CloseEvent(false);
}
else if (nowProgress>progressMax)
{
StartTime = false;
_action.Invoke(openData ? true : false);
CloseEvent(openData ? true : false);
}
_image.fillAmount = nowProgress / progressMax;
}
public void CloseEvent(bool bo)
{
start.SetActive(false);
close.SetActive(true);
_spine.Skeleton.SetSkin(bo?"win":"lost");
_spine.Skeleton.SetSlotsToSetupPose();
}
}