From 54854c0915946834d136f30bd228ddb4d0a7d5e7 Mon Sep 17 00:00:00 2001 From: Lenheart <947330670@qq.com> Date: Fri, 10 Oct 2025 12:27:21 +0800 Subject: [PATCH] DNF_DEV_SQR --- Game/CharacterScript/Common/Move.nut | 52 +++++++++++++++++ Game/CharacterScript/Common/Rest.nut | 22 +++++++ Game/ObjectClass/ActiveObject.nut | 25 ++++++++ Game/ObjectClass/BaseObject.nut | 32 +++++++++++ Game/ObjectClass/CharacterObject.nut | 23 ++++++++ Game/StateMachine/StateMachine.nut | 85 ++++++++++++++++++++++++++++ SquirrelFileConfig.cfg | 16 ++++++ UI/asd.nut | 0 enum.nut | 27 +++++++++ folder-alias.json | 32 +++++++++++ main.nut | 6 ++ 11 files changed, 320 insertions(+) create mode 100644 Game/CharacterScript/Common/Move.nut create mode 100644 Game/CharacterScript/Common/Rest.nut create mode 100644 Game/ObjectClass/ActiveObject.nut create mode 100644 Game/ObjectClass/BaseObject.nut create mode 100644 Game/ObjectClass/CharacterObject.nut create mode 100644 Game/StateMachine/StateMachine.nut create mode 100644 SquirrelFileConfig.cfg create mode 100644 UI/asd.nut create mode 100644 enum.nut create mode 100644 folder-alias.json create mode 100644 main.nut diff --git a/Game/CharacterScript/Common/Move.nut b/Game/CharacterScript/Common/Move.nut new file mode 100644 index 0000000..134ba22 --- /dev/null +++ b/Game/CharacterScript/Common/Move.nut @@ -0,0 +1,52 @@ +/* +文件名:Move.nut +路径:Game/CharacterScript/Common/Move.nut +创建日期:2025-10-03 02:02 +文件用途:角色移动脚本 +*/ + +function checkCanChangeState_Character_Move(obj) { + //获取当前状态 + local CurState = obj.GetState(); + //得到摇杆的数据 X 和 Y的范围 -1.0 ~ 1.0 + local arr = obj.GetVars("_move_data_", "float"); + if (fabs(arr[0]) > 0.35 || fabs(arr[1]) > 0.35) { + return true; + } else return false; +} + +function SetState_Character_Move(obj) { + //获取移动数据 + local arr = obj.GetVars("_move_data_", "float"); + + //设置人物朝向 摇杆X轴大于0则朝右,小于0则朝左 + local SetValue = arr[0] > 0 ? 0 : 1; + if (SetValue != obj.GetDirection()) { + if (fabs(arr[0]) > 0.35) obj.SetDirection(SetValue); + //设置动作 + obj.SetAction("move"); + } else { + if (obj.GetState() != 1) obj.SetAction("move"); + } + + local Speed = 250; + local Dir = obj.GetDirection(); + local Pos = obj.GetPosition(); + local XOffset = null; + if (fabs(arr[0]) > 0.35) XOffset = ((arr[0] > 0 ? Speed : -Speed)); + else XOffset = 0; + local YOffset = null; + //当摇杆Y轴大于0.35或小于-0.35时,设置Y轴偏移量 + if (fabs(arr[1]) > 0.35) YOffset = (arr[1] > 0 ? Speed : -Speed); + else YOffset = 0; + + obj.SetSpeed(XOffset, YOffset, null); +} + +function ProcState_Character_Move(obj, DeltaTime) { + //获取移动数据 + local arr = obj.GetVars("_move_data_", "float"); + if (arr[0] == 0 && arr[1] == 0) { + obj.SetState(0); + } +} \ No newline at end of file diff --git a/Game/CharacterScript/Common/Rest.nut b/Game/CharacterScript/Common/Rest.nut new file mode 100644 index 0000000..e1d5be2 --- /dev/null +++ b/Game/CharacterScript/Common/Rest.nut @@ -0,0 +1,22 @@ +/* +文件名:Rest.nut +路径:Game/CharacterScript/Common/Rest.nut +创建日期:2025-10-03 02:02 +文件用途:角色站立状态 +*/ + +function checkCanChangeState_Character_Rest(obj) { + + return true; +} + +function SetState_Character_Rest(obj) { + //设置动作 + obj.SetAction("rest"); + //设置速度 + obj.SetSpeed(0, 0, null); +} + +function ProcState_Character_Rest(obj, DeltaTime) { + +} \ No newline at end of file diff --git a/Game/ObjectClass/ActiveObject.nut b/Game/ObjectClass/ActiveObject.nut new file mode 100644 index 0000000..3ae04cd --- /dev/null +++ b/Game/ObjectClass/ActiveObject.nut @@ -0,0 +1,25 @@ +/* +文件名:ActiveObject.nut +路径:Game/ObjectClass/ActiveObject.nut +创建日期:2025-10-03 02:50 +文件用途: +*/ + +class ActiveObject extends BaseObject { + + constructor(Object) { + base.constructor(Object); + } + + function SetSpeed(x, y, z) { + local CurSpeed = sq_GetSpeed(C_Object); + if (x == null) x = CurSpeed.x; + if (y == null) y = CurSpeed.y; + if (z == null) z = CurSpeed.z; + sq_SetSpeed(C_Object, x, y, z); + } + + function GetSpeed() { + return sq_GetSpeed(C_Object); + } +} \ No newline at end of file diff --git a/Game/ObjectClass/BaseObject.nut b/Game/ObjectClass/BaseObject.nut new file mode 100644 index 0000000..e339537 --- /dev/null +++ b/Game/ObjectClass/BaseObject.nut @@ -0,0 +1,32 @@ +class BaseObject { + //c++对象指针 + C_Object = null; + + constructor(Object) { + C_Object = Object; + } + + function GetPosition() { + return sq_GetPosition(C_Object); + } + + function SetPosition(x, y, z) { + sq_SetPosition(C_Object, x, y, z); + } + + function GetVars(Name, Type) { + return sq_GetVars(C_Object, Name, Type); + } + + function SetVars(Name, Type, Value) { + sq_SetVars(C_Object, Name, Type, Value); + } + + function GetDirection() { + return sq_GetDirection(C_Object); + } + + function SetDirection(Dir) { + sq_SetDirection(C_Object, Dir); + } +} \ No newline at end of file diff --git a/Game/ObjectClass/CharacterObject.nut b/Game/ObjectClass/CharacterObject.nut new file mode 100644 index 0000000..1c3a68b --- /dev/null +++ b/Game/ObjectClass/CharacterObject.nut @@ -0,0 +1,23 @@ +/* +文件名:CharacterObject.nut +路径:Game/ObjectClass/CharacterObject.nut +创建日期:2025-10-03 02:50 +文件用途: +*/ +class CharacterObject extends ActiveObject { + constructor(Object) { + base.constructor(Object); + } + + function GetState() { + return sq_GetState(C_Object); + } + + function SetState(State) { + sq_SetState(C_Object, State); + } + + function SetAction(key) { + sq_SetAction(C_Object, key); + } +} \ No newline at end of file diff --git a/Game/StateMachine/StateMachine.nut b/Game/StateMachine/StateMachine.nut new file mode 100644 index 0000000..f41ef81 --- /dev/null +++ b/Game/StateMachine/StateMachine.nut @@ -0,0 +1,85 @@ +/* +文件名:StateMachine.nut +路径:Game/StateMachine/StateMachine.nut +创建日期:2025-10-03 02:11 +文件用途: +*/ + +class StateMachine { + //状态注册表 + StatusRegistry = null; + + constructor() { + if (getroottable().rawin("__StateMachine__")) { + // 防止重复实例化 + throw "StateMachine 不能重复实例化!"; + } + //初始化状态注册表 + StatusRegistry = {}; + foreach(JobIndex in getconsttable().CHARACTERJOB) { + StatusRegistry[JobIndex] <- {}; + } + } + + // 静态方法:获取唯一实例 + function GetInstance() { + if (!getroottable().rawin("__StateMachine__")) { + // 首次调用时创建实例 + getroottable()["__StateMachine__"] <- StateMachine(); + } + return getroottable()["__StateMachine__"]; + } + + //注册状态 + function RegisterState(Job, StateName, StateIndex) { + StatusRegistry[Job][StateIndex] <- { + StateName = StateName, + StateIndex = StateIndex + } + print("注册了状态" + StateName + ",状态索引为" + StateIndex); + } + + //获取状态信息 + function GetStateInfo(Job, State) { + return StatusRegistry[Job][State]; + } +} + +function StateMachine_checkCanChangeState(C_Object, Job, State) { + //获取状态信息 + local StateInfo = StateMachine.GetInstance().GetStateInfo(Job, State); + //判断是否有 检查能否切换状态函数 + local FuncName = "checkCanChangeState_" + StateInfo.StateName; + if (getroottable().rawin(FuncName)) { + return getroottable()[FuncName](CharacterObject(C_Object)); + } else { + error("没有找到" + FuncName + "函数"); + } + return false; +} + +function StateMachine_SetState(C_Object, Job, State) { + //获取状态信息 + local StateInfo = StateMachine.GetInstance().GetStateInfo(Job, State); + //判断是否有 设置状态函数 + local FuncName = "SetState_" + StateInfo.StateName; + if (getroottable().rawin(FuncName)) { + getroottable()[FuncName](CharacterObject(C_Object)); + } else { + error("没有找到" + FuncName + "函数"); + } + return false; +} + +function StateMachine_ProcState(C_Object, Job, State, DeltaTime) { + //获取状态信息 + local StateInfo = StateMachine.GetInstance().GetStateInfo(Job, State); + //判断是否有 设置状态函数 + local FuncName = "ProcState_" + StateInfo.StateName; + if (getroottable().rawin(FuncName)) { + getroottable()[FuncName](CharacterObject(C_Object), DeltaTime); + } else { + error("没有找到" + FuncName + "函数"); + } + return false; +} \ No newline at end of file diff --git a/SquirrelFileConfig.cfg b/SquirrelFileConfig.cfg new file mode 100644 index 0000000..61d795e --- /dev/null +++ b/SquirrelFileConfig.cfg @@ -0,0 +1,16 @@ +enum.nut + + +Game/StateMachine/StateMachine.nut + +Game/ObjectClass/BaseObject.nut +Game/ObjectClass/ActiveObject.nut +Game/ObjectClass/CharacterObject.nut + + +Game/CharacterScript/Common/Rest.nut +Game/CharacterScript/Common/Move.nut + + +UI/asd.nut +main.nut \ No newline at end of file diff --git a/UI/asd.nut b/UI/asd.nut new file mode 100644 index 0000000..e69de29 diff --git a/enum.nut b/enum.nut new file mode 100644 index 0000000..4f6b5c7 --- /dev/null +++ b/enum.nut @@ -0,0 +1,27 @@ +/* +文件名:enum.nut +路径:enum.nut +创建日期:2025-10-03 02:41 +文件用途: +*/ + +enum CHARACTERJOB { + SWORDMAN // 男鬼剑士 + FIGHTER // 女格斗家 + GUNNER // 男神枪手 + MAGE // 女魔法师 + PRIEST // 男圣职者 + AT_GUNNER // 女神枪手 + THIEF // 暗夜使者 + AT_FIGHTER // 男格斗家 + AT_MAGE // 男魔法师 + DEMONIC_SWORDMAN // 黑暗武士 + CREATOR_MAGE // 缔造者 + AT_SWORDMAN // 女鬼剑士 + KNIGHT // 守护者 + DEMONIC_LANCER // 魔枪士 + AT_PRIEST // 女圣职者 + GUN_BLADER // 枪剑士 + ARCHER // 弓箭手 + MAX // 无 +}; \ No newline at end of file diff --git a/folder-alias.json b/folder-alias.json new file mode 100644 index 0000000..a8f24bf --- /dev/null +++ b/folder-alias.json @@ -0,0 +1,32 @@ +{ + "Game": { + "description": "游戏" + }, + "UI": { + "description": "界面" + }, + "Game/ObjectClass": { + "description": "对象类" + }, + "Game/CharacterScript": { + "description": "角色类脚本" + }, + "Game/CharacterScript/Common": { + "description": "通用" + }, + "Game/StateMachine": { + "description": "状态机" + }, + "enum.nut": { + "description": "枚举表" + }, + "Game/ObjectClass/BaseObject.nut": { + "description": "基础对象" + }, + "Game/ObjectClass/ActiveObject.nut": { + "description": "动态对象" + }, + "Game/ObjectClass/CharacterObject.nut": { + "description": "角色对象" + } +} \ No newline at end of file diff --git a/main.nut b/main.nut new file mode 100644 index 0000000..d3a8ebc --- /dev/null +++ b/main.nut @@ -0,0 +1,6 @@ +function main() { + local SM = StateMachine.GetInstance(); + + SM.RegisterState(CHARACTERJOB.SWORDMAN, "Character_Rest", 0); + SM.RegisterState(CHARACTERJOB.SWORDMAN, "Character_Move", 1); +} \ No newline at end of file