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

lua 游戏架构 之 游戏 AI (三)ai_attack

这段Lua脚本定义了一个名为 `ai_attack` 的类,继承自 `ai_base` 类。

lua 游戏架构 之 游戏 AI (一)ai_base-CSDN博客文章浏览阅读119次。定义了一套接口和属性,可以基于这个基础类派生出具有特定行为的AI组件。例如,可以创建追逐敌人的AI、巡逻的AI或使用特定策略的AI等,都继承自这个基础类https://blog.csdn.net/heyuchang666/article/details/140624481?spm=1001.2014.3001.5501

这个类用于处理游戏中AI的攻击逻辑。以下是对代码的详细解释:

1. **引入基类**:
   - `local BASE = require("logic/entity/ai/ai_base").ai_base;` 这行代码引入了基类 `ai_base`。

2. **定义 `ai_attack` 类**:
   - `ai_attack = class("ai_attack", BASE);` 这行代码定义了 `ai_attack` 类并指定其基类为 `BASE`。

3. **构造函数 (`ctor`)**:
   - `function ai_attack:ctor(entity)` 构造函数接受一个 `entity` 参数,并设置 `_type` 属性为 `eAType_ATTACK`。

4. **`IsValid` 方法**:
   - 这个方法用于验证AI是否可以执行攻击。它检查实体是否死亡、是否可以攻击、是否是特定类型的实体等条件。

5. **`CanAttackNoneTarget` 方法**:
   - 这个方法用于判断是否可以攻击没有目标的情况,当前实现返回 `false`。

6. **`OnEnter` 方法**:
   - 当AI组件进入激活状态时执行。该方法处理目标定位、实体朝向调整、攻击开始逻辑等。
   - 如果实体有目标并且速度大于0,则计算目标和实体之间的旋转角度,并设置实体面向目标。

7. **`OnLeave` 方法**:
   - 当AI组件离开激活状态时执行。该方法处理停止攻击的逻辑,并恢复实体的移动能力。

8. **`OnUpdate` 方法**:
   - 每帧调用,用于更新AI状态。如果基类的 `OnUpdate` 方法返回 `false`,则当前方法也返回 `false`。

9. **`OnLogic` 方法**:
   - 逻辑更新方法,用于处理技能序列、检查攻击持续时间等。
   - 如果技能序列有效且是当前技能的子技能,则执行相关逻辑。
   - 检查自攻击开始以来的时间是否已经超过技能的持续时间,如果是,则返回 `false`。

10. **创建组件函数**:
    - `function create_component(entity, priority)` 这个函数用于创建 `ai_attack` 类的新实例,传入一个实体和一个优先级。

代码中的一些关键函数和方法:
- `IsDead()`:检查实体是否死亡。
- `CanAttack()`:检查实体是否可以攻击。
- `GetEntityType()`:获取实体的类型。
- `GetMapEnter()`:获取地图进入的状态。
- `Test(eEBAttack)`:测试实体的行为是否包含攻击行为。
- `CanUse()`:检查技能是否可以使用。
- `IsPlayer()`:检查目标是否是玩家。
- `GetRadius()`:获取实体的半径。
- `vec3_sub1()`:计算两个向量的差。
- `vec3_len()`:计算向量的长度。
- `vec3_angle1()`:计算两个向量之间的夹角。
- `SetFaceDir()`:设置实体的面向方向。
- `StartAttack()`:开始攻击。
- `StopAttack()`:停止攻击。
- `FinishAttack()`:完成攻击。

这个脚本为游戏中的AI提供了一个攻击行为的基础框架,可以根据具体游戏的需求进行扩展和修改。

