This commit is contained in:
MuHua-123
2025-03-07 22:46:13 +08:00
parent 5e819d5257
commit 57a8a6e9a1
83 changed files with 235 additions and 977 deletions
@@ -2,17 +2,18 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
namespace MuHua {
public abstract class Character : MonoBehaviour {
// Update is called once per frame
void Update()
{
public Movement movement; // 运动控制器
public Animator animator; // 动画控制器
/// <summary> 更新移动 </summary>
public abstract bool UpdateMove(Vector3 position);
/// <summary> 动画触发 </summary>
public abstract void AnimationTrigger(string value);
/// <summary> 动画结束 </summary>
public abstract void AnimationEnd();
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: abc53960d2470184c807cea21744653e
guid: 91660569267b53545ab6e688c384eae2
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -2,36 +2,40 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
public abstract class Machine : MonoBehaviour
{
namespace MuHua {
/// <summary>
/// 有限状态机
/// </summary>
public abstract class Machine : Character {
protected MachineState currentState;
protected Dictionary<string, MachineState> states = new Dictionary<string, MachineState>();
protected virtual void Start() => InitializeStates();
protected virtual void Update() => currentState?.Update();
#region
protected abstract void InitializeStates();
protected virtual void RegisterState(string stateType, MachineState state)
{
protected virtual void RegisterState(string stateType, MachineState state) {
if (!states.ContainsKey(stateType)) { states.Add(stateType, state); }
}
public virtual void ChangeState(string stateType)
{
if (states.ContainsKey(stateType))
{
public virtual void ChangeState(string stateType) {
if (states.ContainsKey(stateType)) {
currentState?.Exit();
currentState = states[stateType];
currentState.Enter();
}
else
{
else {
Debug.LogWarning($"State {stateType} is not registered.");
}
}
#endregion
public override bool UpdateMove(Vector3 position) {
return movement.UpdateMove(position);
}
public override void AnimationTrigger(string value) {
throw new System.NotImplementedException();
}
public override void AnimationEnd() => currentState?.Trigger();
}
}
}
@@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 状态接口
/// </summary>
public abstract class MachineState {
protected readonly Machine machine;
public MachineState(Machine machine) => this.machine = machine;
/// <summary> 进入状态 </summary>
public abstract void Enter();
/// <summary> 更新状态 </summary>
public abstract void Update();
/// <summary> 退出状态 </summary>
public abstract void Exit();
/// <summary> 触发状态 </summary>
public abstract void Trigger();
}
}
@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class PlayerController : Character {
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();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73bafdf07b08fc246ab173a5e555629e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3e92c6f04caccae40bb8e24b5307132d
guid: 7cd21ba4756d406468a37fe882d097ae
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class EffectsLaunch : MonoBehaviour {
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 36eaafdfeb2c4f44f82504e36afe1cda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,6 @@
fileFormatVersion: 2
guid: f76a1c2d2c424dd4a9bbb9ddf5f95eb9
guid: a359f8431aa19b846b623d9dd066bae3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 运动控制器
/// </summary>
public abstract class Movement : MonoBehaviour {
public float moveSpeed = 5.0f; // 最大移动速度
public float acceleration = 20.0f; // 加速度
public float currentSpeed = 0.0f; // 当前速度
public Vector3 front; // 面向
public abstract bool UpdateMove(Vector3 position);
public abstract Vector3 RandomTargetPosition();
public abstract void StopMoving();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d034bee659058394d8641aad1f9e7023
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class MovementNavigation : MonoBehaviour {
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9eb06e286a5289a439f220c0a3656568
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 标准运动实现
/// </summary>
public class MovementStandard : Movement {
public override bool UpdateMove(Vector3 position) {
// 计算相对于世界坐标系的移动方向
Vector3 moveDirection = (position - transform.position).normalized;
float distance = Vector3.Distance(transform.position, position);
// 平滑加速和减速
currentSpeed = distance > 0.2f
? Mathf.MoveTowards(currentSpeed, moveSpeed, acceleration * Time.deltaTime)
: Mathf.MoveTowards(currentSpeed, 0, acceleration * Time.deltaTime);
// 移动玩家
transform.Translate(moveDirection * currentSpeed * Time.deltaTime, Space.World);
// 如果有移动输入,则更新玩家的朝向
if (distance != 0) {
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, moveSpeed * Time.deltaTime * 100);
}
// 计算转向向量
Vector3 localMoveDirection = transform.InverseTransformDirection(moveDirection * currentSpeed);
localMoveDirection = localMoveDirection.normalized;
// 对localMoveDirection的x和z进行分类处理
float moveX = Convert.ToInt32(localMoveDirection.x);
float moveZ = Convert.ToInt32(localMoveDirection.z);
front = new Vector3(moveX, 0, moveZ);
// 如果到达目标位置,返回 true
distance = Vector3.Distance(transform.position, position);
return distance < 0.05f;
}
public override Vector3 RandomTargetPosition() {
float randomX = UnityEngine.Random.Range(-10.0f, 10.0f);
float randomZ = UnityEngine.Random.Range(-10.0f, 10.0f);
return transform.position + new Vector3(randomX, 0, randomZ);
}
public override void StopMoving() {
currentSpeed = 0;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 699b617ae51a79a4390529e4e03405a2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
{
"name": "MuHua.Character"
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 856a56044e2bfce4dbc2d22bfb5a926f
guid: f71c71a343ef5c8459a9f7773cc07460
AssemblyDefinitionImporter:
externalObjects: {}
userData:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1c84ca502025179419c2b8afb29080aa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class VisualField : MonoBehaviour {
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e4d36e9e525a8a7429e5a7d9edf17cb2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class VisualFieldStandard : VisualField {
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3595d54d26ddd5c47b4d4a3025935ecb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-17
View File
@@ -1,17 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua
{
public abstract class MachineState
{
protected Machine machine;
public MachineState(Machine machine) => this.machine = machine;
public abstract void Enter();
public abstract void Update();
public abstract void Exit();
}
}
-14
View File
@@ -1,14 +0,0 @@
{
"name": "MuHua.FSM",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
-18
View File
@@ -1,18 +0,0 @@
{
"name": "muhua-fsm",
"version": "1.0.0",
"displayName": "MuHua FSM",
"description": "FiniteStateMachine\u6709\u9650\u72b6\u6001\u673a\u6846\u67b6",
"author": {
"name": "MuHua",
"email": "muhua233@qq.com"
},
"type": "tool",
"samples": [
{
"displayName": "示例",
"description": "",
"path": "Samples"
}
]
}
-7
View File
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: ab0f2027431743d43afe689084dcd33c
PackageManifestImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 004a039923c4e3845b77cdbaae4fae09
guid: 1a2850bc9ac8725479e46f5d43a2304c
DefaultImporter:
externalObjects: {}
userData:
-6
View File
@@ -221,12 +221,6 @@
"source": "embedded",
"dependencies": {}
},
"muhua-fsm": {
"version": "file:FSM",
"depth": 0,
"source": "embedded",
"dependencies": {}
},
"muhua-label-follow": {
"version": "file:LabelFollow",
"depth": 0,