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();
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 91660569267b53545ab6e688c384eae2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,41 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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) {
if (!states.ContainsKey(stateType)) { states.Add(stateType, state); }
}
public virtual void ChangeState(string stateType) {
if (states.ContainsKey(stateType)) {
currentState?.Exit();
currentState = states[stateType];
currentState.Enter();
}
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,11 @@
fileFormatVersion: 2
guid: 0cd74b3acde213f489a11a9e0ccd81e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -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,11 @@
fileFormatVersion: 2
guid: 18d0a64357fbec04d8fbd51d45c88e4b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -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:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7cd21ba4756d406468a37fe882d097ae
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 EffectsLaunch : MonoBehaviour {
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 36eaafdfeb2c4f44f82504e36afe1cda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a359f8431aa19b846b623d9dd066bae3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -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"
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f71c71a343ef5c8459a9f7773cc07460
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -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: