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

lua 游戏架构 之 游戏 AI (七)ai_dead

定义一个名为`ai_dead`的类,继承自`ai_base`类。这个类用于处理游戏中AI在死亡状态下的行为逻辑。以下是对代码的具体解释:

1. **引入基类**:
   - 使用`require`函数引入`ai_base`类,作为基础类。

2. **定义`ai_dead`类**:
   - 使用`class`关键字定义了`ai_dead`类,并继承自`BASE`(即`ai_base`)。

3. **构造函数 (`ctor`)**:
   - 构造函数接受一个`entity`参数,并设置`_type`属性为`eAType_DEAD`,表示死亡的行为。

4. **`IsValid` 方法**:
   - 这个方法用于验证AI是否应该处于死亡状态。它首先调用基类的`IsValid`方法,然后检查实体是否死亡。

5. **`OnEnter` 方法**:

  •      - 当AI组件进入激活状态时执行。如果基类的`OnEnter`方法返回`true`,则执行以下操作:
  •      - 重置`_fadeOut`和`_slowMotion`标志。
  •      - 检查并重置玩家的自动普通攻击状态。
  •      - 隐藏实体的称号节点。
  •      - 检查并重置玩家的超级模式状态。
  •      - 检查并重置骑乘状态。
  •      - 如果实体不是宠物或技能类型,播放死亡动作列表。
  •      - 锁定实体动画。
  •      - 处理同步RPC和发送死亡命令。
  •      - 处理玩家死亡后的界面逻辑,如复活界面和宠物状态。
  •      - 清理死亡实体的仇恨列表和战斗时间。

6. **`OnLeave` 方法**: 当AI组件离开激活状态时执行。如果基类的`OnLeave`方法返回`true`,则解锁实体动画。

7. **`OnLogic` 方法**: 逻辑更新方法。如果基类的`OnLogic`方法返回`true`,则根据时间间隔处理死亡逻辑,如淡出效果、实体销毁等。

8. **创建组件函数**: `create_component`函数用于创建`ai_dead`类的新实例,传入一个实体和一个优先级。

代码中的一些关键点:

  • - `IsDead()`:检查实体是否死亡。
  • - `ShowTitleNode()`:显示或隐藏实体的称号节点。
  • - `SuperMode()`:处理玩家的超级模式。
  • - `OnRideMode()`:处理玩家的骑乘模式。
  • - `PlayActionList()`:播放一系列动作。
  • - `LockAni()`:锁定实体的动画。
  • - `Show()`:控制实体的显示与隐藏。
  • - `Destory()`:销毁实体。
  • - `CanRelease()`:检查实体是否可以释放。

`OnEnter` 方法的逻辑流程:

  • - 调用基类的`OnEnter`方法,如果返回`true`,则继续。
  • - 重置相关动画和攻击状态。
  • - 隐藏称号节点。
  • - 检查并重置超级模式和骑乘状态。
  • - 播放死亡动作列表并锁定动画。
  • - 发送死亡同步命令和处理复活界面逻辑。
  • - 清理仇恨列表和战斗时间。

`OnLogic` 方法中,根据时间间隔处理死亡后的逻辑,如:

  • - 如果实体存活时间超过4秒,并且可以释放,则销毁实体。
  • - 如果实体存活时间超过2秒,并且可以释放,则开始淡出效果。

整体而言,`ai_dead`类的目的是在AI实体死亡时,提供一套标准的行为和逻辑处理,确保游戏内死亡状态的表现和交互符合预期。

