Cute_demon_attacks/meng_yao/Assets/script/miner/minercontorl.cs
2024-10-28 15:33:52 +08:00

208 lines
5.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum State
{
stand, // 站立状态
move, // 移动状态
dig // 挖矿状态
}
public class minercontorl : MonoBehaviour
{
// 动画组件
public Animator animator;
// 当前状态
public State nowState = State.stand;
// 状态概率(在 0 到 1 之间)
[Range(0f, 1f)]
public float probabilityOfStand=0.4f; // 站立状态的概率
[Range(0f, 1f)]
public float probabilityOfMove=0.3f; // 移动状态的概率
[Range(0f, 1f)]
public float probabilityOfDig = 0.3f; // 挖矿状态的概率
// 状态切换计时器
private float timer = 0;
public float ChangeStateTimer = 3f; // 状态切换的时间间隔
// 移动参数
public float moveSpeed = 2f; // 移动速度
public Vector3 maxMovePos; // 移动范围的最大位置
public Vector3 minMovePos; // 移动范围的最小位置
private Vector3 targetPosition; // 当前目标位置
// 工作参数
public Vector3 digPosition; // 挖矿的位置
public float digTime = 5f; // 挖矿的持续时间
private bool isDiging = false;
[SerializeField]
private Vector3 newV;//随机的挖矿位置
// 初始化
void Start()
{
//animator = transform.GetComponent<Animator>();
if (animator == null)
{
Debug.LogError("Animator component is missing.");
}
// 初始化当前位置
targetPosition = transform.position;
newV = new Vector3(digPosition.x, digPosition.y + Random.Range(-0.3f, 0.3f), digPosition.z);
}
// 每帧更新
void Update()
{
// 状态切换逻辑
if (nowState == State.stand) // 只有在站立、移动完成、或工作完成后才能切换状态
{
timer += Time.deltaTime;
if (timer >= ChangeStateTimer)
{
//nowState = RangNumber(); // 随机生成新的状态
SetState(RangNumber());
timer = 0;
}
}
if (nowState==State.move&&!isDiging)
{
Move(targetPosition);
if (Vector3.Distance(transform.position, targetPosition)<0.01f)
{
SetState(State.stand);
}
}
if (nowState==State.dig)
{
Autodig();
}
}
// 设置当前状态
void SetState(State state)
{
nowState = state;
switch (state)
{
case State.dig:
animator.SetInteger("state", 2);
isDiging = true;
break;
case State.move:
animator.SetInteger("state", 1);
targetPosition = GetRandomPosition();
break;
case State.stand:
animator.SetInteger("state", 0);
break;
}
}
// 随机生成状态
State RangNumber()
{
// 计算概率总和并进行归一化
float totalProbability = probabilityOfStand + probabilityOfMove + probabilityOfDig;
if (totalProbability != 1f)
{
probabilityOfStand /= totalProbability;
probabilityOfMove /= totalProbability;
probabilityOfDig /= totalProbability;
}
// 根据概率范围随机生成状态
float randomValue = Random.value;
if (randomValue < probabilityOfStand)
{
return State.stand;
}
else if (randomValue < probabilityOfStand + probabilityOfMove)
{
return State.move;
}
else
{
return State.dig;
}
}
// 移动到指定位置
void Move(Vector3 pos)
{
// 设置朝向
if (transform.position.x < pos.x)
{
transform.rotation = Quaternion.Euler(0, 0, 0); // 朝向右边
}
else
{
transform.rotation = Quaternion.Euler(0, 180, 0); // 朝向左边
}
// 平滑移动到目标位置
transform.position = Vector3.MoveTowards(transform.position, pos, moveSpeed * Time.deltaTime);
}
// 在指定范围内生成随机位置
public Vector3 GetRandomPosition()
{
float randomX = Random.Range(minMovePos.x, maxMovePos.x);
float randomY = Random.Range(minMovePos.y, maxMovePos.y);
float randomZ = Random.Range(minMovePos.z, maxMovePos.z);
return new Vector3(randomX, randomY, randomZ);
}
void Autodig()
{
CheckDigPosition();
}
// 检查是否在挖矿位置
void CheckDigPosition()
{
if (Vector3.Distance(transform.position, newV) > 0.01f)
{
animator.SetInteger("state", 1);
Move(newV); // 移动到挖矿位置
}
else
{
StartDigging(); // 开始挖矿
}
}
// 开始工作逻辑
void StartDigging()
{
transform.rotation = Quaternion.Euler(0, 0, 0); // 朝向右边
animator.SetInteger("state", 2);
StartCoroutine(DigRoutine()); // 启动挖矿协程
}
// 挖矿协程
IEnumerator DigRoutine()
{
float workTimer = 0;
// 挖矿计时
while (workTimer < digTime)
{
workTimer += Time.deltaTime;
yield return null;
}
isDiging = false;
SetState(State.stand);
}
}