博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【cocos2d-x 手游研发----怪物智能AI】
阅读量:7034 次
发布时间:2019-06-28

本文共 7231 字,大约阅读时间需要 24 分钟。

原创文章,转载请注明出处:

 

谈到怪物AI,我觉得就比较话多了,首先理解一下(Artificial Intelligence人工智能),人工智能是计算机科学的一个分支,人工智能是计算机科学技术的前沿科技领域。人工智能与计算机软件有密切的关系。各种人工智能应用系统都要用计算机软件去实现,许多聪明的计算机软件也应用了人工智能的理论方法和技术。

总而言之,言而总之,我理解的AI就是:用人类自己的思考模式去赋予游戏中角色判断的能力,来进行某些特定的行为!

下面具体图文介绍一下,我在游戏中一些实用的AI制作思路;

 

 

这是这个思路就可以让怪物捕获玩家,并且追踪玩家攻击的一个图示!这个思路是基本,最终决策怪物是否去执行这个行为的操作算法又是一个AI智能,所以咱们又可以分为:

决策AI:可以根据怪物等级,怪物的类型(BOSS),怪物技能类型等很多决策和根本属性相连所起作用的操作,都可以做成决策AI;

行为AI:追踪玩家,攻击玩家,给玩家加血(如同伴),逃跑等这些行为动作操作;

OK,下面大家应该了解AI的基本思路了,那我们直接贴代码了;

我直接把AI写在了地图层里面了,因为之前我框架的设计,是有一个地图base,而所有的怪物,主角色,NPC都属于地图层,因为要做很多遍历操作,所以在地图base中写上智能AI算法,然后再继承拓展下来。

首先先制造一群怪物!并且让怪物在设置的Rect内随机移动!

/********************************************注释:makenum 最大怪物数量monsteridtags 怪物起始标签randomrange 配置怪圈大小(只能填正整数)monster_model 怪物数据yinzihigth 影子的距离fristpoint 怪物初始设定的怪圈点*********************************************/void Maps_Diyu::makemonster(int makenum,int monsteridtags,CCPoint randomrange,MainRoledata monster_model,int yinzihigth,CCPoint fristpoint){    for (int i = 0; i < makenum; i++)    {        int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);        int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);        monster_model.tags=monsteridtags+i;        monster_model.nowpoint= CCPointMake(nowmap->getContentSize().width/2+fristpoint.x+add_x,nowmap->getContentSize().height/2+fristpoint.y+add_y);        SpiritsMonster* newmonster = new SpiritsMonster(monster_model,1,yinzihigth);        nowmap->addChild(newmonster->monster, 1,monsteridtags+i);    }}

然后处理怪物的行为AI:

/********************************************注释:monsternum 最大怪物数量monsteridtags 怪物起始标签monster_model 怪物数据reatR 怪物仇恨视角的范围值(通常200左右)attck_reatR 怪物可攻击范围值(远程怪和物理怪物)playerpoint 主角坐标randomrange 怪物默认随机移动范围fristpoint 怪物初始设定的怪圈点*********************************************/void Maps_Diyu::attact_todo(int monsternum,int monsteridtags ,MainRoledata monster_model,int reatR,int attck_reatR,CCPoint playerpoint,CCPoint randomrange,CCPoint fristpoint){    for (int i = 0; i < monsternum; i++)    {        CCRect* attck_rect = new CCRectMake(this->getChildByTag(monsteridtags+i)->getPosition().x-attck_reatR/2,this->getChildByTag(monsteridtags+i)->getPosition().y-attck_reatR/2,attck_reatR,attck_reatR);        if(attck_rect->containsPoint(playerpoint)==true)        {            //执行怪物攻击主角            CCLog(FontChina::G2U("进入怪物可攻击范围"));            //攻击时改变方向            SpiritsMonster::attackTomainRole_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);            //释放技能动画            int add_x = (int)(CCRANDOM_MINUS1_1()*8);            int add_y =(int)(CCRANDOM_MINUS1_1()*8);            SkillEffects* skill = new SkillEffects(ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y),monster_model,(0.2f+CCRANDOM_0_1()),8,3,1000);            this->addChild(skill->effects_main,1000,50);        }        else        {            CCRect* track_rect = new CCRectMake(this->getChildByTag(monsteridtags+i)->getPosition().x-reatR/2,this->getChildByTag(monsteridtags+i)->getPosition().y-reatR/2,reatR,reatR);            if(track_rect->containsPoint(playerpoint)==true)            {                //执行移动,不停追杀主角                CCLog(FontChina::G2U("进入怪物仇恨视野区域"));                //防止靠近主角,保持一定距离!主要为了好看和处理被攻击效果。                int add_x = (int)(CCRANDOM_MINUS1_1()*25);                int add_y =(int)(CCRANDOM_MINUS1_1()*25);                //移动时改变方向                SpiritsMonster::moveTomap_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);                CCArray* callbackArray = CCArray::create();                int nowtags = monsteridtags+i;                char strs[64];                sprintf(strs,"%d",nowtags);                CCString* ccnowtags = CCString::create(strs);                callbackArray->addObject(ccnowtags);                callbackArray->addObject(monster_model.spiritname);                char dir_x[64];                sprintf(dir_x,"%d",(int)this->getChildByTag(nowtags)->getPositionX());                char dir_y[64];                sprintf(dir_y,"%d",(int)this->getChildByTag(nowtags)->getPositionY());                CCString* d_x = CCString::create(dir_x);                CCString* d_y = CCString::create(dir_y);                callbackArray->addObject(d_x);                callbackArray->addObject(d_y);                CCFiniteTimeAction *actbackfun = CCCallFuncO::create(this, callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);                CCActionInterval* act_movexixue = CCMoveTo::create(3+CCRANDOM_MINUS1_1(),ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y));                this->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL));            }            else            {                //脱离仇恨时,随机定点周围移动                int stopandrun = (int)(CCRANDOM_MINUS1_1()*10);                if(stopandrun>=1)                {                    //脱离仇恨                    int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);                    int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);                    CCPoint move_dir = ccp(this->getContentSize().width/2+fristpoint.x+add_x,this->getContentSize().height/2+fristpoint.y+add_y);                    SpiritsMonster::moveTomap_dir(ccp((int)move_dir.x,(int)move_dir.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);                    CCArray* callbackArray = CCArray::create();                    int nowtags = monsteridtags+i;                    char strs[64];                    sprintf(strs,"%d",nowtags);                    CCString* ccnowtags = CCString::create(strs);                    callbackArray->addObject(ccnowtags);                    callbackArray->addObject(monster_model.spiritname);                    char dir_x[64];                    sprintf(dir_x,"%d",(int)this->getChildByTag(nowtags)->getPositionX());                    char dir_y[64];                    sprintf(dir_y,"%d",(int)this->getChildByTag(nowtags)->getPositionY());                    CCString* d_x = CCString::create(dir_x);                    CCString* d_y = CCString::create(dir_y);                    callbackArray->addObject(d_x);                    callbackArray->addObject(d_y);                    CCFiniteTimeAction *actbackfun = CCCallFuncO::create(this, callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);                    CCActionInterval* act_movexixue = CCMoveTo::create(3+CCRANDOM_MINUS1_1(),ccp(move_dir.x,move_dir.y));                    this->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL));                }            }        }    }}

这段逻辑包括了,如果玩家进入怪物的仇恨区域,那么怪物会先移动到玩家附近的点(此时玩家移动开,怪物会根据定时器去判断下一步如何操作:如1,玩家移动后还在仇恨区域,那么继续追踪玩家;2,如果玩家脱离仇恨,那么怪物执行回到初始坐标点的操作,然后继续原地巡逻!),如果此时玩家已经在怪物的攻击范围内了,那么怪物会直接选择攻击!这样怪物就会变得貌似有了思想一样,他会去考虑判断各种行为,那个区域我该做什么?不该做什么?

有了这个逻辑,我们还要赋予怪物循环思考的能力!

//启动所有怪物智能AI监听器    nowmap->schedule(schedule_selector(Maps_Diyu::makemonsterAttack), (3.0f));

我们根据定时器去每隔3秒内去监控行为AI!

整个行为AI已经完毕!就可以实现比较智能的怪物追杀主角了!这也就牵扯到了游戏难度怪物难度的问题了,往往好玩的游戏智能AI都占用了程序一半以上时间去处理;怪物并不是血多,防御高他就是一个BOSS,如果BOSS他会满地图追着玩家打,会自己加血,逃跑,那这样的BOSS我们作为玩家来说,打起来也比较刺激有意思,有挑战!

这篇就写这么多了,下回分解一下游戏界面UI交互的一些东西,充分运用定时器去管理一些玩家对屏幕基本操作的逻辑;

PS:COCOS2DQQ群 ,和大家一起分享cocos2dx开发经验【41131516】

你可能感兴趣的文章
【状压DP】【TSP问题专题】
查看>>
System-Defined Device Setup Classes Available to Vendors
查看>>
实验七:Linux内核如何装载和启动一个可执行程序
查看>>
性能测试工具、方法
查看>>
mysql常见优化,更多mysql,Redis,memcached等文章
查看>>
github+hexo(window10)
查看>>
树(7)-----二叉树的序列化和反序列化
查看>>
13.Fibonacci数
查看>>
easyui tab 关闭 打开
查看>>
ZendStudio快捷键
查看>>
前端性能优化
查看>>
浅谈 串行信号 转换成 并行信号 原理
查看>>
分布式系统(Distributed System)资料
查看>>
underscore.js库的浅析
查看>>
Android 通过软引用实现图片缓存,防止内存溢出
查看>>
EXT.NET常用控件使用
查看>>
C语言两种方式实现矩阵的转置
查看>>
javaee自定义servlet的步骤
查看>>
更进ATM
查看>>
C#和.Net的关系
查看>>