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`。
  1. **定义 `ai_attack` 类**:
  • `ai_attack = class("ai_attack", BASE);` 这行代码定义了 `ai_attack` 类并指定其基类为 `BASE`。
  1. **构造函数 (`ctor`)**:
  • `function ai_attack:ctor(entity)` 构造函数接受一个 `entity` 参数,并设置 `_type` 属性为 `eAType_ATTACK`。
  1. **`IsValid` 方法**:
  • 这个方法用于验证AI是否可以执行攻击。它检查实体是否死亡、是否可以攻击、是否是特定类型的实体等条件。
  1. **`CanAttackNoneTarget` 方法**:
  • 这个方法用于判断是否可以攻击没有目标的情况,当前实现返回 `false`。
  1. **`OnEnter` 方法**:
  • 当AI组件进入激活状态时执行。该方法处理目标定位、实体朝向调整、攻击开始逻辑等。
  • 如果实体有目标并且速度大于0,则计算目标和实体之间的旋转角度,并设置实体面向目标。
  1. **`OnLeave` 方法**:
  • 当AI组件离开激活状态时执行。该方法处理停止攻击的逻辑,并恢复实体的移动能力。
  1. **`OnUpdate` 方法**:
  • 每帧调用,用于更新AI状态。如果基类的 `OnUpdate` 方法返回 `false`,则当前方法也返回 `false`。
  1. **`OnLogic` 方法**:
  • 逻辑更新方法,用于处理技能序列、检查攻击持续时间等。

  • 如果技能序列有效且是当前技能的子技能,则执行相关逻辑。

  • 检查自攻击开始以来的时间是否已经超过技能的持续时间,如果是,则返回 `false`。

  1. **创建组件函数**:
  • `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提供了一个攻击行为的基础框架,可以根据具体游戏的需求进行扩展和修改。

Lua 复制代码
----------------------------------------------------------------

local require = require

local BASE = require("logic/entity/ai/ai_base").ai_base;

------------------------------------------------------
ai_attack = class("ai_attack", BASE);
function ai_attack:ctor(entity)
	self._type = eAType_ATTACK;
end

function ai_attack:IsValid()
	local entity = self._entity;

	if entity:IsDead() or not entity:CanAttack() then
		return false;
	end
	if entity:GetEntityType() == eET_Player and entity._DigStatus == 2 then
		return false;
	end

	if entity:GetEntityType() == eET_Trap then
		if entity._ntype ~= eEntityTrapType_AOE then
			return false;
		else
			if entity._curSkill and entity._curSkill:CanUse() then
				return true
			end
		end
	end

	if not g_game_context:GetMapEnter() then
		return false;
	end

	if entity._behavior:Test(eEBAttack) then
		return true;
	end

	if entity._curSkill and entity._curSkill:CanUse() then
		local target = entity._target;
		if target then
			if not target:IsPlayer() and target:GetEntityType() == eET_Player then
				if target._behavior:Test(eEBInvisible) then
					return false;
				end
			end
			local dist = vec3_sub1(entity._curPos, target._curPos);
			if (entity._curSkill._range + (entity:GetRadius() + target:GetRadius())) > vec3_len(dist) then
				return true;
			end

			return false;
		else
			if entity:GetEntityType() == eET_Player then 
				if entity._AutoFight then
					return false
				end
			elseif entity:GetEntityType() == eET_Mercenary then
				if entity._cfg.ultraSkill == entity._curSkill._id then
					return true
				end

				if entity._curSkill._specialArgs.rushInfo and not entity._hoster:IsPlayer() then
					return true
				end
			end
		end

		return self:CanAttackNoneTarget();
	end

	return false;
end

function ai_attack:CanAttackNoneTarget()
	return false;
end

function ai_attack:OnEnter()
	if BASE.OnEnter(self) then
		local 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 then
			local 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);
		end

		self._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 then
			return false;
		end

		self._entity._movable = false;

		return true;
	end

	return false;
end

function ai_attack:OnLeave()
	if BASE.OnLeave(self) then
		if self._canBreak and self._attacker then
			self._attacker:StopAttack(false);
		end

		self._entity:FinishAttack();

		self._entity._movable = self._movable;

		return true;
	end

	return false;
end

function ai_attack:OnUpdate(dTime)
	if not BASE.OnUpdate(self, dTime) then return false; end

	return true;
end

function ai_attack:OnLogic(dTick)
	if not BASE.OnLogic(self, dTick) then return false; end

	local seq_skill = self._entity._seq_skill;
	if seq_skill and seq_skill.valid and seq_skill.parent == self._skill then
		seq_skill.valid = false;

		if self._attacker then
			if self._attacker:NextSequence(seq_skill.skill) then
				self._duration	= seq_skill.skill._duration;
				self._startTick	= game_get_logic_time();
			end
		end
	end

	if self._skill then
		if (game_get_logic_time() - self._startTick) * 1000 >= self._duration then
			return false;
		end
	end

	return true;
end

function create_component(entity, priority)
	return ai_attack.new(entity, priority);
end
相关推荐
池央1 分钟前
CANN oam-tools 诊断体系深度解析:自动化信息采集、AI Core 异常解析与 CI/CD 流水线集成策略
人工智能·ci/cd·自动化
CV@CV5 分钟前
2026自动驾驶商业化提速——从智驾平权到Robotaxi规模化落地
人工智能·机器学习·自动驾驶
财经三剑客6 分钟前
AI元年,春节出行安全有了更好的答案
大数据·人工智能·安全
艾莉丝努力练剑15 分钟前
图像处理全栈加速:ops-cv算子库在CV领域的应用
图像处理·人工智能
tq108617 分钟前
AI 时代的3类程序员
人工智能
island131417 分钟前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
呆呆敲代码的小Y29 分钟前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
骥龙31 分钟前
第六篇:AI平台篇 - 从Jupyter Notebook到生产级模型服务
ide·人工智能·jupyter
TOPGUS32 分钟前
谷歌SEO第三季度点击率趋势:榜首统治力的衰退与流量的去中心化趋势
大数据·人工智能·搜索引擎·去中心化·区块链·seo·数字营销
松☆1 小时前
CANN深度解析:构建高效AI推理引擎的软件基
人工智能