local require = requirelocal BASE = require("logic/entity/ai/ai_base").ai_base;------------------------------------------------------
ai_dead = class("ai_dead", BASE);
function ai_dead:ctor(entity)self._type = eAType_DEAD;
endfunction ai_dead:IsValid()if not BASE.IsValid(self) then return false; endreturn self._entity:IsDead();
endfunction ai_dead:OnEnter()if BASE.OnEnter(self) thenself._fadeOut	= false;self._slowMotion= false;local logic = game_get_logic();if logic thenlocal player = logic:GetPlayer();if player and player:GetHero() thenlocal hero = player:GetHero();if hero thenif hero._autonormalattack thenif hero._guid == self._entity._guid thenhero._preautonormalattack = false;hero._autonormalattack = false;elseif self._entity._selected == self._entity._guid thenhero._preautonormalattack = false;hero._autonormalattack = false;elseif hero._follow thenif hero._follow._guid == self._entity._guid thenhero._preautonormalattack = false;hero._autonormalattack = false;endendendendendendself._entity:ShowTitleNode(false);if self._entity:GetEntityType() == eET_Player then--清神兵状态if self._entity._superMode.valid thenlocal hero = game_get_player_hero()if self._entity:IsPlayer() thenself._entity:SuperMode(false)elseself._entity:OnSuperMode(false)endendif self._entity:IsPlayer() theng_game_context:ResetLeadMode()local animation = g_game_context:GetSelectWeaponMaxAnimation()if animation theng_game_context:OnStuntAnimationChangeHandler(animation,false)endend--清骑乘local world = game_get_world()if world and not world._syncRpc thenif self._entity:IsOnRide() thenself._entity:OnRideMode(false, true)endendif self._entity._DIYSkill thenself._entity._DIYSkill:OnReset();endendif self._entity:GetEntityType() ~= eET_Pet and self._entity:GetEntityType() ~= eET_Skill thenlocal alist = {}table.insert(alist, {actionName = db_common.engine.defaultDeadAction, actloopTimes = 1})table.insert(alist, {actionName = db_common.engine.defaultDeadLoopAction, actloopTimes = -1})self._entity:PlayActionList(alist, 1);endself._entity:LockAni(true);local logic = game_get_logic();if logic thenlocal world = logic:GetWorld();if world thenlocal syncRpc = world._syncRpc;if not syncRpc then-- send cmdlocal entity = self._entity;if entity and entity:GetEntityType() == eET_Monster and entity._spawnID > 0 thenlocal weaponID = g_game_context:IsInSuperMode() and 1 or 0local damageRank = g_game_context:GetMapCopyDamageRank()local args = { spawnPointID = entity._spawnID , pos = entity._curPos, weaponID = weaponID, damageRank = damageRank}sbean.sync_privatemap_kill(args)endlocal hero = game_get_player_hero()if hero thenhero:RemoveSummoned();endendendendif self._entity:GetEntityType() == eET_Player thenif logic thenlocal player = logic:GetPlayer()local hero = nilif player thenhero = player:GetHero()endlocal logic = game_get_logic();local world = logic:GetWorld()local PetCount = player:GetPetCount()for i = 1,tonumber(PetCount) dolocal Pet = player:GetPet(i)Pet:OnDead()endif g_db.db_get_is_open_revive_ui() then	if hero._guid == self._entity._guid thenif hero._killerId and hero._killerId < 0 then--天雷复活界面local killerID = math.abs(hero._killerId);local guid = string.split(hero._guid, "|")local playerId = tonumber(guid[2])if killerID == playerId theng_ui_mgr:OpenUI(eUIID_PlayerRevive)g_ui_mgr:RefreshUI(eUIID_PlayerRevive, true)endelseg_logic:OpenReviveUI()end--log("eUIID_PlayerRevive")local MercenaryCount = player:GetMercenaryCount();for i = 1,tonumber(MercenaryCount) dolocal Mercenary = player:GetMercenary(i)Mercenary:OnDead()endendelseif hero._guid == self._entity._guid and (game_get_map_type() == g_ARENA_SOLO or game_get_map_type() == g_TAOIST) theng_game_context:SetAutoFight(false)local MercenaryCount = player:GetMercenaryCount();for i = 1,tonumber(MercenaryCount) dolocal Mercenary = player:GetMercenary(i)if not Mercenary:IsDead() thenlocal camera = logic:GetMainCamera()hero:DetachCamera()Mercenary:AttachCamera(camera);camera:UpdatePos(Mercenary._curPosE);break;endendendendif world and not world._syncRpc thenself._entity:ClsHorseAi();endendendif self._entity._forceAttackTarget thenself._entity._forceAttackTarget = nil;endif self._entity:GetEntityType() == eET_Mercenary thenself._entity._deadTimeLine = g_get_GMTtime(game_get_time())if logic thenlocal player = logic:GetPlayer()local hero = nilif player thenhero = player:GetHero()endlocal logic = game_get_logic();local world = logic:GetWorld()if world._fightmap thenif hero:IsDead() and (game_get_map_type() == g_ARENA_SOLO or game_get_map_type() == g_TAOIST) thenlocal guid1 = string.split(hero._guid, "_")local guid2 = string.split(self._entity._guid, "_")	if tonumber(guid1[2]) == tonumber(guid2[3]) thenlocal MercenaryCount = player:GetMercenaryCount();for i = 1,tonumber(MercenaryCount) dolocal Mercenary = player:GetMercenary(i)if not Mercenary:IsDead() thenlocal camera = logic:GetMainCamera()self._entity:DetachCamera()Mercenary:AttachCamera(camera);camera:UpdatePos(Mercenary._curPosE);break;endendendendendendself._entity:ClsEnmities();endif self._entity:GetEntityType() == eET_Player or self._entity:GetEntityType() == eET_Mercenary thenself._entity:ClearFightTime();endif self._entity:GetEntityType() ~= eET_Player and self._entity:GetEntityType() ~= eET_Mercenary then--self._entity:SetHittable(false);endif self._entity:GetEntityType() == eET_Pet thenlocal world = game_get_world();local player = game_get_player()if world and not world._syncRpc thenif self._entity._hoster thenlocal curFightSP = self._entity._hoster:GetFightSp()self._entity._hoster:UpdateFightSpCanYing(curFightSP - 1)local index = player:GetPetIdx(self._entity._guid)if index > 0  thenplayer:RmvPet(index)endendendendreturn true;endreturn false;
endfunction ai_dead:OnLeave()if BASE.OnLeave(self) thenself._entity:LockAni(false);return true;endreturn false;
endfunction ai_dead:OnLogic(dTick)if BASE.OnLogic(self, dTick) thenif dTick > 0 thenif self._timeTick > 4000 thenif self._entity:CanRelease() thenself._entity:Destory()endelseif self._timeTick > 2000 thenif self._entity:CanRelease() thenif not self._fadeOut thenself._fadeOut = true;self._entity:Show(false, true, 2000);endendendif self._entity:GetEntityType() == eET_Pet or self._entity:GetEntityType() == eET_Skill thenif self._entity:CanRelease() thenself._entity:Destory()endendendreturn true;endreturn false;
endfunction create_component(entity, priority)return ai_dead.new(entity, priority);
end


 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 爬取贴吧的标题和链接
  • 加拿大云手机:进入加拿大市场的最佳工具
  • UE5 C++在Cesium上给定一个坐标垂直地面射线正交的地形高度
  • 监控系列(八)部署dameng_exporter并对接prometheus
  • CVE-2020-7248 OpenWRT libubox标记二进制数据序列化漏洞(更新中)
  • MySQL SQL 编程练习
  • 深度解读大语言模型中的Transformer架构
  • Jetpack Compose 通过 OkHttp 发送 HTTP 请求的示例
  • FTP传输的两种模式的技术原理和应用
  • vue3+element-plus 实现动态菜单和动态路由的渲染
  • 传神社区|数据集合集第7期|法律NLP数据集合集
  • 【芯智雲城】详解智能电机驱动在汽车中的应用
  • GUI界面开发之tkinter(二) 学习文本组件
  • k8s部署kafka集群
  • Navicat图形化管理工具安装教程
  • Akka系列(七):Actor持久化之Akka persistence
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • Angular6错误 Service: No provider for Renderer2
  • JavaScript服务器推送技术之 WebSocket
  • k8s如何管理Pod
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Linux各目录及每个目录的详细介绍
  • Shell编程
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • 排序(1):冒泡排序
  • 扑朔迷离的属性和特性【彻底弄清】
  • 前端路由实现-history
  • 如何进阶一名有竞争力的程序员?
  • 首页查询功能的一次实现过程
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • ​如何防止网络攻击?
  • ​学习一下,什么是预包装食品?​
  • # Redis 入门到精通(一)数据类型(4)
  • ()、[]、{}、(())、[[]]命令替换
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (java)关于Thread的挂起和恢复
  • (八)Spring源码解析:Spring MVC
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (亲测有效)推荐2024最新的免费漫画软件app,无广告,聚合全网资源!
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (一)基于IDEA的JAVA基础1
  • (一)模式识别——基于SVM的道路分割实验(附资源)
  • (转)memcache、redis缓存
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (轉貼) UML中文FAQ (OO) (UML)
  • ***详解账号泄露:全球约1亿用户已泄露
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET C# 配置 Options
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .net6+aspose.words导出word并转pdf
  • .NET8 动态添加定时任务(CRON Expression, Whatever)