319 lines
9.7 KiB
C#
319 lines
9.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class BlobAnimationController : MonoBehaviour
|
|
{
|
|
public Animator animator;
|
|
private BlobController bc;
|
|
|
|
//磕头区
|
|
public ParticleSystem FaithParticleSystem;
|
|
|
|
//锤子区
|
|
public GameObject Harmer;
|
|
public Transform HarmerPosi;
|
|
public Transform HarmerStick;
|
|
public bool Bounce = true;
|
|
private AudioSource ac;
|
|
|
|
private void Start()
|
|
{
|
|
animator = GetComponent<Animator>();
|
|
bc = GetComponent<BlobController>();
|
|
ac = GetComponent<AudioSource>();
|
|
}
|
|
|
|
#region 磕头动画
|
|
private float _kowtowSpeed1 = 1.0f;
|
|
private float _kowtowSpeed2 = 1.0f;
|
|
public float KowtowSpeed1
|
|
{
|
|
get { return _kowtowSpeed1; }
|
|
set
|
|
{
|
|
_kowtowSpeed1 = value;
|
|
animator.SetFloat("kowtowSpeed1", _kowtowSpeed1);
|
|
}
|
|
}
|
|
|
|
public float KowtowSpeed2
|
|
{
|
|
get { return _kowtowSpeed2; }
|
|
set
|
|
{
|
|
_kowtowSpeed2 = value;
|
|
animator.SetFloat("kowtowSpeed2", _kowtowSpeed2);
|
|
}
|
|
}
|
|
|
|
public void KowAudio()
|
|
{
|
|
Debug.Log("KowAudio");
|
|
AudioManager.Instance.PlayKowSFX(ac);
|
|
}
|
|
public void KowTowFaith()
|
|
{
|
|
((Sun_Buildings)bc.BelongingBuilding)?.KowTowGenerate(FaithParticleSystem);
|
|
AudioManager.Instance.PlayKowtowSFX(ac);
|
|
}
|
|
public void KowtowController()
|
|
{
|
|
int x = Random.Range(1, 11);
|
|
if (x > BuffManager.Instance.KowtowParameter)
|
|
{
|
|
GetComponent<Animator>().SetBool("isKowtowing",true);
|
|
}
|
|
else
|
|
{
|
|
GetComponent<Animator>().SetBool("isKowtowing", false);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 砍树
|
|
public void CutTree()
|
|
{
|
|
((Tree_Buildings)bc.BelongingBuilding)?.CutTreeGenerate();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 锤子动画
|
|
|
|
public void StartGoUp()
|
|
{
|
|
upNum = 0;
|
|
//创建锤子
|
|
Harmer.SetActive(true);
|
|
HarmerStick.gameObject.SetActive(true);
|
|
initialParentPosition = transform.position;
|
|
startInitialParentPosition = transform.position; // 记录父物体的初始位置
|
|
initialChildAPosition = HarmerPosi.transform.position;
|
|
upNum++;
|
|
|
|
// GameObjectPool.Instance.CreateDebugSphere(transform.position);
|
|
|
|
//播放上升协程
|
|
StartCoroutine(MoveAndStretch());
|
|
}
|
|
public float moveDistance = 3f; // 父物体上升的距离
|
|
public float duration = 1f; // 上升的时间
|
|
private Vector3 startInitialParentPosition;
|
|
private Vector3 initialParentPosition;
|
|
private Vector3 initialChildAPosition;
|
|
public int upNum = 0;
|
|
private float distance;
|
|
IEnumerator MoveAndStretch()
|
|
{
|
|
float elapsedTime = 0f;
|
|
Vector3 targetPosition = initialParentPosition + new Vector3(0, moveDistance, 0); // 父物体的目标位置
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float t = elapsedTime / duration;
|
|
|
|
// 1. 父物体上升
|
|
transform.position = Vector3.Lerp(initialParentPosition, targetPosition, t);
|
|
|
|
// 2. 保持子物体A的世界坐标不变
|
|
Vector3 currentChildAWorldPos = Harmer.transform.position;
|
|
Harmer.transform.position = initialChildAPosition; // 强制子物体A保持在原始世界位置
|
|
|
|
// 3. 动态调整子物体B的长度以连接父物体和子物体A
|
|
distance = Vector3.Distance(transform.position, startInitialParentPosition); // 父物体与子物体A的距离
|
|
HarmerStick.localScale = new Vector3(HarmerStick.localScale.x, distance/(0.1281f)+1, HarmerStick.localScale.z); // 伸长子物体B的Y轴
|
|
|
|
yield return null; // 等待下一帧
|
|
}
|
|
|
|
// 最后确保父物体到达目标位置
|
|
transform.position = targetPosition;
|
|
animator.SetTrigger("POPOver");
|
|
}
|
|
//length 0.183
|
|
private int MaxNum;
|
|
public void ContinuePop()
|
|
{
|
|
int x = Random.Range(0, 101);
|
|
if (upNum <= 10)
|
|
{
|
|
if (x < BuffManager.Instance.HarmerParameter)
|
|
{
|
|
animator.SetBool("ContinuePop", true);
|
|
initialParentPosition = transform.position; // 记录父物体的初始位置
|
|
initialChildAPosition = Harmer.transform.position;
|
|
upNum++;
|
|
StartCoroutine(MoveAndStretch());
|
|
}
|
|
else
|
|
{
|
|
if (BuffManager.Instance.HarmerDoubleRotate) upNum *= 2;
|
|
MaxNum = upNum;
|
|
Harmer.transform.position = HarmerPosi.position;
|
|
HarmerStick.localScale = new Vector3(HarmerStick.localScale.x, 1, HarmerStick.localScale.z);
|
|
Harmer.SetActive(false);
|
|
HarmerStick.gameObject.SetActive(false);
|
|
animator.SetBool("ContinuePop", false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (BuffManager.Instance.HarmerDoubleRotate) upNum *= 2;
|
|
MaxNum = upNum;
|
|
Harmer.transform.position = HarmerPosi.position;
|
|
HarmerStick.localScale = new Vector3(HarmerStick.localScale.x, 1, HarmerStick.localScale.z);
|
|
Harmer.SetActive(false);
|
|
HarmerStick.gameObject.SetActive(false);
|
|
animator.SetBool("ContinuePop", false);
|
|
}
|
|
|
|
}
|
|
|
|
public void StartRotate()
|
|
{
|
|
upNum--;
|
|
if (upNum <= 0)
|
|
{
|
|
animator.SetTrigger("ContinueRotStop");
|
|
}
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
//if (Input.GetKeyDown(KeyCode.A))
|
|
//{
|
|
|
|
// animator.Play("木锤移动");
|
|
//}
|
|
KowtowSpeed1 = BuffManager.Instance.kowtowSpeed1;
|
|
KowtowSpeed2 = BuffManager.Instance.kowtowSpeed2;
|
|
|
|
animator.SetFloat("CutTree", BuffManager.Instance.cutTreeSpeed);
|
|
}
|
|
|
|
public bool isFalling;
|
|
public void FallingDown()
|
|
{
|
|
if (!isFalling)
|
|
{
|
|
isFalling = true;
|
|
Debug.Log("开始下落木雕");
|
|
StartCoroutine(FallBackToInitialPosition(0.3f*distance));
|
|
}
|
|
|
|
}
|
|
IEnumerator FallBackToInitialPosition(float time)
|
|
{
|
|
float elapsedTime = 0f;
|
|
Vector3 startPosition = transform.position; // 记录当前物体的位置
|
|
|
|
while (elapsedTime < time)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float t = elapsedTime / time;
|
|
|
|
// 父物体平滑地移动到初始位置
|
|
transform.position = Vector3.Lerp(startPosition, startInitialParentPosition, t);
|
|
|
|
yield return null; // 等待下一帧
|
|
}
|
|
|
|
// 确保物体完全回到初始位置
|
|
transform.position = startInitialParentPosition;
|
|
Debug.Log("ReachTarget");
|
|
animator.SetTrigger("NewHarmer");
|
|
|
|
StartBounceAndBack();
|
|
}
|
|
|
|
private Vector3 startPosition;
|
|
public float bounceHeight = 2f; // 弹跳的高度
|
|
public float bounceDuration = 0.7f; // 弹跳持续时间
|
|
public float returnSpeed = 2f;
|
|
public float rotationSpeed = 360f;
|
|
public void StartBounceAndBack()
|
|
{
|
|
PUNCH();
|
|
startPosition = transform.position;
|
|
if(Bounce) StartCoroutine(BounceRoutine());
|
|
else
|
|
{
|
|
isFalling = false;
|
|
Bounce = true;
|
|
if (((WoodSculpture_Buildings)bc.BelongingBuilding).isProducing)
|
|
{
|
|
animator.SetTrigger("Landed");
|
|
}
|
|
else
|
|
{
|
|
((WoodSculpture_Buildings)bc.BelongingBuilding).AddController(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerator BounceRoutine()
|
|
{
|
|
// 计算随机弹远的目标位置
|
|
float bounceDistance = Random.Range(-4.5f, -2f);
|
|
Vector3 bounceTarget = new Vector3(transform.position.x + bounceDistance, startPosition.y, startPosition.z);
|
|
|
|
// 第一步:向上跳跃,同时横向移动到目标位置
|
|
//Vector3 peakPosition = new Vector3(transform.position.x+bounceDistance/2, startPosition.y + bounceHeight, startPosition.z);
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < bounceDuration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float progress = elapsedTime / bounceDuration;
|
|
|
|
// 计算水平方向的插值
|
|
float xPosition = Mathf.Lerp(startPosition.x, bounceTarget.x, progress);
|
|
|
|
// 计算垂直方向的抛物线轨迹
|
|
float yPosition = Mathf.Lerp(startPosition.y, bounceTarget.y, progress) + bounceHeight * Mathf.Sin(progress * Mathf.PI);
|
|
|
|
// 更新物体的位置
|
|
transform.position = new Vector3(xPosition, yPosition, startPosition.z);
|
|
yield return null;
|
|
}
|
|
|
|
|
|
// 落地后设置动画参数
|
|
|
|
transform.rotation = Quaternion.identity;
|
|
|
|
// 第二步:匀速移动回起始位置
|
|
while (Vector3.Distance(transform.position, startPosition) > 0.01f)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, startPosition, returnSpeed * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
isFalling = false;
|
|
transform.position = startPosition; // 确保精确回到起始位置
|
|
transform.rotation = Quaternion.identity;
|
|
if (((WoodSculpture_Buildings)bc.BelongingBuilding).isProducing)
|
|
{
|
|
animator.SetTrigger("Landed");
|
|
}
|
|
else
|
|
{
|
|
((WoodSculpture_Buildings)bc.BelongingBuilding).AddController(this);
|
|
}
|
|
|
|
}
|
|
|
|
public void PUNCH()
|
|
{
|
|
((WoodSculpture_Buildings)bc.BelongingBuilding)?.Punch(MaxNum);
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|