----------------------------------------------------------------local require = requirelocal BASE = require("logic/entity/ai/ai_base").ai_base;------------------------------------------------------
ai_attack = class("ai_attack", BASE);
function ai_attack:ctor(entity)self._type = eAType_ATTACK;
endfunction ai_attack:IsValid()local entity = self._entity;if entity:IsDead() or not entity:CanAttack() thenreturn false;endif entity:GetEntityType() == eET_Player and entity._DigStatus == 2 thenreturn false;endif entity:GetEntityType() == eET_Trap thenif entity._ntype ~= eEntityTrapType_AOE thenreturn false;elseif entity._curSkill and entity._curSkill:CanUse() thenreturn trueendendendif not g_game_context:GetMapEnter() thenreturn false;endif entity._behavior:Test(eEBAttack) thenreturn true;endif entity._curSkill and entity._curSkill:CanUse() thenlocal target = entity._target;if target thenif not target:IsPlayer() and target:GetEntityType() == eET_Player thenif target._behavior:Test(eEBInvisible) thenreturn false;endendlocal dist = vec3_sub1(entity._curPos, target._curPos);if (entity._curSkill._range + (entity:GetRadius() + target:GetRadius())) > vec3_len(dist) thenreturn true;endreturn false;elseif entity:GetEntityType() == eET_Player then if entity._AutoFight thenreturn falseendelseif entity:GetEntityType() == eET_Mercenary thenif entity._cfg.ultraSkill == entity._curSkill._id thenreturn trueendif entity._curSkill._specialArgs.rushInfo and not entity._hoster:IsPlayer() thenreturn trueendendendreturn self:CanAttackNoneTarget();endreturn false;
endfunction ai_attack:CanAttackNoneTarget()return false;
endfunction ai_attack:OnEnter()if BASE.OnEnter(self) thenlocal entity = self._entity;local target = entity._target;local speed = entity:GetPropertyValue(ePropID_speed);if speed > 0 and entity._target and entity._target._guid ~= entity._guid thenlocal p1 = target._curPos;local p2 = entity._curPos;local rot_y = vec3_angle1(p1, p2, { x = 1, y = 0, z = 0 });entity:SetFaceDir(0, rot_y, 0);endself._startTick	= game_get_logic_time();self._skill		= entity._curSkill;self._canBreak	= self._skill._canBreak;self._duration	= self._skill._duration;self._attacker	= entity:StartAttack();self._movable = entity._movable;if not self._attacker thenreturn false;endself._entity._movable = false;return true;endreturn false;
endfunction ai_attack:OnLeave()if BASE.OnLeave(self) thenif self._canBreak and self._attacker thenself._attacker:StopAttack(false);endself._entity:FinishAttack();self._entity._movable = self._movable;return true;endreturn false;
endfunction ai_attack:OnUpdate(dTime)if not BASE.OnUpdate(self, dTime) then return false; endreturn true;
endfunction ai_attack:OnLogic(dTick)if not BASE.OnLogic(self, dTick) then return false; endlocal seq_skill = self._entity._seq_skill;if seq_skill and seq_skill.valid and seq_skill.parent == self._skill thenseq_skill.valid = false;if self._attacker thenif self._attacker:NextSequence(seq_skill.skill) thenself._duration	= seq_skill.skill._duration;self._startTick	= game_get_logic_time();endendendif self._skill thenif (game_get_logic_time() - self._startTick) * 1000 >= self._duration thenreturn false;endendreturn true;
endfunction create_component(entity, priority)return ai_attack.new(entity, priority);
end


 

http://www.lryc.cn/news/408254.html

相关文章:

  • 大数据之Oracle同步Doris数据不一致问题
  • visual studio 问题总结
  • go-错误码的最佳实践
  • Python面试题:使用Matplotlib和Seaborn进行数据可视化
  • 模拟实现c++中的vector模版
  • uniapp安卓通过绝对路径获取文件
  • Known框架实战演练——进销存业务单据
  • 解决npm依赖树冲突的方法以及npm ERR! code ERESOLVE错误的解决方案
  • Spring Boot + Spring Batch + Quartz 整合定时批量任务
  • C++STL简介(二)
  • 嵌入式高频面试题100道及参考答案(3万字长文)
  • python爬虫-事件触发机制
  • LeetCode-day27-3106. 满足距离约束且字典序最小的字符串
  • C++中的static_cast函数
  • 从零开始学习网络安全渗透测试之基础入门篇——(二)Web架构前后端分离站Docker容器站OSS存储负载均衡CDN加速反向代理WAF防护
  • 2679. 矩阵中的和
  • Unity Playables:下一代动画与音频序列
  • matlab仿真 模拟调制(下)
  • RabbitMQ是什么?
  • 追问试面试系列:分布式id
  • 护网紧急情况应对指南:Linux 应急响应手册
  • WEB攻防-通用漏洞-SQL 读写注入-MYSQLMSSQLPostgreSQL
  • 【前端学习笔记】CSS基础一
  • Github遇到的问题解决方法总结(持续更新...)
  • 数字信封+数字签名工具类测试样例(Java实现)
  • The Schematic workflow failed. See above.
  • 操作系统面试知识点总结4
  • Lua实现面向对象以及类的继承
  • 机器学习课程学习周报五
  • vue3.0学习笔记(二)——生命周期与响应式数据(ref,reactive,toRef,toRefs函数)