修改角色包
This commit is contained in:
@@ -3,30 +3,30 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
public class AnimalChasingState<T> : MachineState where T : Component {
|
||||
public T target;
|
||||
public string ToDefault = "Eating";
|
||||
public readonly AnimalMachine animal;
|
||||
public AnimalChasingState(Machine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
// public class AnimalChasingState<T> : IStateMachine where T : Component {
|
||||
// public T target;
|
||||
// public string ToDefault = "Eating";
|
||||
// public readonly AnimalMachine animal;
|
||||
// public AnimalChasingState(StateMachine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
|
||||
public override void Enter() {
|
||||
bool valid = animal.Find(out target);
|
||||
if (!valid) { animal.ChangeState("Idle"); }
|
||||
}
|
||||
// public override void Enter() {
|
||||
// bool valid = animal.Find(out target);
|
||||
// if (!valid) { animal.ChangeState("Idle"); }
|
||||
// }
|
||||
|
||||
public override void Exit() {
|
||||
animal.movement.StopMoving();
|
||||
animal.animator.SetFloat("MoveSpeed", 0);
|
||||
}
|
||||
// public override void Exit() {
|
||||
// animal.movement.StopMoving();
|
||||
// animal.animator.SetFloat("MoveSpeed", 0);
|
||||
// }
|
||||
|
||||
public override void Trigger() {
|
||||
// public override void Trigger() {
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
public override void Update() {
|
||||
if (target == null) { animal.ChangeState(ToDefault); return; }
|
||||
bool complete = animal.movement.UpdateMove(target.transform.position);
|
||||
animal.animator.SetFloat("MoveSpeed", animal.movement.currentSpeed);
|
||||
if (complete) { animal.ChangeState(ToDefault); }
|
||||
}
|
||||
}
|
||||
// public override void Update() {
|
||||
// if (target == null) { animal.ChangeState(ToDefault); return; }
|
||||
// bool complete = animal.movement.UpdateMove(target.transform.position);
|
||||
// animal.animator.SetFloat("MoveSpeed", animal.movement.currentSpeed);
|
||||
// if (complete) { animal.ChangeState(ToDefault); }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -3,37 +3,37 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
public class AnimalEatingState : MachineState {
|
||||
public string ToDefault = "Idle";
|
||||
// public class AnimalEatingState : IStateMachine {
|
||||
// public string ToDefault = "Idle";
|
||||
|
||||
private AnimalFood target;
|
||||
// private AnimalFood target;
|
||||
|
||||
public AnimalMachine animal;
|
||||
public AnimalEatingState(Machine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
// public AnimalMachine animal;
|
||||
// public AnimalEatingState(StateMachine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
|
||||
public override void Enter() {
|
||||
if (!animal.Find(out target)) { Exit(); return; }
|
||||
// public override void Enter() {
|
||||
// if (!animal.Find(out target)) { Exit(); return; }
|
||||
|
||||
// 判断target距离是否小于0.3f
|
||||
float distance = Vector3.Distance(animal.transform.position, target.transform.position);
|
||||
if (distance >= 0.1f) { Exit(); return; }
|
||||
// // 判断target距离是否小于0.3f
|
||||
// float distance = Vector3.Distance(animal.transform.position, target.transform.position);
|
||||
// if (distance >= 0.1f) { Exit(); return; }
|
||||
|
||||
animal.animator.SetBool("Eating", true);
|
||||
}
|
||||
// animal.animator.SetBool("Eating", true);
|
||||
// }
|
||||
|
||||
public override void Exit() {
|
||||
animal.animator.SetBool("Eating", false);
|
||||
}
|
||||
// public override void Exit() {
|
||||
// animal.animator.SetBool("Eating", false);
|
||||
// }
|
||||
|
||||
public override void Trigger() {
|
||||
animal.hunger += target.nutritionValue;
|
||||
animal.hunger = Mathf.Clamp(animal.hunger, 0, animal.maxHunger);
|
||||
GameObject.Destroy(target.gameObject);
|
||||
// public override void Trigger() {
|
||||
// animal.hunger += target.nutritionValue;
|
||||
// animal.hunger = Mathf.Clamp(animal.hunger, 0, animal.maxHunger);
|
||||
// GameObject.Destroy(target.gameObject);
|
||||
|
||||
animal.ChangeState(ToDefault);
|
||||
}
|
||||
// animal.ChangeState(ToDefault);
|
||||
// }
|
||||
|
||||
public override void Update() {
|
||||
// public override void Update() {
|
||||
|
||||
}
|
||||
}
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -3,28 +3,28 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
public class AnimalIdleState : MachineState {
|
||||
public string ToDefault = "Roaming";
|
||||
// public class AnimalIdleState : IStateMachine {
|
||||
// public string ToDefault = "Roaming";
|
||||
|
||||
private float idleTime;
|
||||
// private float idleTime;
|
||||
|
||||
public AnimalMachine animal;
|
||||
public AnimalIdleState(Machine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
// public AnimalMachine animal;
|
||||
// public AnimalIdleState(StateMachine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
|
||||
public override void Enter() {
|
||||
idleTime = Random.Range(3.0f, 5.0f);
|
||||
}
|
||||
// public override void Enter() {
|
||||
// idleTime = Random.Range(3.0f, 5.0f);
|
||||
// }
|
||||
|
||||
public override void Exit() {
|
||||
// public override void Exit() {
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
public override void Trigger() {
|
||||
// public override void Trigger() {
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
public override void Update() {
|
||||
idleTime -= Time.deltaTime;
|
||||
if (idleTime <= 0) { animal.ChangeState(ToDefault); }
|
||||
}
|
||||
}
|
||||
// public override void Update() {
|
||||
// idleTime -= Time.deltaTime;
|
||||
// if (idleTime <= 0) { animal.ChangeState(ToDefault); }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -3,79 +3,79 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
public class AnimalMachine : Machine {
|
||||
[Header("饥饿度参数")]
|
||||
public float hunger = 100.0f; // 饥饿度,从0到100
|
||||
public float maxHunger = 100.0f; // 最大饥饿度
|
||||
public float searchRadius = 10.0f; // 搜索食物的半径
|
||||
// public class AnimalMachine : StateMachine {
|
||||
// [Header("饥饿度参数")]
|
||||
// public float hunger = 100.0f; // 饥饿度,从0到100
|
||||
// public float maxHunger = 100.0f; // 最大饥饿度
|
||||
// public float searchRadius = 10.0f; // 搜索食物的半径
|
||||
|
||||
[Header("控制组件")]
|
||||
public Movement movement; // 运动控制器
|
||||
public Animator animator; // 动画控制器
|
||||
// [Header("控制组件")]
|
||||
// public Movement movement; // 运动控制器
|
||||
// public Animator animator; // 动画控制器
|
||||
|
||||
private float hungerTimer = 0.0f; // 计时器
|
||||
private float chasingCooldownTimer = 0.0f; // 追逐状态冷却计时器
|
||||
private const float chasingCooldown = 5.0f; // 追逐状态冷却时间
|
||||
// private float hungerTimer = 0.0f; // 计时器
|
||||
// private float chasingCooldownTimer = 0.0f; // 追逐状态冷却计时器
|
||||
// private const float chasingCooldown = 5.0f; // 追逐状态冷却时间
|
||||
|
||||
protected override void InitializeStates() {
|
||||
RegisterState("Idle", new AnimalIdleState(this));
|
||||
RegisterState("Roaming", new AnimalRoamingState(this));
|
||||
RegisterState("Chasing", new AnimalChasingState<AnimalFood>(this));
|
||||
RegisterState("Eating", new AnimalEatingState(this));
|
||||
// protected override void InitializeStates() {
|
||||
// RegisterState("Idle", new AnimalIdleState(this));
|
||||
// RegisterState("Roaming", new AnimalRoamingState(this));
|
||||
// RegisterState("Chasing", new AnimalChasingState<AnimalFood>(this));
|
||||
// RegisterState("Eating", new AnimalEatingState(this));
|
||||
|
||||
ChangeState("Idle");
|
||||
}
|
||||
// ChangeState("Idle");
|
||||
// }
|
||||
|
||||
protected override void Update() {
|
||||
base.Update();
|
||||
// protected override void Update() {
|
||||
// base.Update();
|
||||
|
||||
// 更新计时器
|
||||
hungerTimer += Time.deltaTime;
|
||||
chasingCooldownTimer += Time.deltaTime;
|
||||
// // 更新计时器
|
||||
// hungerTimer += Time.deltaTime;
|
||||
// chasingCooldownTimer += Time.deltaTime;
|
||||
|
||||
if (hungerTimer >= 1.0f) { ConsumeHunger(); }
|
||||
}
|
||||
// if (hungerTimer >= 1.0f) { ConsumeHunger(); }
|
||||
// }
|
||||
|
||||
public virtual void ConsumeHunger() {
|
||||
// 重置计时器
|
||||
hungerTimer = 0.0f;
|
||||
// 每次消耗1点饥饿度
|
||||
hunger -= 1.0f;
|
||||
if (hunger < 0.0f) { hunger = 0.0f; }
|
||||
// public virtual void ConsumeHunger() {
|
||||
// // 重置计时器
|
||||
// hungerTimer = 0.0f;
|
||||
// // 每次消耗1点饥饿度
|
||||
// hunger -= 1.0f;
|
||||
// if (hunger < 0.0f) { hunger = 0.0f; }
|
||||
|
||||
// 如果饥饿度低于最大饥饿度的70%,有50%的概率触发Chasing状态
|
||||
// 如果饥饿度低于最大饥饿度的30%,有90%的概率触发Chasing状态
|
||||
float foraging = hunger < maxHunger * 0.3f ? 0.9f : 0.5f;
|
||||
bool valid = hunger < maxHunger * 0.7f && Random.value < foraging;
|
||||
// // 如果饥饿度低于最大饥饿度的70%,有50%的概率触发Chasing状态
|
||||
// // 如果饥饿度低于最大饥饿度的30%,有90%的概率触发Chasing状态
|
||||
// float foraging = hunger < maxHunger * 0.3f ? 0.9f : 0.5f;
|
||||
// bool valid = hunger < maxHunger * 0.7f && Random.value < foraging;
|
||||
|
||||
// 如果触发Chasing状态,且冷却时间已过,切换到Chasing状态
|
||||
if (valid && chasingCooldownTimer >= chasingCooldown) {
|
||||
ChangeState("Chasing");
|
||||
chasingCooldownTimer = 0.0f; // 重置冷却计时器
|
||||
}
|
||||
}
|
||||
// // 如果触发Chasing状态,且冷却时间已过,切换到Chasing状态
|
||||
// if (valid && chasingCooldownTimer >= chasingCooldown) {
|
||||
// ChangeState("Chasing");
|
||||
// chasingCooldownTimer = 0.0f; // 重置冷却计时器
|
||||
// }
|
||||
// }
|
||||
|
||||
// 从指定范围内查找指定类型的组件
|
||||
public virtual bool Find<T>(out T value) where T : Component {
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, searchRadius);
|
||||
foreach (Collider collider in colliders) {
|
||||
T component = collider.GetComponent<T>();
|
||||
if (component != null) {
|
||||
value = component;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
// // 从指定范围内查找指定类型的组件
|
||||
// public virtual bool Find<T>(out T value) where T : Component {
|
||||
// Collider[] colliders = Physics.OverlapSphere(transform.position, searchRadius);
|
||||
// foreach (Collider collider in colliders) {
|
||||
// T component = collider.GetComponent<T>();
|
||||
// if (component != null) {
|
||||
// value = component;
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// value = null;
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public override bool UpdateMove(Vector3 position) {
|
||||
return movement.UpdateMove(position);
|
||||
}
|
||||
public override void AnimationTrigger(string value) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void AnimationEnd() {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
// // public override bool UpdateMove(Vector3 position) {
|
||||
// // return movement.UpdateMove(position);
|
||||
// // }
|
||||
// // public override void AnimationTrigger(string value) {
|
||||
// // throw new System.NotImplementedException();
|
||||
// // }
|
||||
// // public override void AnimationEnd() {
|
||||
// // throw new System.NotImplementedException();
|
||||
// // }
|
||||
// }
|
||||
|
||||
@@ -3,28 +3,28 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MuHua;
|
||||
|
||||
public class AnimalRoamingState : MachineState {
|
||||
public Vector3 targetPosition;
|
||||
public string ToDefault = "Idle";
|
||||
public readonly AnimalMachine animal;
|
||||
public AnimalRoamingState(Machine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
// public class AnimalRoamingState : IStateMachine {
|
||||
// public Vector3 targetPosition;
|
||||
// public string ToDefault = "Idle";
|
||||
// public readonly AnimalMachine animal;
|
||||
// public AnimalRoamingState(StateMachine machine) : base(machine) => animal = machine as AnimalMachine;
|
||||
|
||||
public override void Enter() {
|
||||
targetPosition = animal.movement.RandomTargetPosition();
|
||||
}
|
||||
// public override void Enter() {
|
||||
// targetPosition = animal.movement.RandomTargetPosition();
|
||||
// }
|
||||
|
||||
public override void Exit() {
|
||||
animal.movement.StopMoving();
|
||||
animal.animator.SetFloat("MoveSpeed", 0);
|
||||
}
|
||||
// public override void Exit() {
|
||||
// animal.movement.StopMoving();
|
||||
// animal.animator.SetFloat("MoveSpeed", 0);
|
||||
// }
|
||||
|
||||
public override void Trigger() {
|
||||
// public override void Trigger() {
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
public override void Update() {
|
||||
bool complete = animal.movement.UpdateMove(targetPosition);
|
||||
animal.animator.SetFloat("MoveSpeed", animal.movement.currentSpeed);
|
||||
if (complete) { animal.ChangeState(ToDefault); }
|
||||
}
|
||||
}
|
||||
// public override void Update() {
|
||||
// bool complete = animal.movement.UpdateMove(targetPosition);
|
||||
// animal.animator.SetFloat("MoveSpeed", animal.movement.currentSpeed);
|
||||
// if (complete) { animal.ChangeState(ToDefault); }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user