Files
DNF_DEV_SQR/Game/StateMachine/StateMachine.nut
2025-10-18 21:39:57 +08:00

107 lines
2.6 KiB
Plaintext

/*
文件名:StateMachine.nut
路径:Game/StateMachine/StateMachine.nut
创建日期:2025-10-03 02:11
文件用途:
*/
/** 状态机
* @global
*/
class StateMachine {
//状态注册表
StatusRegistry = null;
constructor() {
if (getroottable().rawin("__StateMachine__")) {
// 防止重复实例化
throw "StateMachine 不能重复实例化!";
}
//初始化状态注册表
StatusRegistry = {};
foreach (JobIndex in getconsttable().CHARACTERJOB) {
StatusRegistry[JobIndex] <- {};
}
}
/**
* 静态方法:获取唯一实例
* @function
* @returns {StateMachine}
*/
function GetInstance() {
if (!getroottable().rawin("__StateMachine__")) {
// 首次调用时创建实例
getroottable()["__StateMachine__"] <- StateMachine();
}
return getroottable()["__StateMachine__"];
}
/**
* 注册状态
* @function
* @param {integer} Job
* @param {any} StateName
* @param {integer} StateIndex
* @returns {void}
*/
function RegisterState(Job, StateName, StateIndex) {
StatusRegistry[Job][StateIndex] <- {
StateName = StateName,
StateIndex = StateIndex
};
print("注册了状态" + StateName + ",状态索引为" + StateIndex);
}
/**
* 获取状态信息
* @function
* @param {integer} Job
* @param {integer} State
* @returns {integer}
*/
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;
}