134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class GatheringBall : MonoBehaviour, IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler
|
||
{
|
||
public float Radius = 10f;
|
||
public Dictionary<Transform, Vector3> GatherBalls = new();
|
||
public Dictionary<Transform, Vector3> DivorceBalls = new();
|
||
public float Force = 10f;
|
||
public int MaxCapacity = 100;
|
||
private int count = 0;
|
||
public GameObject GatheringBallObject;
|
||
public ParticleSystem Releaserpc;
|
||
public GameObject ExcludeFaithForce;
|
||
|
||
private int curCapacityLevel = 0;
|
||
private int[] Levels = new int[3]{ 100, 1000, 10000 };
|
||
void Update()
|
||
{
|
||
foreach (var balls in GatherBalls)
|
||
{
|
||
var ballTrans = balls.Key;
|
||
var ballVelocity = balls.Value;
|
||
ballVelocity += (transform.position - ballTrans.position) * Time.deltaTime;
|
||
ballTrans.position += ballVelocity * (Time.deltaTime * Force);
|
||
}
|
||
|
||
}
|
||
|
||
public void CapacityUpgrade()
|
||
{
|
||
//后面要补颜色变化逻辑
|
||
curCapacityLevel++;
|
||
MaxCapacity = Levels[curCapacityLevel];
|
||
Debug.Log("curCapacity:"+MaxCapacity);
|
||
}
|
||
|
||
public void AddBall(GameObject gameObject, Vector3 position)
|
||
{
|
||
gameObject.transform.position = position;
|
||
if (GatherBalls.Count > MaxCapacity)
|
||
{
|
||
DivorceBalls[gameObject.transform] = Vector3.zero;
|
||
}
|
||
else
|
||
{
|
||
gameObject.transform.SetParent(this.transform);
|
||
GatherBalls[gameObject.transform] = Vector3.zero;
|
||
}
|
||
}
|
||
//Max 3
|
||
public void AddFaith(int num)
|
||
{
|
||
//Debug.Log("CurrentFaith "+count);
|
||
if (count + num <= MaxCapacity)
|
||
{
|
||
count+=num;
|
||
GatheringBallObject.transform.localScale += 3*Vector3.one*num/MaxCapacity;
|
||
}
|
||
else
|
||
{
|
||
ExcludeFaithForce.gameObject.SetActive(true);
|
||
}
|
||
}
|
||
private bool isCoroutineRunning = false; // 标记协程状态
|
||
|
||
public void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
CursorManager.Instance.SetHover();
|
||
}
|
||
public void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
CursorManager.Instance.SetNormal();
|
||
}
|
||
public void OnPointerClick(PointerEventData eventData)
|
||
{
|
||
if (!isCoroutineRunning)
|
||
{
|
||
StartCoroutine(EmitParticlesAndReduceScale());
|
||
}
|
||
GatherBalls.Clear();
|
||
}
|
||
|
||
private IEnumerator EmitParticlesAndReduceScale()
|
||
{
|
||
|
||
float xIncreaseInterval = 0.5f / 10;
|
||
int incrementAmount = count / 10;
|
||
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
FindObjectOfType<TopBarUI>().EmitFaith(incrementAmount, transform.position);
|
||
// GameProcedureManager.Instance.FaithCount += incrementAmount;
|
||
yield return new WaitForSeconds(xIncreaseInterval);
|
||
}
|
||
|
||
// FindObjectOfType<TopBarUI>().EmitFaith(count,transform.position);
|
||
ExcludeFaithForce.gameObject.SetActive(false);
|
||
print("释放信仰");
|
||
//Singleton<GameProcedureManager>.Instance.FaithCount += count;
|
||
//
|
||
//ExcludeFaithForce.gameObject.SetActive(false);
|
||
isCoroutineRunning = true;
|
||
float elapsedTime = 0f;
|
||
float emissionDuration = count / (float)(Levels[curCapacityLevel]);
|
||
float scaleReductionSpeed = GatheringBallObject.transform.localScale.x / emissionDuration;
|
||
// // 启用粒子系统,并设置发射速率
|
||
//var emissionModule = Releaserpc.emission;
|
||
//emissionModule.rateOverTime = count / emissionDuration;
|
||
//Releaserpc.Play();
|
||
count = 0;
|
||
// 每帧缩减GatheringBallObject的scale,保持匀速减小
|
||
while (elapsedTime < emissionDuration)
|
||
{
|
||
// 缩减GatheringBallObject的scale
|
||
float scaleReduction = scaleReductionSpeed * Time.deltaTime;
|
||
GatheringBallObject.transform.localScale -= new Vector3(scaleReduction, scaleReduction, scaleReduction);
|
||
|
||
// 确保scale不会缩减到负值
|
||
//GatheringBallObject.transform.localScale = Vector3.Max(GatheringBallObject.transform.localScale, Vector3.zero);
|
||
elapsedTime += Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
//
|
||
// // 停止粒子系统
|
||
//Releaserpc.Stop();
|
||
isCoroutineRunning = false;
|
||
yield return null;
|
||
}
|
||
}
|