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
相关推荐
千宇宙航19 分钟前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
onceco1 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
jndingxin4 小时前
OpenCV CUDA模块设备层-----高效地计算两个 uint 类型值的带权重平均值
人工智能·opencv·计算机视觉
Sweet锦4 小时前
零基础保姆级本地化部署文心大模型4.5开源系列
人工智能·语言模型·文心一言
hie988945 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
晨同学03275 小时前
opencv的颜色通道问题 & rgb & bgr
人工智能·opencv·计算机视觉
蓝婷儿5 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
大千AI助手5 小时前
PageRank:互联网的马尔可夫链平衡态
人工智能·机器学习·贝叶斯·mc·pagerank·条件概率·马尔科夫链
TESmart碲视5 小时前
HKS201-M24 大师版 8K60Hz USB 3.0 适用于 2 台 PC 1台显示器 无缝切换 KVM 切换器
单片机·嵌入式硬件·物联网·游戏·计算机外设·电脑·智能硬件
小和尚同志5 小时前
Cline | Cline + Grok3 免费 AI 编程新体验
人工智能·aigc