Cute_demon_attacks/meng_yao/Assets/script/scene_jinshakuang/minerControl.cs
2025-01-06 17:59:37 +08:00

191 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using DG.Tweening;
using Debug = UnityEngine.Debug;
public enum minerState
{
stand,
move,
dig
}
public class minerControl : MonoBehaviour
{
[Header("会find要有")]
public SpriteAniationpro myAni;
[Header("会find")]
public List<Path> paths;
//航行时间
[Header("走路时间,会find")]
public float pathsNeedTimer = 30f;
//捕鱼时间
[Header("挖矿时间,会find")]
public float fishingNeedTimer = 30f;
//休息时间
[Header("休息时间,会find")]
public float restTimer = 5f;
[Header("精灵生成点,会find")]
public Transform startPos;
[Header("精灵终点,会find")]
public Transform endPos;
[Range(0f, 1f)]
public float probabilityOfStand = 0.2f;//站立状态
[Header("精灵走路的概率")]
[Range(0f, 1f)]
public float probabilityOfWalk = 0.3f;//走路状态
[Header("精灵挖矿的概率")]
[Range(0f, 1f)]
public float probabilityOfDig = 0.5f;//挖矿状态
// Start is called before the first frame update
void Awake()
{
myAni = transform.GetComponent<SpriteAniationpro>();
if (myAni==null)
{
Debug.LogError("myAni==null");
}
}
public async void init(List<Path> path,float pathsNeedTimer,float fishingNeedTimer,float restTimer,Transform startPos,Transform endPos)
{
this.paths = path;
this.pathsNeedTimer = pathsNeedTimer+Random.Range(0,10);
this.fishingNeedTimer = fishingNeedTimer;
this.restTimer = restTimer;
this.startPos = startPos;
this.endPos = endPos;
// 等待移动完成
await MoveToEndPositionAsync(endPos.position);
// 挖矿操作
dig();
}
private async System.Threading.Tasks.Task MoveToEndPositionAsync(Vector3 targetPosition)
{
// 使用 DOTween 进行移动,并等待动画完成
Tweener tweener = transform.DOMove(targetPosition, 3f);
// 等待动画完成
await tweener.AsyncWaitForCompletion();
}
public void SetState(minerState newState)
{
switch (newState)
{
case minerState.stand:
myAni.SetAni(0);
break;
case minerState.move:
myAni.SetAni(1);
break;
case minerState.dig:
myAni.SetAni(2);
break;
}
}
void MoveToFishingPos()
{
// 随机选择一条路径
Path selectedPath = paths[Random.Range(0, paths.Count)];
// 移动到挖矿点
StartCoroutine(MoveToFishingPoint(selectedPath.pathPoints));
}
IEnumerator MoveToFishingPoint(List<Transform> path)
{
// 将路径点转换为 Vector3 数组
Vector3[] pathPoints = new Vector3[path.Count];
for (int i = 0; i < path.Count; i++)
{
pathPoints[i] = path[i].position;
}
//改变朝向
ChangeShipLook(this.transform.position, pathPoints[path.Count - 1]);
//动作变化,移动
//fishManShipContorl.SetAni(1);
SetState(minerState.move);
// 使用 DOPath 沿着路径移动到捕鱼点,并禁用旋转变化
yield return transform.DOPath(pathPoints, pathsNeedTimer, PathType.CatmullRom)
.SetOptions(false) // 禁用旋转
.SetEase(Ease.InOutSine)
.WaitForCompletion();
//挖矿
dig();
}
void dig()
{
//动作变化,捕鱼
//fishManShipContorl.SetAni(2);
SetState(minerState.dig);
StartCoroutine(movetoStatrPos());
}
IEnumerator movetoStatrPos()
{
yield return new WaitForSeconds(3);
Debug.Log("朝向左边");
transform.rotation = Quaternion.Euler(0, 180, 0);
SetState(minerState.move);
Vector3[] ve = { endPos.position, startPos.position };
// 使用 DOPath 沿着路径移动到捕鱼点,并禁用旋转变化
yield return transform.DOPath(ve, pathsNeedTimer, PathType.CatmullRom)
.SetOptions(false) // 禁用旋转
.SetEase(Ease.InOutSine)
.WaitForCompletion();
SetState(minerState.stand);
yield return new WaitForSeconds(3);
MoveToFishingPos();
}
//根据前后位置改变朝向
void ChangeShipLook(Vector3 _startpos, Vector3 _endpos)
{
if (_startpos.x > _endpos.x)
{
Debug.Log("朝向左边");
transform.rotation = Quaternion.Euler(0, 180, 0);
}
else
{
Debug.Log("朝向右边");
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}