网站tkd优化,常熟网站设计,建网站的注意事项,无锡网站程序这段Lua脚本定义了一个名为 ai_attack 的类#xff0c;继承自 ai_base 类。
lua 游戏架构 之 游戏 AI #xff08;一#xff09;ai_base-CSDN博客文章浏览阅读119次。定义了一套接口和属性#xff0c;可以基于这个基础类派生出具有特定行为的AI组件。例如#xff0c;可以…这段Lua脚本定义了一个名为 ai_attack 的类继承自 ai_base 类。
lua 游戏架构 之 游戏 AI 一ai_base-CSDN博客文章浏览阅读119次。定义了一套接口和属性可以基于这个基础类派生出具有特定行为的AI组件。例如可以创建追逐敌人的AI、巡逻的AI或使用特定策略的AI等都继承自这个基础类https://blog.csdn.net/heyuchang666/article/details/140624481?spm1001.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