新增控制器系统

This commit is contained in:
Lenheart 2024-12-25 11:35:29 +08:00
parent db1fd3dfd4
commit 1b9696bd5d
4 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/*
文件名:InputClass.nut
路径:Core/InputClass/InputClass.nut
创建日期:2024-12-21 07:27
文件用途:输入类
*/
//输入逻辑函数Map
_Game_Input_Func_ <- {};
class _Input_ {
constructor() {
_Game_Input_Func_.rawset("SystemInput", SystemInput.bindenv(this));
// _Game_Logic_Func_.rawset("SystemInput", Proc.bindenv(this));
getroottable().Input <- this;
}
//系统对游戏的输入
function SystemInput(Code, Type) {
}
//Proc逻辑
function Proc(Dt, GameLister) {
}
}
//输入逻辑入口
function _Yosin_Game_Input_Logic_(Code, Type) {
foreach(Key, Func in _Game_Input_Func_) {
local Ret = Func(Code, Type);
//主体对象释放 删除自己
if (Ret == -1) {
_Game_Input_Func_.rawdelete(Key);
}
}
}

View File

@ -0,0 +1,80 @@
/*
文件名:Controller.nut
路径:User/Controller/Controller.nut
创建日期:2024-12-21 07:31
文件用途:控制器类
*/
class _GameController_ extends _Input_ {
//最新键码输入
KeyCode = null;
GameKeyCode = null;
GameKeyCodeCallback = null;
constructor() {
base.constructor();
GameKeyCodeCallback = {};
//建立键码表 按下flag全部为false
this.KeyCode = {};
foreach(value in getconsttable().KeyCode) {
this.KeyCode.rawset(value, false);
}
//建立游戏键码表
this.GameKeyCode = {};
foreach(Key, Value in getconsttable().CONTROLLER) {
this.GameKeyCode.rawset(Key, Value);
}
}
//系统对游戏的输入
function SystemInput(Code, Type) {
SetKeyCode(Code, Type);
}
//设定键码状态
function SetKeyCode(Code, Status) {
this.KeyCode[Code] = Status;
//只遍历有回调的键码
foreach(Code, Callback in GameKeyCodeCallback) {
//是否按下传递不同指令
if (GetGameKeyCode(Code)) {
Callback(true);
} else {
Callback(false);
}
}
}
//获取键码状态
function GetKeyCode(Code) {
return this.KeyCode[Code];
}
//获取游戏键码状态
function GetGameKeyCode(Code) {
if (!(GameKeyCode.rawin(Code))) return false;
return GetKeyCode(GameKeyCode[Code]);
}
//注册游戏键码回调函数
function RegisterGameKeyCode(Code, Callback) {
GameKeyCodeCallback[Code] <- Callback;
}
//Proc逻辑
function Proc(Dt, GameLister) {
// //只遍历有回调的键码
// foreach(Code, Callback in GameKeyCodeCallback) {
// //是否按下传递不同指令
// if (GetGameKeyCode(Code)) {
// Callback(true);
// } else {
// Callback(false);
// }
// }
}
}

View File

@ -0,0 +1,103 @@
/*
文件名:ObjectController.nut
路径:User/Controller/ObjectController.nut
创建日期:2024-12-21 14:16
文件用途:
*/
//输入指令集
class InputInstructionSpecial {
//指令表
InstructionArray = null;
//指令等待帧数
WaitFrame = null;
//动态数据
DynamicData = null;
constructor(gInstructionArray, gWaitFrame) {
this.InstructionArray = gInstructionArray;
this.WaitFrame = gWaitFrame;
DynamicData = {
CurrentIndex = 0,
Time = 0
};
}
//指令集更新
function OnUpdate(Dt) {
//比对值
local Match = false;
//获取最新的按键键值
}
}
class Object_Controller {
//控制对象
Parent = null;
//控制器是否启用
Enabled = true;
constructor(gParent) {
this.Parent = gParent.weakref();
Move_Instruction_Horizontal = [];
Move_Instruction_Vertical = [];
}
//启用控制器
function Enable() {
this.Enabled = true;
}
//关闭控制器
function Disable() {
this.Enabled = false;
}
//横向移动方向List
Move_Instruction_Horizontal = null;
//纵向移动方向List
Move_Instruction_Vertical = null;
//检查按键与移动方向
function CheckMove(Key, Flag, Buffer) {
//上 0 下 1 左 2 右 3
local Move_Index = Buffer.find(Flag);
if (Input.GetGameKeyCode(Key)) {
if (Move_Index == null) Buffer.push(Flag);
} else {
if (Move_Index != null) Buffer.remove(Move_Index);
}
}
//方向移动控制
function MoveControl(Dt) {
//上 0 下 1 左 2 右 3
CheckMove("OPTION_HOTKEY_MOVE_UP", 0, Move_Instruction_Vertical);
CheckMove("OPTION_HOTKEY_MOVE_DOWN", 1, Move_Instruction_Vertical);
CheckMove("OPTION_HOTKEY_MOVE_LEFT", 2, Move_Instruction_Horizontal);
CheckMove("OPTION_HOTKEY_MOVE_RIGHT", 3, Move_Instruction_Horizontal);
//获取横向移动方向
local Move_Horizontal = Move_Instruction_Horizontal.len() ? Move_Instruction_Horizontal.top() : null;
//获取纵向移动方向
local Move_Vertical = Move_Instruction_Vertical.len() ? Move_Instruction_Vertical.top() : null;
local X = 0, Y = 0;
if (Move_Horizontal == 2) X = -1;
else if (Move_Horizontal == 3) X = 1;
if (Move_Vertical == 0) Y = -1;
else if (Move_Vertical == 1) Y = 1;
//至少有一个方向才会调用
Parent.Move(Dt, X, Y);
}
//控制器更新
function OnUpdate(Dt) {
if (!this.Enabled) return;
//方向移动控制
MoveControl(Dt);
}
}

View File

@ -21,6 +21,8 @@ function InitGame() {
_FontAssetManager_(); _FontAssetManager_();
//初始化鼠标 //初始化鼠标
_IMouse_(); _IMouse_();
//初始化控制器
_GameController_();
//预加载 //预加载
Animation("ui/charactercreate/dust.ani"); Animation("ui/charactercreate/dust.ani");