当前位置: 首页 > news >正文

lua 游戏架构 之 游戏 AI (九)ai_mgr Ai管理

定义`ai_mgr`的类,用于管理游戏中实体的AI组件。

先定义 AI行为枚举和优先级: 

lua 游戏架构 之 游戏 AI (八)ai_tbl 行为和优先级-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/heyuchang666/article/details/140712839?spm=1001.2014.3001.5501lua 游戏架构 之 游戏 AI (一)ai_base-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/heyuchang666/article/details/140624481?spm=1001.2014.3001.5501

以下是对代码的具体解释:

1. **引入依赖**:  使用`require`函数引入全局定义和AI定义。

2. **关闭JIT编译**: 如果启用了JIT编译,则关闭它,以确保AI逻辑的一致性。

3. **定义`ai_mgr`类**: `ai_mgr`类用于管理实体的AI组件。

4. **构造函数 (`ctor`)**:

  •    - 构造函数接受一个`entity`参数,并初始化实体、子组件列表、子组件索引和当前激活的组件。
  •    - 调用`AddComponent`方法添加默认的AI组件(基础AI)。

5. **`AddComponent`方法**:

  •    - 根据传入的AI类型(`atype`),从`ai_tbl`映射表中获取对应的脚本信息。
  •    - 动态加载对应的脚本,并创建组件实例。
  •    - 将新创建的组件添加到子组件列表中,并调用其`OnAttach`方法。

6. **`RmvComponent`方法**:移除指定类型的AI组件,并调用其`OnDetach`方法。

7. **`GetActiveComp`方法**:返回当前激活的AI组件。

8. **`BuildIdx`方法**: 构建子组件索引列表,并按优先级排序。

9. **`SwitchComp`方法**:

  • 如果当前激活的组件(self._activeComp)不是新切换的组件,则先检查当前激活的组件是否开启(IsTurnOn)。如果是,则调用其OnLeave方法,使其离开当前状态。
  • 如果新切换的组件未开启,则调用其OnEnter方法,使其进入激活状态。
  • 更新当前激活的组件为新切换的组件(self._activeComp = comp)。

10. **`OnUpdate`方法**:
    - 调用当前激活的AI组件的`OnUpdate`方法,更新AI状态。

11. **`OnLogic`方法**:
    - 调用当前激活的AI组件的`OnLogic`方法,执行逻辑更新。如果逻辑更新失败,则将组件从激活状态移除。

12. **`OnStopAction`方法**:
    - 调用当前激活的AI组件的`OnStopAction`方法,停止当前动作。

13. **`OnAttackAction`方法**:
    - 调用当前激活的AI组件的`OnAttackAction`方法,处理攻击动作。

14. **`create_mgr`函数**:
    - 创建并返回一个新的`ai_mgr`实例。

代码逻辑流程:

  • - **初始化**:在实体创建时,通过`ai_mgr`的构造函数初始化AI管理器,添加默认的AI组件。
  • - **添加组件**:通过`AddComponent`方法添加新的AI组件。
  • - **移除组件**:通过`RmvComponent`方法移除不需要的AI组件。
  • - **切换组件**:在游戏运行时,通过`SwitchComp`方法根据当前情况切换到合适的AI组件。
  • - **更新和逻辑处理**:在每帧更新和逻辑处理时,调用当前激活的AI组件的相关方法。

关键点:

  • - **动态加载脚本**:通过`require`函数动态加载AI组件脚本。
  • - **组件管理**:通过`ai_tbl`映射表管理不同AI组件的脚本和优先级。
  • - **组件切换**:根据游戏逻辑和实体状态,动态切换AI组件,以实现不同的AI行为。

整体而言,这段代码为游戏中的AI组件提供了一个灵活的管理框架,根据不同的游戏场景和实体状态动态地切换和控制AI行为。
 

local require = requirerequire("global");
require("logic/entity/ai/ai_def");------------------------------------------------------
if jit thenjit.off(true, true)
end------------------------------------------------------
ai_mgr = class("ai_mgr");
function ai_mgr:ctor(entity)self._entity	= entity;self._childs	= { };self._child_idx	= { };self._activeComp= nil;-- add default ai componentself:AddComponent(eAType_BASE);-- get default ai componentself:SwitchComp();
endfunction ai_mgr:AddComponent(atype)local ai = ai_tbl[atype];if ai thenlocal comp = require("logic/entity/ai/" .. ai.script);if comp thenlocal c = comp.create_component(self._entity, ai.priority);if c thenc:SetName(ai.script);c:OnAttach();endself._childs[atype] = c;self:BuildIdx();endend
endfunction ai_mgr:RmvComponent(atype)local c = self._childs[atype];if c thenc:OnDetach();endself._childs[atype] = nil;self:BuildIdx();
endfunction ai_mgr:GetActiveComp()return self._activeComp;
endfunction ai_mgr:BuildIdx()self._child_idx = { };for k ,_ in pairs(self._childs) dotable.insert(self._child_idx, k);endlocal _cmp = function(d1, d2)if d1 > d2 thenreturn true;endreturn false;endtable.sort(self._child_idx, _cmp);
endfunction ai_mgr:SwitchComp()if jit thenjit.off(true, true)endif self._entity and self._entity:IsPlayer() and g_game_context:IsInPingMode() thenreturn false;endfor k, v in ipairs(self._child_idx) dolocal comp = self._childs[v];if self._entity:IsRenderable() or (comp._priority == eAI_Priority_High) thenif comp:Switch() thenif self._activeComp ~= comp thenif self._activeComp and self._activeComp:IsTurnOn() thenself._activeComp:OnLeave();endif not comp:IsTurnOn() then--if self._entity:GetEntityType()==eET_Mercenary then--log("entity enter ai " .. comp:GetName());--	endcomp:OnEnter();endself._activeComp = comp;endreturn true;endendendreturn false;
endfunction ai_mgr:OnUpdate(dTime)if self._activeComp thenif self._entity and self._entity:IsPlayer() and g_game_context:IsInPingMode() thenreturn ;endself._activeComp:OnUpdate(dTime);end
endfunction ai_mgr:OnLogic(dTick)if self._activeComp thenif self._entity and self._entity:IsPlayer() and g_game_context:IsInPingMode() thenreturn ;endif not self._activeComp:OnLogic(dTick) thenself._activeComp:OnLeave();self._activeComp = nil;return false;endendreturn true;
endfunction ai_mgr:OnStopAction(action)if self._activeComp thenself._activeComp:OnStopAction(action);end
endfunction ai_mgr:OnAttackAction(id)if self._activeComp thenself._activeComp:OnAttackAction(id);end
endfunction create_mgr(entity)return ai_mgr.new(entity);
end

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Matlab M_map工具箱绘制Interrupted Mollweide Projection
  • 【React 】开发环境搭建详细指南
  • Java中的集合相关知识汇总
  • 【Go】探索 Go 语言的内建函数 copy
  • nacos2.x作为配置中心和服务注册和发现以及springcloud使用
  • linux离线安装mysql8(单机版)
  • 信息安全工程师题
  • K8s 核心组件——API Server
  • Python 代码中的 yield 到底是什么鬼?
  • 【C#】Func、Action和Predicate
  • 操作系统:进程1
  • liteos定时器回调时间过长造成死机问题解决思路
  • 拓扑排序与有向无环图 -- Kahn算法和深度优先搜索
  • Redis - SpringDataRedis - RedisTemplate
  • QT Creator下载安装详细教程(保姆级教程)
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【347天】每日项目总结系列085(2018.01.18)
  • 2019年如何成为全栈工程师?
  • HTTP--网络协议分层,http历史(二)
  • Java 23种设计模式 之单例模式 7种实现方式
  • leetcode388. Longest Absolute File Path
  • Lucene解析 - 基本概念
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • QQ浏览器x5内核的兼容性问题
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 前端设计模式
  • 使用Gradle第一次构建Java程序
  • 网页视频流m3u8/ts视频下载
  • !!java web学习笔记(一到五)
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #数据结构 笔记一
  • (+4)2.2UML建模图
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (SpringBoot)第七章:SpringBoot日志文件
  • (ZT)薛涌:谈贫说富
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (十六)一篇文章学会Java的常用API
  • (算法)Game
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Core 项目指定SDK版本
  • .NET 中让 Task 支持带超时的异步等待
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • [ 渗透工具篇 ] 一篇文章让你掌握神奇的shuize -- 信息收集自动化工具
  • [2023年]-hadoop面试真题(一)
  • [3D基础]理解计算机3D图形学中的坐标系变换
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [Big Data - Kafka] kafka学习笔记:知识点整理
  • [BZOJ1010] [HNOI2008] 玩具装箱toy (斜率优化)
  • [C++] vector对比list deque的引出
  • [C++]——带你学习类和对象
  • [EFI]Dell Inspiron 15 5567 电脑 Hackintosh 黑苹果efi引导文件
  • [Electron] 将应用打包成供Ubuntu、Debian平台下安装的deb包
  • [GXYCTF2019]BabyUpload1 -- 题目分析与详解